Skip to content

Commit c680610

Browse files
committed
feat: Add IServiceCollection.AddBoleroComponents
1 parent 51615be commit c680610

File tree

4 files changed

+64
-23
lines changed

4 files changed

+64
-23
lines changed

src/Bolero.Server/Extensions.fs

+15-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ type ServerComponentsExtensions =
8888
static member RenderBoleroScript(html: IHtmlHelper, config: IBoleroHostConfig) =
8989
html.Raw(BoleroHostConfig.Body(config))
9090

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

120+
/// <summary>
121+
/// Configure the hosting of Bolero components using interactive render modes.
122+
/// </summary>
123+
[<Extension>]
124+
static member AddBoleroComponents(this: IServiceCollection) =
125+
this.AddSingleton(
126+
{ new IBoleroHostConfig with
127+
member _.IsInteractiveComponents = true
128+
member _.IsServer = false
129+
member _.IsPrerendered = false })
130+
117131
/// <summary>
118132
/// Adds a route endpoint that will match requests for non-file-names with the lowest possible priority.
119133
/// The request will be routed to a Bolero page.

src/Bolero.Server/HostConfig.fs

+20-4
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,24 @@ open Microsoft.AspNetCore.Http
2424
open Microsoft.Extensions.Hosting
2525

2626
/// <summary>
27-
/// The Bolero hosting configuration set by <see cref="M:Bolero.Server.ServerComponentsExtensions.AddBoleroHost" />.
27+
/// The Bolero hosting configuration set by <see cref="M:Bolero.Server.ServerComponentsExtensions.AddBoleroHost" />
28+
/// or <see cref="M:Bolero.Server.ServerComponentsExtensions.AddBoleroComponents" />.
2829
/// </summary>
2930
type IBoleroHostConfig =
30-
/// <summary>If true, use server-side Bolero; if false, use WebAssembly.</summary>
31+
/// <summary>
32+
/// If true, use Bolero with interactive render modes, and bypass <see cref="P:IsServer"/> and <see cref="P:IsPrerendered"/>.
33+
/// If false, use Bolero's legacy render mode handling.
34+
/// </summary>
35+
abstract IsInteractiveComponents: bool
36+
/// <summary>
37+
/// If true, use server-side Bolero; if false, use WebAssembly.
38+
/// Only applies if <see cref="IsInteractiveComponents"/> is false.
39+
/// </summary>
3140
abstract IsServer: bool
32-
/// <summary>If true, prerender the initial view in the served HTML.</summary>
41+
/// <summary>
42+
/// If true, prerender the initial view in the served HTML.
43+
/// Only applies if <see cref="IsInteractiveComponents"/> is false.
44+
/// </summary>
3345
abstract IsPrerendered: bool
3446

3547
/// <exclude />
@@ -56,7 +68,11 @@ type BoleroHostConfig(baseConfig: IBoleroHostBaseConfig, env: IHostEnvironment,
5668
interface IBoleroHostConfig with
5769
member this.IsServer = this.IsServer
5870
member this.IsPrerendered = this.IsPrerendered
71+
member this.IsInteractiveComponents = false
5972

6073
static member internal Body(config: IBoleroHostConfig) =
61-
let k = if config.IsServer then "server" else "webassembly"
74+
let k =
75+
if config.IsInteractiveComponents then "web"
76+
elif config.IsServer then "server"
77+
else "webassembly"
6278
$"""<script src="_framework/blazor.{k}.js"></script>"""

tests/Remoting.Client/Main.fs

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
module Bolero.Tests.Remoting.Client
2222

2323
open System.Collections.Generic
24+
open Microsoft.AspNetCore.Components
2425
open Microsoft.AspNetCore.Components.Authorization
2526
open Bolero
2627
open Bolero.Html
@@ -220,6 +221,7 @@ let Display model dispatch =
220221
}
221222
}
222223

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

tests/Remoting.Server/Startup.fs

+27-18
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,22 @@ module Page =
3838
open Bolero.Html
3939
open Bolero.Server.Html
4040

41-
let index = doctypeHtml {
42-
head {
43-
title { "Bolero (remoting)" }
44-
meta { attr.charset "UTF-8" }
45-
``base`` { attr.href "/" }
46-
}
47-
body {
48-
div { attr.id "main"; comp<MyApp> }
49-
script { attr.src "_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js" }
50-
boleroScript
51-
}
52-
}
41+
type Page() =
42+
inherit Bolero.Component()
43+
44+
override _.Render() =
45+
doctypeHtml {
46+
head {
47+
title { "Bolero (remoting)" }
48+
meta { attr.charset "UTF-8" }
49+
Bolero.Html.``base`` { attr.href "/" }
50+
}
51+
body {
52+
div { attr.id "main"; comp<MyApp> }
53+
script { attr.src "_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js" }
54+
boleroScript
55+
}
56+
}
5357

5458
type MyApiHandler(log: ILogger<MyApiHandler>, ctx: IRemoteContext) =
5559
inherit RemoteHandler<MyApi>()
@@ -91,14 +95,16 @@ type MyApiHandler(log: ILogger<MyApiHandler>, ctx: IRemoteContext) =
9195
type Startup() =
9296

9397
member this.ConfigureServices(services: IServiceCollection) =
94-
services.AddMvc() |> ignore
98+
services.AddRazorComponents()
99+
.AddInteractiveServerComponents()
100+
.AddInteractiveWebAssemblyComponents()
101+
|> ignore
95102
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
96103
.AddCookie()
97104
|> ignore
98105
services
99106
.AddBoleroRemoting<MyApiHandler>()
100-
.AddBoleroHost()
101-
.AddServerSideBlazor()
107+
.AddBoleroComponents()
102108
|> ignore
103109
services.AddSwaggerForSystemTextJson(JsonFSharpOptions()) |> ignore
104110
services.AddEndpointsApiExplorer() |> ignore
@@ -110,13 +116,16 @@ type Startup() =
110116
.UseSwaggerUI()
111117
.UseRouting()
112118
.UseAuthorization()
113-
.UseBlazorFrameworkFiles()
119+
.UseAntiforgery()
114120
.UseEndpoints(fun endpoints ->
115-
endpoints.MapBlazorHub() |> ignore
116121
endpoints.MapBoleroRemoting()
117122
.WithOpenApi()
118123
|> ignore
119-
endpoints.MapFallbackToBolero(Page.index) |> ignore)
124+
endpoints.MapRazorComponents<Page.Page>()
125+
.AddInteractiveServerRenderMode()
126+
.AddInteractiveWebAssemblyRenderMode()
127+
.AddAdditionalAssemblies(typeof<MyApp>.Assembly)
128+
|> ignore)
120129
|> ignore
121130

122131
if env.IsDevelopment() then

0 commit comments

Comments
 (0)