API Reference
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 line that connects Flux's IDB layer to offline navigation and asset caching.
Prerequisites
- @tsworldtech/flux-next installed
- A storagePrefix that matches the one passed to createFlux() — see api/create-flux
1
Basic usage
Wrap your existing Next.js config with
withFlux() — it returns a modified config, so it composes with any other config wrappers you already use.typescript
2
Config reference
precacheRoutes, offlineShell, and storagePrefix are the fields you'll 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.typescript
3
What happens at build time
Two things happen automatically — you never hand-write or edit the generated service worker file yourself.
typescript
4
The SW vs App Router hydration race fix
This is the specific problem
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.typescript
Pairs directly with HydrationGate
window.FLUX_SW_READY only matters because HydrationGate reads it. If you're not using HydrationGate, this script still runs but nothing consumes it by default.
5
dynamicRoutePrefixes
This is what makes a dynamic route like
/blog/[slug] work fully offline, even for a specific slug the user has never visited before — as long as the homepage (or any page) has been visited once.typescript
6
What the generated service worker does
You don't write any of this — it's worth knowing what's running, since it's the layer between a navigation and whatever
withFlux() configured for you.typescript
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 — same SW template, no App Router specifics.