Skip to content

Commit

Permalink
fix: server side rendering (#250)
Browse files Browse the repository at this point in the history
* Fixes SSR exception
* Add .NET 6 samples
  • Loading branch information
IvanJosipovic committed Dec 1, 2023
1 parent bf47f72 commit 0a3b480
Show file tree
Hide file tree
Showing 71 changed files with 2,791 additions and 32 deletions.
35 changes: 27 additions & 8 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ on:

name: Create Release
jobs:
build:
name: Create Release
test:
name: Run Tests
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
matrix:
path: [samples/BlazorApplicationInsights.Sample.Wasm, samples/BlazorApplicationInsights.Sample.Wasm6]
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand All @@ -27,12 +29,8 @@ jobs:
- name: Dotnet Build
run: dotnet build -c Release

- name: Dotnet Publish
working-directory: samples/BlazorApplicationInsights.Sample.Wasm
run: dotnet publish -c Release

- name: Run App
working-directory: samples/BlazorApplicationInsights.Sample.Wasm
working-directory: ${{ matrix.path }}
run: Start-Process dotnet 'run','-c','Release','--launch-profile','https' -PassThru
shell: pwsh

Expand All @@ -45,6 +43,27 @@ jobs:
working-directory: tests/BlazorApplicationInsights.Tests
run: dotnet test -c Release

build:
name: Create Release
needs: [test]
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup .NET Core
uses: actions/setup-dotnet@v3
with:
global-json-file: global.json

- name: Dotnet Build
run: dotnet build -c Release

- name: Dotnet Publish
working-directory: samples/BlazorApplicationInsights.Sample.Wasm
run: dotnet publish -c Release

- name: Deploy
run: npx netlify deploy ${{ (github.ref == 'refs/heads/master' && '--prod') || '' }} --json -d samples/BlazorApplicationInsights.Sample.Wasm/bin/Release/net8.0/publish/wwwroot
env:
Expand Down
18 changes: 16 additions & 2 deletions BlazorApplicationInsights.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{D6AB13B2
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApplicationInsights.Sample.Wasm", "samples\BlazorApplicationInsights.Sample.Wasm\BlazorApplicationInsights.Sample.Wasm.csproj", "{C8F5829A-B2F4-46C6-9749-2A7BB3565F25}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorApplicationInsights.Sample.WebApp", "samples\BlazorApplicationInsights.Sample.WebApp\BlazorApplicationInsights.Sample.WebApp\BlazorApplicationInsights.Sample.WebApp.csproj", "{E3782F8D-0C4F-4161-B816-C432865639AE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApplicationInsights.Sample.WebApp", "samples\BlazorApplicationInsights.Sample.WebApp\BlazorApplicationInsights.Sample.WebApp\BlazorApplicationInsights.Sample.WebApp.csproj", "{E3782F8D-0C4F-4161-B816-C432865639AE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorApplicationInsights.Sample.WebApp.Client", "samples\BlazorApplicationInsights.Sample.WebApp\BlazorApplicationInsights.Sample.WebApp.Client\BlazorApplicationInsights.Sample.WebApp.Client.csproj", "{A73F04FB-FCDD-43B7-AC8E-1595E8E21CA7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApplicationInsights.Sample.WebApp.Client", "samples\BlazorApplicationInsights.Sample.WebApp\BlazorApplicationInsights.Sample.WebApp.Client\BlazorApplicationInsights.Sample.WebApp.Client.csproj", "{A73F04FB-FCDD-43B7-AC8E-1595E8E21CA7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApplicationInsights.Sample.Server", "samples\BlazorApplicationInsights.Sample.Server\BlazorApplicationInsights.Sample.Server.csproj", "{7F20811A-451A-46D5-B47A-FF45FEB3299E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorApplicationInsights.Sample.Wasm6", "samples\BlazorApplicationInsights.Sample.Wasm6\BlazorApplicationInsights.Sample.Wasm6.csproj", "{998E59D1-3046-4BCF-A90A-72BCFEAD3919}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -60,6 +64,14 @@ Global
{A73F04FB-FCDD-43B7-AC8E-1595E8E21CA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A73F04FB-FCDD-43B7-AC8E-1595E8E21CA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A73F04FB-FCDD-43B7-AC8E-1595E8E21CA7}.Release|Any CPU.Build.0 = Release|Any CPU
{7F20811A-451A-46D5-B47A-FF45FEB3299E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7F20811A-451A-46D5-B47A-FF45FEB3299E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7F20811A-451A-46D5-B47A-FF45FEB3299E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7F20811A-451A-46D5-B47A-FF45FEB3299E}.Release|Any CPU.Build.0 = Release|Any CPU
{998E59D1-3046-4BCF-A90A-72BCFEAD3919}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{998E59D1-3046-4BCF-A90A-72BCFEAD3919}.Debug|Any CPU.Build.0 = Debug|Any CPU
{998E59D1-3046-4BCF-A90A-72BCFEAD3919}.Release|Any CPU.ActiveCfg = Release|Any CPU
{998E59D1-3046-4BCF-A90A-72BCFEAD3919}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -71,6 +83,8 @@ Global
{C8F5829A-B2F4-46C6-9749-2A7BB3565F25} = {8E51F70D-BCC4-4A78-85F2-79D639EED440}
{E3782F8D-0C4F-4161-B816-C432865639AE} = {8E51F70D-BCC4-4A78-85F2-79D639EED440}
{A73F04FB-FCDD-43B7-AC8E-1595E8E21CA7} = {8E51F70D-BCC4-4A78-85F2-79D639EED440}
{7F20811A-451A-46D5-B47A-FF45FEB3299E} = {8E51F70D-BCC4-4A78-85F2-79D639EED440}
{998E59D1-3046-4BCF-A90A-72BCFEAD3919} = {8E51F70D-BCC4-4A78-85F2-79D639EED440}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3EBB1AF9-73E1-4D46-B0F7-C0E8B8E2173D}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand All @@ -11,7 +11,18 @@
<SupportedPlatform Include="browser" />
</ItemGroup>

<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.AspNetCore.Components" Version="6.0.*" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="6.0.*" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.*" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.AspNetCore.Components" Version="7.0.*" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="7.0.*" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.AspNetCore.Components" Version="8.0.*" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="8.0.*" />
</ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions samples/BlazorApplicationInsights.Sample.Server/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\BlazorApplicationInsights\BlazorApplicationInsights.csproj" />
<ProjectReference Include="..\BlazorApplicationInsights.Sample.Components\BlazorApplicationInsights.Sample.Components.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace BlazorApplicationInsights.Sample.Server.Data
{
public class WeatherForecast
{
public DateTime Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string? Summary { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace BlazorApplicationInsights.Sample.Server.Data
{
public class WeatherForecastService
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
{
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = startDate.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
}).ToArray());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@page "/counter"

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
42 changes: 42 additions & 0 deletions samples/BlazorApplicationInsights.Sample.Server/Pages/Error.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@page
@model BlazorApplicationInsights.Sample.Server.Pages.ErrorModel

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Error</title>
<link href="~/css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="~/css/site.css" rel="stylesheet" asp-append-version="true" />
</head>

<body>
<div class="main">
<div class="content px-4">
<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (Model.ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@Model.RequestId</code>
</p>
}

<h3>Development Mode</h3>
<p>
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>
</div>
</div>
</body>

</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Diagnostics;

namespace BlazorApplicationInsights.Sample.Server.Pages
{
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken]
public class ErrorModel : PageModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

private readonly ILogger<ErrorModel> _logger;

public ErrorModel(ILogger<ErrorModel> logger)
{
_logger = logger;
}

public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
@page "/fetchdata"

<PageTitle>Weather forecast</PageTitle>

@using BlazorApplicationInsights.Sample.Server.Data
@inject WeatherForecastService ForecastService

<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from a service.</p>

@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}

@code {
private WeatherForecast[]? forecasts;

protected override async Task OnInitializedAsync()
{
forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@page "/"
@using BlazorApplicationInsights.Sample.Components

<PageTitle>BlazorApplicationInsights</PageTitle>

<TestComponents/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@page "/"
@namespace BlazorApplicationInsights.Sample.Server.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = "_Layout";
}

<component type="typeof(App)" render-mode="ServerPrerendered" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@using Microsoft.AspNetCore.Components.Web
@namespace BlazorApplicationInsights.Sample.Server.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<base href="~/" />
<component type="typeof(ApplicationInsightsInit)" render-mode="ServerPrerendered" />
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
<link href="css/site.css" rel="stylesheet" />
<link href="BlazorApplicationInsights.Sample.Server.styles.css" rel="stylesheet" />
<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
</head>
<body>
@RenderBody()

<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>

<script src="_framework/blazor.server.js"></script>
</body>
</html>
Loading

0 comments on commit 0a3b480

Please sign in to comment.