Coder Social home page Coder Social logo

c0g1t8 / blazor-page-script Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mackinnonbuck/blazor-page-script

0.0 0.0 0.0 217 KB

A Blazor component enabling per-page JavaScript initialization logic in statically rendered Blazor Web apps

JavaScript 95.04% HTML 4.96%

blazor-page-script's Introduction

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" />

blazor-page-script's People

Contributors

mackinnonbuck avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.