Skip to content
This repository was archived by the owner on Feb 25, 2021. It is now read-only.

[WIP] Prerendering #785

Closed
wants to merge 11 commits into from
Closed
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
36 changes: 36 additions & 0 deletions Blazor.sln
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorHosted-CSharp.Server"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorHosted-CSharp.Shared", "src\Microsoft.AspNetCore.Blazor.Templates\content\BlazorHosted-CSharp\BlazorHosted-CSharp.Shared\BlazorHosted-CSharp.Shared.csproj", "{F3E02B21-1127-431A-B832-0E53CB72097B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Prerendering", "Prerendering", "{0A6A1E7D-2236-4695-A5B1-0A82EEED0B6C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Blazor.Server.Rendering", "src\Microsoft.AspNetCore.Blazor.Server.Rendering\Microsoft.AspNetCore.Blazor.Server.Rendering.csproj", "{9C424433-8AA6-452B-BA9E-01ACD2852176}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrerenderingApp.Client", "samples\PrerenderingApp.Client\PrerenderingApp.Client.csproj", "{034F0B11-DBA2-44A9-932E-BFCD94DB5F7D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrerenderingApp.Server", "samples\PrerenderingApp.Server\PrerenderingApp.Server.csproj", "{A61788D9-3764-4BC5-A5BB-118D4A10BBEB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Blazor.Razor.Extensions.Test", "test\Microsoft.AspNetCore.Blazor.Razor.Extensions.Test\Microsoft.AspNetCore.Blazor.Razor.Extensions.Test.csproj", "{FF25111E-5A3E-48A3-96D8-08A2C5A2A91C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tooling", "tooling", "{F563ABB6-85FB-4CFC-B0D2-1D5130E8246D}"
Expand Down Expand Up @@ -292,6 +300,30 @@ Global
{F3E02B21-1127-431A-B832-0E53CB72097B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3E02B21-1127-431A-B832-0E53CB72097B}.Release|Any CPU.Build.0 = Release|Any CPU
{F3E02B21-1127-431A-B832-0E53CB72097B}.ReleaseNoVSIX|Any CPU.ActiveCfg = Release|Any CPU
{9C424433-8AA6-452B-BA9E-01ACD2852176}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9C424433-8AA6-452B-BA9E-01ACD2852176}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C424433-8AA6-452B-BA9E-01ACD2852176}.DebugNoVSIX|Any CPU.ActiveCfg = Debug|Any CPU
{9C424433-8AA6-452B-BA9E-01ACD2852176}.DebugNoVSIX|Any CPU.Build.0 = Debug|Any CPU
{9C424433-8AA6-452B-BA9E-01ACD2852176}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C424433-8AA6-452B-BA9E-01ACD2852176}.Release|Any CPU.Build.0 = Release|Any CPU
{9C424433-8AA6-452B-BA9E-01ACD2852176}.ReleaseNoVSIX|Any CPU.ActiveCfg = Release|Any CPU
{9C424433-8AA6-452B-BA9E-01ACD2852176}.ReleaseNoVSIX|Any CPU.Build.0 = Release|Any CPU
{034F0B11-DBA2-44A9-932E-BFCD94DB5F7D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{034F0B11-DBA2-44A9-932E-BFCD94DB5F7D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{034F0B11-DBA2-44A9-932E-BFCD94DB5F7D}.DebugNoVSIX|Any CPU.ActiveCfg = Debug|Any CPU
{034F0B11-DBA2-44A9-932E-BFCD94DB5F7D}.DebugNoVSIX|Any CPU.Build.0 = Debug|Any CPU
{034F0B11-DBA2-44A9-932E-BFCD94DB5F7D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{034F0B11-DBA2-44A9-932E-BFCD94DB5F7D}.Release|Any CPU.Build.0 = Release|Any CPU
{034F0B11-DBA2-44A9-932E-BFCD94DB5F7D}.ReleaseNoVSIX|Any CPU.ActiveCfg = Release|Any CPU
{034F0B11-DBA2-44A9-932E-BFCD94DB5F7D}.ReleaseNoVSIX|Any CPU.Build.0 = Release|Any CPU
{A61788D9-3764-4BC5-A5BB-118D4A10BBEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A61788D9-3764-4BC5-A5BB-118D4A10BBEB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A61788D9-3764-4BC5-A5BB-118D4A10BBEB}.DebugNoVSIX|Any CPU.ActiveCfg = Debug|Any CPU
{A61788D9-3764-4BC5-A5BB-118D4A10BBEB}.DebugNoVSIX|Any CPU.Build.0 = Debug|Any CPU
{A61788D9-3764-4BC5-A5BB-118D4A10BBEB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A61788D9-3764-4BC5-A5BB-118D4A10BBEB}.Release|Any CPU.Build.0 = Release|Any CPU
{A61788D9-3764-4BC5-A5BB-118D4A10BBEB}.ReleaseNoVSIX|Any CPU.ActiveCfg = Release|Any CPU
{A61788D9-3764-4BC5-A5BB-118D4A10BBEB}.ReleaseNoVSIX|Any CPU.Build.0 = Release|Any CPU
{FF25111E-5A3E-48A3-96D8-08A2C5A2A91C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FF25111E-5A3E-48A3-96D8-08A2C5A2A91C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FF25111E-5A3E-48A3-96D8-08A2C5A2A91C}.DebugNoVSIX|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -386,6 +418,10 @@ Global
{7549444A-9C81-44DE-AD0D-2C55501EAAC7} = {73DA1DFD-79F0-4BA2-B0B6-4F3A21D2C3F8}
{78ED9932-0912-4F36-8F82-33DE850E7A33} = {73DA1DFD-79F0-4BA2-B0B6-4F3A21D2C3F8}
{F3E02B21-1127-431A-B832-0E53CB72097B} = {73DA1DFD-79F0-4BA2-B0B6-4F3A21D2C3F8}
{0A6A1E7D-2236-4695-A5B1-0A82EEED0B6C} = {F5FDD4E5-6A52-4A86-BE5E-5E42CB1DC8DA}
{9C424433-8AA6-452B-BA9E-01ACD2852176} = {B867E038-B3CE-43E3-9292-61568C46CDEB}
{034F0B11-DBA2-44A9-932E-BFCD94DB5F7D} = {0A6A1E7D-2236-4695-A5B1-0A82EEED0B6C}
{A61788D9-3764-4BC5-A5BB-118D4A10BBEB} = {0A6A1E7D-2236-4695-A5B1-0A82EEED0B6C}
{FF25111E-5A3E-48A3-96D8-08A2C5A2A91C} = {ADA3AE29-F6DE-49F6-8C7C-B321508CAE8E}
{43E39257-7DC1-46BD-9BD9-2319A1313D07} = {F563ABB6-85FB-4CFC-B0D2-1D5130E8246D}
{9088E4E4-B855-457F-AE9E-D86709A5E1F4} = {F563ABB6-85FB-4CFC-B0D2-1D5130E8246D}
Expand Down
22 changes: 22 additions & 0 deletions samples/PrerenderingApp.Client/ClientWeatherService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Blazor;

namespace PrerenderingApp.Client
{
public class ClientWeatherService : IWeatherService
{
private readonly HttpClient _httpClient;

public ClientWeatherService(HttpClient httpClient)
{
_httpClient = httpClient;
}

public async Task<IEnumerable<WeatherForecast>> GetForecast()
{
return await _httpClient.GetJsonAsync<WeatherForecast[]>("/api/SampleData/WeatherForecasts");
}
}
}
14 changes: 14 additions & 0 deletions samples/PrerenderingApp.Client/ConsumeService.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@inject Greeter Greeter

<div>
<h2>@Greeter.Greet(name)</h2>
<div>
<input type="text" bind=@name placeholder="enter name"/>
</div>
</div>

@functions {

string name = null;

}
43 changes: 43 additions & 0 deletions samples/PrerenderingApp.Client/FetchData.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
@inject IWeatherService WeatherService

<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from the server.</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>
}

@functions {
WeatherForecast[] forecasts;

protected override async Task OnInitAsync()
{
forecasts = (await WeatherService.GetForecast()).ToArray();
}
}
10 changes: 10 additions & 0 deletions samples/PrerenderingApp.Client/Greeter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace PrerenderingApp.Client
{
public class Greeter
{
public string Greet(string name)
{
return $"Hello {name}";
}
}
}
28 changes: 28 additions & 0 deletions samples/PrerenderingApp.Client/Home.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<h1>Prerendering Sample</h1>

<h3>Input component</h3>
<MyInput />

<h3>List component</h3>
<MyList />

<h3>Input element</h3>
<input type="text" />

<div>
<h3>Bound inputs</h3>
<input type="text" bind=@test />
<input type="text" bind=@test />
</div>

<h3>Consume same service on client and server</h3>
<ConsumeService />

<h3>Consume different service on client and server</h3>
<FetchData />

@functions {

string test = "hello";

}
10 changes: 10 additions & 0 deletions samples/PrerenderingApp.Client/IWeatherService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace PrerenderingApp.Client
{
public interface IWeatherService
{
Task<IEnumerable<WeatherForecast>> GetForecast();
}
}
1 change: 1 addition & 0 deletions samples/PrerenderingApp.Client/MyInput.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<input type="text" value="component" />
16 changes: 16 additions & 0 deletions samples/PrerenderingApp.Client/MyList.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@foreach (var item in items)
{
<div>@item</div>
}

@functions {

int[] items = new[] {1, 2, 3, 4};

protected override void OnInit()
{
base.OnInit();

items = new[] {6, 7, 8, 9};
}
}
19 changes: 19 additions & 0 deletions samples/PrerenderingApp.Client/PrerenderingApp.Client.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<OutputType>Exe</OutputType>

<!-- Temporary workaround for a VS build issue -->
<BlazorRebuildOnFileChange>false</BlazorRebuildOnFileChange>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Blazor.Browser\Microsoft.AspNetCore.Blazor.Browser.csproj" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Blazor\Microsoft.AspNetCore.Blazor.csproj" />
</ItemGroup>

<!-- Local alternative to <PackageReference Include="Microsoft.AspNetCore.Blazor.Build" /> -->
<Import Project="..\..\src\Microsoft.AspNetCore.Blazor.Build\ReferenceFromSource.props" />

</Project>
23 changes: 23 additions & 0 deletions samples/PrerenderingApp.Client/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.AspNetCore.Blazor.Browser.Rendering;
using Microsoft.AspNetCore.Blazor.Browser.Services;
using Microsoft.Extensions.DependencyInjection;

namespace PrerenderingApp.Client
{
public class Program
{
static void Main(string[] args)
{
var serviceProvider = new BrowserServiceProvider(configure =>
{
configure.AddSharedServices();
configure.AddSingleton<IWeatherService, ClientWeatherService>();
});

new BrowserRenderer(serviceProvider).AddComponent<Home>("app");
}
}
}
12 changes: 12 additions & 0 deletions samples/PrerenderingApp.Client/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.Extensions.DependencyInjection;

namespace PrerenderingApp.Client
{
public static class ServiceCollectionExtensions
{
public static void AddSharedServices(this IServiceCollection configure)
{
configure.AddSingleton<Greeter>();
}
}
}
12 changes: 12 additions & 0 deletions samples/PrerenderingApp.Client/WeatherForecast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace PrerenderingApp.Client
{
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public string Summary { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This is only used for E2E tests to verify that we're correctly serving static content.
// Later this will be replaced with something more useful.
window.customJsWasLoaded = true;
12 changes: 12 additions & 0 deletions samples/PrerenderingApp.Client/wwwroot/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Sample Blazor app</title>
</head>
<body>
<app>Loading...</app>
<script src="customJsFileForTests.js"></script>
<script type="blazor-boot"></script>
</body>
</html>
24 changes: 24 additions & 0 deletions samples/PrerenderingApp.Server/Controller/SampleDataController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using PrerenderingApp.Client;

namespace PrerenderingApp.Server.Controller
{
[Route("api/[controller]")]
public class SampleDataController : Microsoft.AspNetCore.Mvc.Controller
{
private readonly IWeatherService _weatherService;

public SampleDataController(IWeatherService weatherService)
{
_weatherService = weatherService;
}

[HttpGet("[action]")]
public async Task<IEnumerable<WeatherForecast>> WeatherForecasts()
{
return await _weatherService.GetForecast();
}
}
}
18 changes: 18 additions & 0 deletions samples/PrerenderingApp.Server/PrerenderingApp.Server.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Blazor.Server.Rendering\Microsoft.AspNetCore.Blazor.Server.Rendering.csproj" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Blazor.Server\Microsoft.AspNetCore.Blazor.Server.csproj" />
<ProjectReference Include="..\PrerenderingApp.Client\PrerenderingApp.Client.csproj" />
</ItemGroup>

</Project>
38 changes: 38 additions & 0 deletions samples/PrerenderingApp.Server/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Reflection;
using System.Runtime.Loader;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;

namespace PrerenderingApp.Server
{
public class Program
{
public static void Main(string[] args)
{
AssemblyLoadContext.Default.Resolving += Default_Resolving;
BuildWebHost(args).Run();
}

private static Assembly Default_Resolving(AssemblyLoadContext context, AssemblyName assemblyName)
{
var location = Assembly.GetEntryAssembly().Location;
var directoryName = Path.GetDirectoryName(location);
var assemblyPath = Path.Combine(directoryName, assemblyName.Name + ".dll");
return context.LoadFromAssemblyPath(assemblyPath);
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseConfiguration(new ConfigurationBuilder()
.AddCommandLine(args)
.Build())
.UseStartup<Startup>()
.Build();
}
}
Loading