DocsoPWAService Worker & Caching Strategies
Features

Service Worker & Caching Strategies

oPWAorravo.com/docs/opwa/service-worker

Service Worker

The SW is served at /sw.js as dynamic PHP output (registered via a rewrite rule). This allows PHP to inject config values without a build step.

Caching Strategies

StrategyBehavior
Network FirstNetwork → cache fallback (good for HTML)
Cache FirstCache → network fallback (good for versioned assets)
Stale While RevalidateReturn cache immediately, update in background
Network OnlyAlways network (WooCommerce checkout/cart)
Cache OnlyAlways cache (precached assets only)

Default strategy assignment

Asset typeSetting keyDefault
HTML pagesstrategy_pagesnetwork-first
Static assets (JS/CSS)strategy_staticcache-first
Imagesstrategy_imagescache-first
Fontsstrategy_fontscache-first

Custom URL Routing

In Service Worker → Custom Routes, add URL patterns with custom strategy overrides:

Pattern: /api/*         Strategy: network-only
Pattern: /blog/*        Strategy: stale-while-revalidate
Pattern: /wp-content/*  Strategy: cache-first

Patterns support * wildcards and are matched against the request URL.

Precache

List of URLs to cache on SW install. Default includes the home page and critical static assets. Add custom URLs in Service Worker → Precache List.

Offline Fallback

Set an offline fallback page in Offline tab. When the network fails and no cache entry exists, the SW serves this page instead of a browser error.

Service Worker & Caching Strategies — oPWA Docs — Orravo