BlazorPrerenderHelper 1.2.1

dotnet add package BlazorPrerenderHelper --version 1.2.1
NuGet\Install-Package BlazorPrerenderHelper -Version 1.2.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BlazorPrerenderHelper" Version="1.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BlazorPrerenderHelper --version 1.2.1
#r "nuget: BlazorPrerenderHelper, 1.2.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install BlazorPrerenderHelper as a Cake Addin
#addin nuget:?package=BlazorPrerenderHelper&version=1.2.1

// Install BlazorPrerenderHelper as a Cake Tool
#tool nuget:?package=BlazorPrerenderHelper&version=1.2.1

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.1 213 4/26/2023
1.2.0 152 4/26/2023
1.1.0 218 3/26/2023
1.0.1 220 3/19/2023
1.0.0 211 3/19/2023