BlazorPrerenderHelper 1.1.0
See the version list below for details.
dotnet add package BlazorPrerenderHelper --version 1.1.0
NuGet\Install-Package BlazorPrerenderHelper -Version 1.1.0
<PackageReference Include="BlazorPrerenderHelper" Version="1.1.0" />
paket add BlazorPrerenderHelper --version 1.1.0
#r "nuget: BlazorPrerenderHelper, 1.1.0"
// Install BlazorPrerenderHelper as a Cake Addin #addin nuget:?package=BlazorPrerenderHelper&version=1.1.0 // Install BlazorPrerenderHelper as a Cake Tool #tool nuget:?package=BlazorPrerenderHelper&version=1.1.0
BlazorPrerenderHelper
This library provides a handful of helper functions for eliminating double data retrieval problem of WASM prerendered application.
Quick start
Add the following line before </body>
in your _Host.cshtml
<script src="_content/BlazorPrerenderHelper/scripts.js"></script>
@inject IPrerenderScriptGenerator PrerenderScriptGenerator
@Html.Raw(PrerenderScriptGenerator.Generate())
Add the following line in both of your client and server Program.cs
or Startup.cs
for client:
builder.Services.AddPrerenderHelperForClient();
for server:
builder.Services.AddPrerenderHelperForServer();
Now, you may use the following example to eliminate double data retrieval problem.
IPersonRepository.cs
(Shared)
public interface IPersonRepository
{
Task<Person> GetPerson(int id);
}
PersonServerRepository.cs
(Server)
public class PersonServerRepository : IPersonRepository
{
public async Task<Person> GetPerson(int id)
{
return _dbContext.People.SingleOrDefault(person => person.Id == id);
}
}
PersonController.cs
(Server)
[HttpGet]
[Route("api/person/{id}")]
public async Task<IActionResult> GetPerson(int id)
{
var person = await _personRepo.GetPerson(id);
return Ok(person);
}
PersonClientRepository.cs
(Client)
public class PersonClientRepository : IPersonRepository
{
private readonly ISSRService _ssr;
public async Task<Person> GetPerson(int id)
{
var ssrHint = await SSR.GetHint("person"); // this is instant return, because hint is retrieved from JS, which is generated during prerendering
if (ssrHint.IsFound)
return ssrHint.Result;
// hint not found, call API to retrieve data as normal
return await httpClient.GetFromJsonAsync("api/person/1");
}
}
Person.razor
(Client)
@page "/"
@inject IPersonRepository PersonRepo
<div>Hello, @person.Name</div>
@*// The following code will generate JS that can be retrieved by `GetHint` method. In client context, the component is just a dummy component *@
<SSRHint Key="person" Value="_person" />
@code {
private readonly Person? _person;
protected override async Task OnInitializedAsync()
{
_person = PersonRepo.GetPerson(1);
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- Microsoft.AspNetCore.Components.Web (>= 7.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.