BlazorPageScript 1.0.0

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

// Install BlazorPageScript as a Cake Tool
#tool nuget:?package=BlazorPageScript&version=1.0.0

BlazorPageScript

A Blazor <PageScript> component enabling per-page JavaScript navigation callbacks.

This allows Blazor Web applications to have page-specific JavaScript that works with enhanced navigation enabled or disabled.

Basic usage

Example.razor

@page "/example"

<PageTitle>Example</PageTitle>

<PageScript Src="./Example.razor.js" />

Welcome to the example page.

Example.razor.js

// Called when the script first gets loaded on the page.
export function onLoad() {
    console.log('Load');
}

// Called when an enhanced page update occurs, plus once immediately after
// the initial load.
export function onUpdate() {
    console.log('Update');
}

// Called when an enhanced page update removes the script from the page.
export function onDispose() {
    console.log('Dispose');
}

Take a look at the samples folder in this repository for more usage examples.

Expected usage patterns

A <PageScript> in a @page component

This is expected to be the most common use case. This allows pages to put initialization logic in onLoad and cleanup logic in onDispose, while reacting to enhanced page updates in onUpdate.

A <PageScript> in the application's layout

A <PageScript> placed in the app's layout will get its onLoad callback invoked once per full page load and the onUpdate callback invoked once per enhanced page update.

Other notes

Duplicating <PageScript> components with the same Src won't cause duplicate callback invocations. Likewise, if multiple pages render <PageScript> components with the same Src, then an enhanced navigation between those pages won't invoke the onDispose and onLoad callbacks.

If you want to override this behavior, you can deduplicate two <PageScript> components by giving them unique querystring values in their Src parameters. For example:

Home.razor

<PageScript Src="./common-logic.js?home" />

Counter.razor

<PageScript Src="./common-logic.js?counter" />
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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 (2)

Showing the top 2 NuGet packages that depend on BlazorPageScript:

Package Downloads
LEC.Blazor.Recaptcha

Package Description

LEC.Blazor.Localization.Client

Package Description

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on BlazorPageScript:

Repository Stars
masastack/MASA.Blazor
Blazor UI component library based on Material Design. Support Blazor Server, Blazor WebAssembly and MAUI Blazor.
Version Downloads Last updated
1.0.0 8,786 11/16/2023
0.1.0-preview.2 254 11/3/2023
0.1.0-preview.1 62 11/3/2023