Bymse.AspNetCore.NextJsStaticHosting 0.0.3

dotnet add package Bymse.AspNetCore.NextJsStaticHosting --version 0.0.3
NuGet\Install-Package Bymse.AspNetCore.NextJsStaticHosting -Version 0.0.3
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="Bymse.AspNetCore.NextJsStaticHosting" Version="0.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Bymse.AspNetCore.NextJsStaticHosting --version 0.0.3
#r "nuget: Bymse.AspNetCore.NextJsStaticHosting, 0.0.3"
#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 Bymse.AspNetCore.NextJsStaticHosting as a Cake Addin
#addin nuget:?package=Bymse.AspNetCore.NextJsStaticHosting&version=0.0.3

// Install Bymse.AspNetCore.NextJsStaticHosting as a Cake Tool
#tool nuget:?package=Bymse.AspNetCore.NextJsStaticHosting&version=0.0.3

NuGet Gallery | Bymse.AspNetCore.NextJsStaticHosting

Bymse.AspNetCore.NextJsStaticHosting

Library that enables you to host statically exported Next.js application within ASP.NET Core application.

Features

  • Next.js pages hosting. Serves static html pages exported from Next.js application. Includes support for dynamic routes.
  • Next.js static assets hosting. Serves static assets exported from Next.js application.
  • Arbitrary sources of files. Allows to serve files from any source, including embedded resources, physical files, etc.
  • Simple chunks version skew protection. Allows to serve files from multiple versions of Next.js application.

Usage

next build && next export can be used to build and export Next.js application.

  1. Install Bymse.AspNetCore.NextJsStaticHosting NuGet package.
  2. Add the following code to Startup.cs file:
public void Configure(IApplicationBuilder app)
{
        app.UseRouting();

        var latestVersion = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "out", "latest")); 
        var previousDeployment = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "out", "previous")); // optional
        
        app.UseEndpoints(b => b.MapNextJsStaticEndpoints(new NextJsStaticEndpointsOptions(latestVersion)));

        app.UseNextJsStaticFiles(new NextJsStaticFilesOptions
        {
            FileProvider = new CompositeFileProvider(
                latestVersion,
                previousDeployment
            )
        });
}
  1. Or for minimal API:
using AspNetCore.NextJsStaticHosting;
using Microsoft.Extensions.FileProviders;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

var latestVersion = new PhysicalFileProvider(Path.Combine(app.Environment.ContentRootPath, "out", "latest")); 
var previousDeployment = new PhysicalFileProvider(Path.Combine(app.Environment.ContentRootPath, "out", "previous")); // optional

app.MapNextJsStaticEndpoints(new NextJsStaticEndpointsOptions(latestVersion));

app.UseNextJsStaticFiles(new NextJsStaticFilesOptions
{
    FileProvider = new CompositeFileProvider(
        latestVersion,
        previousDeployment
    )
});

app.Run();

Example use case can be found in AspNetCore.NextJsStaticHosting.Tests folder.

Chunks version skew protection

Chunks version skew in a Single Page Application (SPA) refers to the situation where an application opened in a browser tries to fetch a chunk for the next page, but the chunk is not available on the server because the server was updated with a new version of the application. This situation is not handled by default by Next.js. This library provides a simple mechanism to handle this situation.

In `Bymse.AspNetCore.NextJsStaticHosting`` it is possible to provide multiple versions of Next.js static build as input for serving. It would follow the convention:

  1. Html pages only from latest version of Next.js application are served. It allows to update in-browser version on refresh and prevents deleted pages from being available.
  2. Static files are served from all available versions, but files from the latest version will have priority.

This way opened version can access old chunks and after refresh user will get new version of application.

References

NextjsStaticHosting-AspNetCore was used as a reference for endpoint map building. Thanks to the author https://github.com/davidnx.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  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.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.

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
0.0.3 1,227 10/24/2023
0.0.2 309 10/24/2023
0.0.1 337 10/22/2023