Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add injectable configuration for server/client mode and prerendering in razor #141

Merged
merged 2 commits into from
May 21, 2020

Conversation

Tarmil
Copy link
Member

@Tarmil Tarmil commented Apr 26, 2020

This adds a cleaner way to configure _Host.cshtml by injecting a configuration. With this, a typical app changes as follows:

  • HostModel.fs is removed.

  • Pages/_Host.cshtml changes as follows:

     @page "/"
     @namespace HelloWorld
    -@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
    -@model HostModel
    +@using Bolero.Server.RazorHost
    +@inject IBoleroHostConfig BoleroHostConfig
     <!DOCTYPE html>
     <html>
     <head>
       <title>Hello world!</title>
       <meta charset="UTF-8" />
       <base href="/" />
     </head>
     <body>
    -  <div id="main">@(await Html.RenderComponentAsync<HelloWorld.Client.MyApp>(Model.IsServer ? RenderMode.ServerPrerendered : RenderMode.Static))</div>
    +  <div id="main">@(await Html.RenderComponentAsync<HelloWorld.Client.MyApp>(BoleroHostConfig))</div>
    -  <script src="_framework/blazor.@(Model.IsServer ? "server" : "webassembly").js"></script>
    +  @Html.RenderBoleroScript(BoleroHostConfig)
     </body>
     </html>
  • In Startup.fs, ConfigureServices must call services.AddBoleroHost(), which is located in Bolero.Server.RazorHost.

The method services.AddBoleroHost() is where this feature is configured. It takes 3 optional arguments:

  • server: bool determines whether the app is in server mode or client mode (default: false).
  • prerendered: bool determines whether the initial view is prerendered in the served HTML (default: true).
  • devToggle: bool determines whether it is possible to set the mode on-the-fly by requesting the URL with ?server=true or false when running with ASP.NET Core environment "development" (default: true).

@Tarmil Tarmil force-pushed the master branch 2 times, most recently from c26486c to cf7af92 Compare May 19, 2020 22:19
@Tarmil Tarmil merged commit b507741 into master May 21, 2020
@Tarmil Tarmil deleted the razorhost branch November 12, 2020 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant