Building PWAs with Faststrap
Faststrap provides a production-safe PWA baseline through add_pwa():
- Manifest generation (
/manifest.json) - Service worker route (
/sw.js) - Automatic service worker registration
- Offline fallback route (
/offline) - Runtime caching for better resilience
Accessibility note:
- Faststrap keeps mobile zoom enabled by default (width=device-width, initial-scale=1).
Quick Start
from fasthtml.common import FastHTML
from faststrap import add_bootstrap, add_pwa
app = FastHTML()
add_bootstrap(app)
add_pwa(
app,
name="My App",
short_name="MyApp",
theme_color="#0d6efd",
icon_path="/static/icon.png",
)
What add_pwa() Configures
- Injects required PWA/mobile meta tags.
- Serves a generated
manifest.json. - Serves a robust service worker script at
/sw.js. - Injects service worker registration code into app headers.
- Serves
/offlinepage (enabled by default). - Respects
scopefor scoped deployments (for example/myapp/sw.js).
Default Offline/Caching Strategy
The built-in service worker uses:
- Install: tolerant pre-cache (
Promise.allSettled) so one failed URL does not break install. - Navigation requests: network-first with cache fallback, then
/offline. - Static assets (
css,js,images, fonts): stale-while-revalidate. - Other GET requests: network-first with runtime cache write-through.
This is a practical baseline for production apps that need reliable offline fallback behavior.
Advanced Configuration
add_pwa() now supports cache controls:
add_pwa(
app,
cache_name="myapp-cache",
cache_version="v2026-02-23",
pre_cache_urls=(
"/health",
"/assets/logo.png",
),
)
cache_name: prefix for cache storagecache_version: version suffix for cache invalidation on deploypre_cache_urls: additional URLs to pre-cache
Faststrap still pre-caches its core defaults and /offline.
Background Sync Foundation (Opt-in)
Faststrap includes a lightweight background sync scaffold that you can enable without committing to a queue implementation yet:
enable_background_sync: enables service workersyncevent hooksbackground_sync_tag: tag used when registering sync tasks
This is intentionally a foundation layer for v0.6.x work; request queue persistence/replay is still application-defined.
Route-Aware Cache Policies (Opt-in)
You can define route prefix policies in the generated service worker:
add_pwa(
app,
route_cache_policies={
"/api/public/": "stale-while-revalidate",
"/assets/": "cache-first",
},
)
Supported strategies:
network-firststale-while-revalidatecache-first
Push Foundation (Opt-in)
Enable push event scaffolding in the generated service worker:
This adds push and notificationclick handlers with safe defaults.
When to Use a Custom Service Worker
Use a custom sw.js when you need:
- fine-grained API caching rules
- background sync
- push notifications
- per-route cache policies
In that case:
- pass
service_worker=Falsetoadd_pwa() - mount your own
/sw.jsroute
Mobile Components
Faststrap also includes mobile-oriented UI components:
BottomNav,BottomNavItemSheetInstallPrompt