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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "3.1.102"
"version": "3.1.201"
}
}
1 change: 1 addition & 0 deletions src/Bolero.Server/Bolero.Server.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ItemGroup>
<Compile Include="HttpContext.fs" />
<Compile Include="Remoting.fs" />
<Compile Include="RazorHost.fs" />
<None Include="paket.references" />
</ItemGroup>
<ItemGroup>
Expand Down
91 changes: 91 additions & 0 deletions src/Bolero.Server/RazorHost.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// $begin{copyright}
//
// This file is part of Bolero
//
// Copyright (c) 2018 IntelliFactory and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you
// may not use this file except in compliance with the License. You may
// obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// $end{copyright}

namespace Bolero.Server.RazorHost

open System.Runtime.CompilerServices
open System.Threading.Tasks
open Microsoft.Extensions.Hosting
open Microsoft.AspNetCore.Mvc.Rendering
open Microsoft.AspNetCore.Components
open Microsoft.Extensions.DependencyInjection
open Microsoft.AspNetCore.Http

type IBoleroHostConfig =
abstract IsServer: bool
abstract IsPrerendered: bool

type IBoleroHostBaseConfig =
abstract IsServer: bool
abstract IsPrerendered: bool

type BoleroHostConfig(baseConfig: IBoleroHostBaseConfig, env: IHostEnvironment, ctx: IHttpContextAccessor) =

member val IsServer =
let mutable queryParam = Unchecked.defaultof<_>
let mutable parsed = false
if env.IsDevelopment()
&& ctx.HttpContext.Request.Query.TryGetValue("server", &queryParam)
&& bool.TryParse(queryParam.[0], &parsed)
then
parsed
else
baseConfig.IsServer

member _.IsPrerendered = baseConfig.IsPrerendered

interface IBoleroHostConfig with
member this.IsServer = this.IsServer
member this.IsPrerendered = this.IsPrerendered

[<Extension>]
type RazorHostingExtensions =

[<Extension>]
static member RenderComponentAsync<'T when 'T :> IComponent>(html: IHtmlHelper, config: IBoleroHostConfig) =
match config.IsServer, config.IsPrerendered with
| true, true -> html.RenderComponentAsync<'T>(RenderMode.ServerPrerendered)
| true, false -> html.RenderComponentAsync<'T>(RenderMode.Server)
| false, true -> html.RenderComponentAsync<'T>(RenderMode.Static)
| false, false -> Task.FromResult(null)


[<Extension>]
static member RenderBoleroScript(html: IHtmlHelper, config: IBoleroHostConfig) =
let k = if config.IsServer then "server" else "webassembly"
html.Raw(sprintf """<script src="_framework/blazor.%s.js"></script>""" k)

[<Extension>]
static member AddBoleroHost(this: IServiceCollection, ?server: bool, ?prerendered: bool, ?devToggle: bool) =
let server = defaultArg server false
let prerendered = defaultArg prerendered true
let devToggle = defaultArg devToggle true
if devToggle then
this.AddSingleton(
{ new IBoleroHostBaseConfig with
member _.IsServer = server
member _.IsPrerendered = prerendered })
.AddScoped<IBoleroHostConfig, BoleroHostConfig>()
.AddHttpContextAccessor()
else
this.AddSingleton(
{ new IBoleroHostConfig with
member _.IsServer = server
member _.IsPrerendered = prerendered })
40 changes: 0 additions & 40 deletions tests/Remoting.Server/HostModel.fs

This file was deleted.

8 changes: 4 additions & 4 deletions tests/Remoting.Server/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@page "/"
@namespace Bolero.Tests.Remoting
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@model HostModel
@using Bolero.Server.RazorHost
@inject IBoleroHostConfig BoleroHostConfig
<!DOCTYPE html>
<html>
<head>
Expand All @@ -10,8 +10,8 @@
<base href="/" />
</head>
<body>
<div id="main">@(await Html.RenderComponentAsync<Bolero.Tests.Remoting.Client.MyApp>(RenderMode.ServerPrerendered))</div>
<script src="_framework/blazor.@(Model.IsServer ? "server" : "webassembly").js"></script>
<div id="main">@(await Html.RenderComponentAsync<Bolero.Test.Remoting.Main.MyApp>(BoleroHostConfig))</div>
@Html.RenderBoleroScript(BoleroHostConfig)
</body>
</html>

1 change: 0 additions & 1 deletion tests/Remoting.Server/Remoting.Server.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<BlazorLinkOnBuild>false</BlazorLinkOnBuild>
</PropertyGroup>
<ItemGroup>
<Compile Include="HostModel.fs" />
<Compile Include="Startup.fs" />
<None Include="paket.references" />
</ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions tests/Remoting.Server/Startup.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ open Microsoft.Extensions.DependencyInjection
open Microsoft.Extensions.Hosting
open Microsoft.Extensions.Logging
open Bolero.Remoting.Server
open Bolero.Server.RazorHost

type MyApiHandler(log: ILogger<MyApiHandler>, ctx: IRemoteContext) =
inherit RemoteHandler<Client.MyApi>()
Expand Down Expand Up @@ -77,6 +78,7 @@ type Startup(config: IConfiguration) =
|> ignore
services
.AddRemoting<MyApiHandler>()
.AddBoleroHost()
.AddServerSideBlazor()
|> ignore

Expand Down
40 changes: 0 additions & 40 deletions tests/Server/HostModel.fs

This file was deleted.

9 changes: 4 additions & 5 deletions tests/Server/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
@page "/"
@namespace Bolero.Test.Server
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@model HostModel

@using Bolero.Server.RazorHost
@inject IBoleroHostConfig BoleroHostConfig
<!DOCTYPE html>
<html>
<head>
Expand All @@ -11,7 +10,7 @@
<base href="/" />
</head>
<body>
<div id="main">@(await Html.RenderComponentAsync<Bolero.Test.Client.Main.MyApp>(RenderMode.ServerPrerendered))</div>
<script src="_framework/blazor.@(Model.IsServer ? "server" : "webassembly").js"></script>
<div id="main">@(await Html.RenderComponentAsync<Bolero.Test.Client.Main.MyApp>(BoleroHostConfig))</div>
@Html.RenderBoleroScript(BoleroHostConfig)
</body>
</html>
2 changes: 1 addition & 1 deletion tests/Server/Server.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<BlazorLinkOnBuild>false</BlazorLinkOnBuild>
</PropertyGroup>
<ItemGroup>
<Compile Include="HostModel.fs" />
<Compile Include="Startup.fs" />
<None Include="paket.references" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Bolero.Server\Bolero.Server.fsproj" />
<ProjectReference Include="..\..\src\Bolero\Bolero.fsproj" />
<ProjectReference Include="..\Client\Client.fsproj" />
</ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions tests/Server/Startup.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ open Microsoft.AspNetCore
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.Extensions.DependencyInjection
open Bolero.Server.RazorHost
open Bolero.Test

type Startup() =

member this.ConfigureServices(services: IServiceCollection) =
services.AddMvc().AddRazorRuntimeCompilation() |> ignore
services.AddServerSideBlazor() |> ignore
services.AddBoleroHost() |> ignore

member this.Configure(app: IApplicationBuilder) =
app
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
@page "/"
@namespace Bolero.Tests.Web
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@using Bolero.Server.RazorHost
@inject IBoleroHostConfig BoleroHostConfig
<!DOCTYPE html>
<html>
<head>
<base href="/">
<meta charset="utf-8">
</head>
<body>
<div id="app">@(await Html.RenderComponentAsync<Bolero.Tests.Client.Tests>(RenderMode.ServerPrerendered))</div>
<div id="app">@(await Html.RenderComponentAsync<Bolero.Tests.Client.Tests>(BoleroHostConfig))</div>
<script>
// Used by ElementBinder test:
function setContent(element, value) {
element.innerHTML = value;
}
</script>
<script src="_framework/blazor.server.js"></script>
@Html.RenderBoleroScript(BoleroHostConfig)
</body>
</html>
3 changes: 2 additions & 1 deletion tests/Unit/Startup.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ open Microsoft.AspNetCore.Authentication.Cookies
open Microsoft.AspNetCore.Authorization
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Http
open Microsoft.Extensions.Configuration
open Microsoft.Extensions.DependencyInjection
open Bolero.Remoting.Server
open Bolero.Server.RazorHost
open Bolero.Tests

type Startup() =
Expand Down Expand Up @@ -76,6 +76,7 @@ type Startup() =
.AddCookie()
.Services
.AddRemoting(remoteHandler)
.AddBoleroHost()
.AddServerSideBlazor()
|> ignore

Expand Down