Skip to content

Commit

Permalink
feat: Add IServiceCollection.AddBoleroComponents
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarmil committed Dec 17, 2023
1 parent 311b1e8 commit 432efe4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
16 changes: 15 additions & 1 deletion src/Bolero.Server/Extensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ type ServerComponentsExtensions =
static member RenderBoleroScript(html: IHtmlHelper, config: IBoleroHostConfig) =
html.Raw(BoleroHostConfig.Body(config))

/// <summary>Configure the hosting of server-side and WebAssembly Bolero components.</summary>
/// <summary>
/// Configure the hosting of server-side and WebAssembly Bolero components using Bolero's legacy render mode handling.
/// </summary>
/// <param name="server">If true, use server-side Bolero; if false, use WebAssembly. Default is false.</param>
/// <param name="prerendered">If true, prerender the initial view in the served HTML. Default is true.</param>
/// <param name="devToggle">
Expand All @@ -111,9 +113,21 @@ type ServerComponentsExtensions =
else
this.AddSingleton(
{ new IBoleroHostConfig with
member _.IsInteractiveComponents = false
member _.IsServer = server
member _.IsPrerendered = prerendered })

/// <summary>
/// Configure the hosting of Bolero components using interactive render modes.
/// </summary>
[<Extension>]
static member AddBoleroComponents(this: IServiceCollection) =
this.AddSingleton(
{ new IBoleroHostConfig with
member _.IsInteractiveComponents = true
member _.IsServer = false
member _.IsPrerendered = false })

/// <summary>
/// Adds a route endpoint that will match requests for non-file-names with the lowest possible priority.
/// The request will be routed to a Bolero page.
Expand Down
24 changes: 20 additions & 4 deletions src/Bolero.Server/HostConfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,24 @@ open Microsoft.AspNetCore.Http
open Microsoft.Extensions.Hosting

/// <summary>
/// The Bolero hosting configuration set by <see cref="M:Bolero.Server.ServerComponentsExtensions.AddBoleroHost" />.
/// The Bolero hosting configuration set by <see cref="M:Bolero.Server.ServerComponentsExtensions.AddBoleroHost" />
/// or <see cref="M:Bolero.Server.ServerComponentsExtensions.AddBoleroComponents" />.
/// </summary>
type IBoleroHostConfig =
/// <summary>If true, use server-side Bolero; if false, use WebAssembly.</summary>
/// <summary>
/// If true, use Bolero with interactive render modes, and bypass <see cref="P:IsServer"/> and <see cref="P:IsPrerendered"/>.
/// If false, use Bolero's legacy render mode handling.
/// </summary>
abstract IsInteractiveComponents: bool
/// <summary>
/// If true, use server-side Bolero; if false, use WebAssembly.
/// Only applies if <see cref="IsInteractiveComponents"/> is false.
/// </summary>
abstract IsServer: bool
/// <summary>If true, prerender the initial view in the served HTML.</summary>
/// <summary>
/// If true, prerender the initial view in the served HTML.
/// Only applies if <see cref="IsInteractiveComponents"/> is false.
/// </summary>
abstract IsPrerendered: bool

/// <exclude />
Expand All @@ -56,7 +68,11 @@ type BoleroHostConfig(baseConfig: IBoleroHostBaseConfig, env: IHostEnvironment,
interface IBoleroHostConfig with
member this.IsServer = this.IsServer
member this.IsPrerendered = this.IsPrerendered
member this.IsInteractiveComponents = false

static member internal Body(config: IBoleroHostConfig) =
let k = if config.IsServer then "server" else "webassembly"
let k =
if config.IsInteractiveComponents then "web"
elif config.IsServer then "server"
else "webassembly"
$"""<script src="_framework/blazor.{k}.js"></script>"""
2 changes: 2 additions & 0 deletions tests/Remoting.Client/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
module Bolero.Tests.Remoting.Client

open System.Collections.Generic
open Microsoft.AspNetCore.Components
open Microsoft.AspNetCore.Components.Authorization
open Bolero
open Bolero.Html
Expand Down Expand Up @@ -220,6 +221,7 @@ let Display model dispatch =
}
}

[<BoleroRenderMode(BoleroRenderMode.Auto); Route "/{*path}">]
type MyApp() =
inherit ProgramComponent<Model, Message>()

Expand Down
45 changes: 27 additions & 18 deletions tests/Remoting.Server/Startup.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,22 @@ module Page =
open Bolero.Html
open Bolero.Server.Html

let index = doctypeHtml {
head {
title { "Bolero (remoting)" }
meta { attr.charset "UTF-8" }
``base`` { attr.href "/" }
}
body {
div { attr.id "main"; comp<MyApp> }
script { attr.src "_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js" }
boleroScript
}
}
type Page() =
inherit Bolero.Component()

override _.Render() =
doctypeHtml {
head {
title { "Bolero (remoting)" }
meta { attr.charset "UTF-8" }
Bolero.Html.``base`` { attr.href "/" }
}
body {
div { attr.id "main"; comp<MyApp> }
script { attr.src "_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js" }
boleroScript
}
}

type MyApiHandler(log: ILogger<MyApiHandler>, ctx: IRemoteContext) =
inherit RemoteHandler<MyApi>()
Expand Down Expand Up @@ -91,14 +95,16 @@ type MyApiHandler(log: ILogger<MyApiHandler>, ctx: IRemoteContext) =
type Startup() =

member this.ConfigureServices(services: IServiceCollection) =
services.AddMvc() |> ignore
services.AddRazorComponents()
.AddInteractiveServerComponents()
.AddInteractiveWebAssemblyComponents()
|> ignore
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie()
|> ignore
services
.AddBoleroRemoting<MyApiHandler>()
.AddBoleroHost()
.AddServerSideBlazor()
.AddBoleroComponents()
|> ignore
services.AddSwaggerForSystemTextJson(JsonFSharpOptions()) |> ignore
services.AddEndpointsApiExplorer() |> ignore
Expand All @@ -110,13 +116,16 @@ type Startup() =
.UseSwaggerUI()
.UseRouting()
.UseAuthorization()
.UseBlazorFrameworkFiles()
.UseAntiforgery()
.UseEndpoints(fun endpoints ->
endpoints.MapBlazorHub() |> ignore
endpoints.MapBoleroRemoting()
.WithOpenApi()
|> ignore
endpoints.MapFallbackToBolero(Page.index) |> ignore)
endpoints.MapRazorComponents<Page.Page>()
.AddInteractiveServerRenderMode()
.AddInteractiveWebAssemblyRenderMode()
.AddAdditionalAssemblies(typeof<MyApp>.Assembly)
|> ignore)
|> ignore

if env.IsDevelopment() then
Expand Down

0 comments on commit 432efe4

Please sign in to comment.