This library allows developers to opt-in to using Navigation Preload in their service worker.
npx @tessl/cli install tessl/npm-workbox-navigation-preload@7.3.0Workbox Navigation Preload provides a simple interface for enabling and disabling Navigation Preload functionality in service workers. Navigation Preload is a browser optimization that allows network requests to be started in parallel with service worker activation, reducing the time between navigation startup and network response.
npm install workbox-navigation-preloadworkbox-core@7.3.0import { enable, disable, isSupported } from "workbox-navigation-preload";For CommonJS:
const { enable, disable, isSupported } = require("workbox-navigation-preload");import { enable, disable, isSupported } from "workbox-navigation-preload";
// Check if Navigation Preload is supported
if (isSupported()) {
// Enable Navigation Preload with default header
enable();
// Or enable with custom header value
enable("custom-preload-header");
// Disable Navigation Preload
disable();
} else {
console.log("Navigation Preload not supported in this browser");
}Workbox Navigation Preload uses an event-driven architecture that integrates with the Service Worker lifecycle:
activate event listeners on the service workerevent.waitUntil() to extend the activation lifecycle until operations completeworkbox-core logger utilitiesChecks whether Navigation Preload is supported in the current browser environment.
/**
* Checks if Navigation Preload is supported in the current browser
* @returns true if supported, false otherwise
*/
function isSupported(): boolean;Usage Example:
import { isSupported } from "workbox-navigation-preload";
if (isSupported()) {
console.log("Navigation Preload is available");
} else {
console.log("Navigation Preload is not supported");
}Enables Navigation Preload functionality with optional custom header configuration.
/**
* Enables Navigation Preload if supported by the browser
* @param headerValue - Optional custom value for the Service-Worker-Navigation-Preload header
*/
function enable(headerValue?: string): void;Usage Examples:
import { enable } from "workbox-navigation-preload";
// Enable with default header (Service-Worker-Navigation-Preload: true)
enable();
// Enable with custom header value
enable("my-custom-preload-value");Behavior:
isSupported() before proceedingevent.waitUntil() to extend the activation event lifecycle until completionself.registration.navigationPreload.enable()self.registration.navigationPreload.setHeaderValue()workbox-core/loggerDisables Navigation Preload functionality.
/**
* Disables Navigation Preload if supported by the browser
*/
function disable(): void;Usage Example:
import { disable } from "workbox-navigation-preload";
// Disable Navigation Preload
disable();Behavior:
isSupported() before proceedingevent.waitUntil() to extend the activation event lifecycle until completionself.registration.navigationPreload.disable()workbox-core/logger/**
* Represents the current state of Navigation Preload
*/
interface NavigationPreloadState {
enabled?: boolean;
headerValue?: string;
}
/**
* Interface for managing Navigation Preload functionality
*/
interface NavigationPreloadManager {
disable(): Promise<void>;
enable(): Promise<void>;
getState(): Promise<NavigationPreloadState>;
setHeaderValue(value: string): Promise<void>;
}Global Type Extension:
declare global {
interface ServiceWorkerRegistration {
readonly navigationPreload: NavigationPreloadManager;
}
}
/**
* Service Worker global scope interface
*/
interface ServiceWorkerGlobalScope extends WorkerGlobalScope {
registration: ServiceWorkerRegistration;
addEventListener(type: 'activate', listener: (event: ExtendableEvent) => void): void;
}
/**
* Extendable event interface for service worker lifecycle events
*/
interface ExtendableEvent extends Event {
waitUntil(f: Promise<any>): void;
}This library is designed to run in a Service Worker context and requires:
self global scope (ServiceWorkerGlobalScope)self.registration propertyself.registration.navigationPreload API supportThe library handles unsupported browsers gracefully:
isSupported() before attempting operationsprocess.env.NODE_ENV checks