withFlux()
A Next.js config wrapper that generates your service worker at build time and injects the hydration race fix script into every page. The one call that connects Flux's IDB layer to offline navigation and asset caching.
- @tsworldtech/flux-next installed
- A storagePrefix that matches the one passed to createFlux(), see api/create_flux
- idbKey and listKey values that match the channels you register with flux.register()
Basic usage
withFlux from @tsworldtech/flux-next/plugin. It takes the Flux config first and returns a function that wraps your Next.js config, so it composes with any other config wrappers you already use.Config reference
precacheRoutes, offlineShell, and storagePrefix are the fields you will set on nearly every project. storagePrefix specifically must match whatever you pass to createFlux(), since the SW and the engine need to agree on the same IDB namespace. The dynamic route fields are only needed if your app has routes like /projects/[slug] that must work offline.What happens at build time
The SW vs App Router hydration race fix
withFlux() exists to solve: without a signal for "the service worker is actually controlling this page yet," the App Router can render before the SW is ready to serve cached assets, causing a flash of unstyled or unhydrated content on repeat visits.window.FLUX_SW_READY only matters because HydrationGate reads it. If you are not using HydrationGate, this script still runs but nothing consumes it by default.
dynamicRoutePrefixes
/portal/projects/[slug] work fully offline, even for a specific slug the user has never visited before, as long as at least one page has been visited once. Every prefix needs a trailing slash.dynamicRouteShapes
dynamicRouteIdbKeys
dynamicRoutePrefixes exactly.If your URLs use a human readable identifier while the detail entry is keyed by a UUID, the exact key lookup will miss. listKey and listFields let the SW find the item by scanning the cached list instead.
bypassRoutePrefixes
What the generated service worker does
withFlux() configured for you.For the hook that consumes window.FLUX_SW_READY, see api/hydrate. For how assets[] reaches the FLUX_WARM_CHUNKS listener in the first place, see api/bootstrap. Building with Vite or Remix instead of Next.js? See the Vite plugin covered under the flux-react package docs, which uses the same SW template with no App Router specifics.