PWA service workers and Nx monorepos
Turning an Angular app into an installable, offline-capable PWA with @angular/pwa, and organizing several Angular projects in one Nx workspace.
Server-side rendering and hydrationBundle size and build optimization
PWA service workers and Nx monorepos
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `ng add @angular/pwa` wires up a service worker and a manifest automatically, making a standard Angular app installable and capable of serving cached content when the network is unavailable.
- `ngsw-config.json` controls exactly which caching strategy applies to which assets and API calls — there's no single universal caching behavior, and getting this config wrong is the most common source of a PWA serving stale content.
- Nx solves a completely different problem — organizing several related Angular projects and shared libraries in one workspace with consistent tooling — worth knowing about even though it has nothing to do with PWAs beyond both living in this lesson's grab-bag of production-ecosystem tooling.
Making a web app behave more like a native one
A Progressive Web App is an ordinary web app with a specific set of capabilities layered on top: it can be installed to a device’s home screen, it can keep working (at least partially) with no network connection, and it can feel, from a user’s perspective, closer to a native app than a browser tab. None of that is automatic — it requires two specific pieces: a web app manifest describing the app to the browser (name, icons, how it should launch when installed), and a service worker, a script that runs separately from your app and intercepts network requests, able to serve a cached response instead of hitting the network at all.
ng add @angular/pwa
This single command wires up both pieces for a standard Angular project — generating the manifest, registering a service worker, and configuring the build to produce what both need. The app immediately becomes installable, and the service worker begins caching assets according to a configuration file this addition also generates.
ngsw-config.json: the configuration that decides what gets cached, and how
{
"assetGroups": [{ "name": "app", "installMode": "prefetch", "resources": { "files": ["/**/*.js", "/**/*.css"] } }],
"dataGroups": [{ "name": "api", "urls": ["/api/**"], "cacheConfig": { "strategy": "freshness", "maxAge": "1h" } }]
}
A service worker’s caching behavior isn’t one universal setting — different
assets genuinely need different treatment. Your own app’s JS and CSS bundles
are usually safe to cache aggressively and prefetch on install, since a new
build simply has a new filename. API responses are a different story entirely:
cache them too aggressively, and users can be looking at data that’s
meaningfully out of date, with no obvious indication anything’s wrong.
ngsw-config.json is where you declare these distinctions explicitly —
assetGroups for your own static files, dataGroups for API calls, each with
its own caching strategy. Getting this configuration wrong — usually erring
too aggressive, too broad — is the single most common source of a PWA quietly
serving stale content to real users.
Nx: solving an entirely unrelated problem
Nothing above has anything to do with what follows, and that’s worth saying plainly rather than forcing a connection that isn’t there: Nx addresses organizing multiple related Angular applications and shared libraries within one single workspace — consistent tooling, dependency-graph-aware builds (only rebuilding and retesting the projects actually affected by a change), and a shared set of conventions across everything in the repository. It matters once you’re maintaining more than one Angular app that shares real code — a design system, a set of API clients, common utilities — and want that sharing to be a first-class, well-tooled part of the workspace rather than an awkward published package you have to version and republish for every internal change.
This lesson’s video builds PWA conversion directly on a real app, including testing the offline behavior once it’s wired up; the two linked videos split the remaining ground — Nx monorepo organization in full, and a second, shorter pass on PWA conversion if a quicker second example helps.
Check yourself
After running `ng add @angular/pwa`, what two things does a browser now recognize about your app that it didn't before?
A web app manifest (declaring the app's name, icons, and install behavior, letting a browser offer to install it like a native app) and a registered service worker (intercepting network requests and able to serve cached responses when offline). Neither existed in a plain Angular app before this addition wired them up.
Why does ngsw-config.json matter so much for whether a PWA's users see stale content — what's actually being configured there?
It defines the caching strategy per group of assets or API routes — how aggressively to cache, when to check for updates, which requests should bypass the cache entirely. A caching strategy that's too aggressive for frequently-changing API data can mean a user keeps seeing yesterday's data long after the server has moved on, which is the single most common complaint about poorly-configured PWAs.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.