Namespace: SpawnDev.SpawnJS.JSObjects
Inherits: SpawnJSObject
Source: JSObjects/NavigationPreloadManager.cs
MDN Reference: NavigationPreloadManager on MDN
The NavigationPreloadManager interface of the Service Worker API provides methods for managing the preloading of resources in parallel with service worker bootup. If supported, an object of this type is returned by ServiceWorkerRegistration.navigationPreload. The result of a preload fetch request is waited on using the promise returned by FetchEvent.preloadResponse. https://developer.mozilla.org/en-US/docs/Web/API/NavigationPreloadManager
| Method | Return Type | Description |
|---|---|---|
Enable() |
Task |
Enables navigation preloading, returning a Promise that resolves with undefined. |
SetHeaderValue(string value) |
Task |
Sets the value of the Service-Worker-Navigation-Preload HTTP header sent in preloading requests and returns an empty Promise. An arbitrary string value, which the target server uses to determine what should returned for the requested resource. |
GetState() |
Task<NavigationPreloadState> |
Returns a Promise that resolves to an object with properties that indicate whether preloading is enabled, and what value will be sent in the Service-Worker-Navigation-Preload HTTP header in preloading requests. |
Disable() |
Task |
Disables navigation preloading, returning a Promise that resolves with undefined. |
SpawnDev.SpawnJS Mapping Note: The JavaScript examples below show the standard Web API usage. In SpawnDev.SpawnJS, the same API is available with C# conventions:
- Properties and methods use PascalCase (e.g.,
readyStatebecomesReadyState)- Events use ActionEvent with
+=/-=(e.g.,addEventListener("click", fn)becomesOnClick += handler)- Async methods return
Task<T>instead ofPromise<T>- Objects should be disposed with
usingstatements- Access via
JS.Get<NavigationPreloadManager>(...)or constructornew NavigationPreloadManager(...)
addEventListener("activate", (event) => {
event.waitUntil(
(async () => {
if (self.registration.navigationPreload) {
// Enable navigation preloads!
await self.registration.navigationPreload.enable();
}
})(),
);
});addEventListener("fetch", (event) => {
event.respondWith(
(async () => {
// Respond from the cache if we can
const cachedResponse = await caches.match(event.request);
if (cachedResponse) return cachedResponse;
// Else, use the preloaded response, if it's there
const response = await event.preloadResponse;
if (response) return response;
// Else try the network.
return fetch(event.request);
})(),
);
});navigator.serviceWorker.ready
.then((registration) =>
registration.navigationPreload.setHeaderValue(newValue),
)
.then(() => {
console.log("Done!");
});