Skip to content
Novus Examples
js916 B

Service Worker (sw.js)

A sample cache-first service worker handling install, activate, and fetch events with an offline fallback — for testing service-worker registration, JS parsers, and PWA tooling.

Preview — first 29 linesjs
// sw.js - a sample service worker (cache-first for static assets, network
// fallback, and offline page). Illustrative fixture, not wired to a real app.
const CACHE = "novus-static-v1";
const ASSETS = ["/", "/index.html", "/styles.css", "/app.js", "/offline.html"];

self.addEventListener("install", (event) => {
  event.waitUntil(caches.open(CACHE).then((cache) => cache.addAll(ASSETS)));
  self.skipWaiting();
});

self.addEventListener("activate", (event) => {
  event.waitUntil(
    caches.keys().then((keys) =>
      Promise.all(keys.filter((key) => key !== CACHE).map((key) => caches.delete(key))),
    ),
  );
  self.clients.claim();
});

self.addEventListener("fetch", (event) => {
  const { request } = event;
  if (request.method !== "GET") return;
  event.respondWith(
    caches.match(request).then(
      (cached) => cached || fetch(request).catch(() => caches.match("/offline.html")),
    ),
  );
});

Specifications

Language
JavaScript
Strategy
cache-first + offline fallback
Events
install, activate, fetch

What is a .js file?

JavaScript (.js) is a plain-text source file for the JavaScript language — the scripting language of the web, also used server-side via Node.js. It is dynamically typed and event-driven, with first-class functions, prototypes, and (in modern syntax) modules, classes, and async/await.

How to use this file

Use an example .js file to test syntax highlighters, linters (ESLint), formatters (Prettier), bundlers, and JavaScript parsers, or as editor and diff-viewer fixtures.

Generated by generation/web_assets.py. Free for any use, no attribution required — license.