Deployment
Deploying a PWA requires compiling assets to ensure optimal performance in production. This guide covers both deployment methods: with and without Symfony's Asset Mapper.
Understanding PWA Asset Compilation
Development vs Production
Development (APP_ENV=dev):
Assets generated on-the-fly
Request listener serves resources dynamically
Debugging enabled (verbose logging, comments)
No optimization or minification
Production (APP_ENV=prod):
All assets pre-compiled and optimized
Resources served as static files
Debugging disabled
Minified and optimized for performance
What Gets Compiled
The compilation process generates:
Web App Manifest (
manifest.json)With correct production URLs
Optimized and minified
Service Worker (
sw.js)Complete with Workbox integration
Cache strategies configured
Optimized and minified
Icons and Favicons
All required sizes (16x16 to 512x512)
Platform-specific formats (PNG, ICO, SVG)
Optimized file sizes
Screenshots (if configured)
App store screenshots
Various device sizes
Dependencies
Workbox libraries
IndexedDB polyfills
Other runtime dependencies
Deployment with Asset Mapper
If you're using Symfony's Asset Mapper (recommended), PWA assets are compiled together with your application assets.
Step 1: Configure Asset Compilation
Ensure asset compilation is enabled (this is the default):
pwa:
asset_compiler: true # Default in 1.xStep 2: Compile Assets
Run the standard Asset Mapper compilation command before deployment:
php bin/console asset-map:compileThis single command:
Compiles all application assets
Generates PWA resources (manifest, service worker, icons)
Optimizes and minifies everything
Prepares assets for production
Step 3: Deploy
Deploy your application with the compiled assets:
# Example deployment workflow
php bin/console asset-map:compile
git add public/assets
git commit -m "Compile assets for production"
git push production mainAsset Mapper integration is the simplest approach - everything is handled in one command!
Deployment without Asset Mapper
If you're not using Asset Mapper or prefer standalone PWA asset compilation, use the dedicated pwa:compile command.
Step 1: Disable Asset Mapper Integration
Configure the bundle to handle compilation independently:
pwa:
asset_compiler: falseStep 2: Compile PWA Assets
Run the PWA-specific compilation command:
php bin/console pwa:compileThis compiles all PWA resources exactly as Asset Mapper would, but independently.
Step 3: Deploy
Deploy your application with the compiled PWA assets.
Context-Aware Deployment
If your application URLs depend on the deployment environment (e.g., different domains for staging and production), you need context-aware compilation.
The Problem
The manifest and service worker include absolute URLs. If you compile assets locally, they might reference http://localhost instead of your production domain https://example.com.
Example issue:
{
"start_url": "http://localhost/app", // ❌ Wrong in production
"scope": "http://localhost/" // ❌ Wrong in production
}Solution 1: Compile in Production Environment
The best approach is to compile assets in the production environment where the correct context is available:
# During deployment on production server
APP_ENV=prod APP_DEFAULT_URI=https://example.com php bin/console pwa:compileSolution 2: Two-Step Compilation
If you must compile assets before deployment (e.g., in CI/CD), use a two-step process:
Step 1: Initial compilation (local or CI)
# Compile all assets (including time-consuming icon generation)
php bin/console pwa:compileStep 2: Context-only recompilation (on production)
# Only recompile context-dependent files (manifest and service worker)
APP_ENV=prod php bin/console pwa:compile --context-onlyThe --context-only flag:
Skips icon generation (already done in step 1)
Only regenerates manifest and service worker
Uses correct production URLs
Completes in seconds instead of minutes
This two-step approach balances build time (icons generated once) with URL correctness (manifest uses production URLs).
Setting Application Context
Ensure your production environment has the correct context configured:
# config/packages/prod/framework.yaml
framework:
router:
default_uri: 'https://example.com'Or set via environment variable:
# .env.prod
APP_DEFAULT_URI=https://example.comDeployment Checklist
Before deploying your PWA to production:
Continuous Integration Example
Here's an example GitLab CI/CD configuration:
stages:
- build
- deploy
build:
stage: build
script:
- composer install --no-dev --optimize-autoloader
- php bin/console pwa:compile
artifacts:
paths:
- public/
deploy:production:
stage: deploy
script:
- rsync -avz public/ user@server:/var/www/html/
- ssh user@server "cd /var/www/html && php bin/console pwa:compile --context-only"
only:
- mainVerifying Deployment
After deployment, verify your PWA is working correctly:
Check Manifest
curl https://example.com/manifest.jsonVerify:
All URLs use production domain
Icons paths are correct
No localhost references
Check Service Worker
curl https://example.com/sw.jsVerify:
File exists and loads
No debugging code (in production)
Minified and optimized
Browser Testing
Open DevTools → Application tab
Check Manifest section - no errors
Check Service Workers - registered and active
Test Install prompt appears
Test Offline functionality
Troubleshooting Deployment
Assets Not Found (404)
Problem: PWA assets return 404 errors
Solutions:
Ensure compilation ran successfully
Check
public/directory contains compiled assetsVerify web server serves static files from
public/Clear application cache:
php bin/console cache:clear
Wrong URLs in Manifest
Problem: Manifest contains localhost or incorrect URLs
Solutions:
Set
default_uriin framework configUse
--context-onlyflag during production deploymentCompile assets in production environment
Service Worker Not Updating
Problem: Old service worker version remains active
Solutions:
Update cache version in configuration
Change service worker file name/path
Implement
skipWaiting: truefor immediate updatesClear browser service worker cache
Icons Not Displaying
Problem: App icons don't appear or are broken
Solutions:
Verify icon files exist in
public/directoryCheck icon paths in manifest are absolute
Ensure icons are accessible (not blocked by firewall)
Regenerate icons:
php bin/console pwa:compile
Performance Optimization
For optimal PWA performance in production:
Enable CDN for static assets
Configure caching headers for PWA resources
Implement versioning for cache invalidation
Compress assets (gzip/brotli)
Use HTTP/2 for faster resource loading
Example Nginx configuration:
location /manifest.json {
expires 1d;
add_header Cache-Control "public, immutable";
}
location /sw.js {
expires 1h;
add_header Cache-Control "public";
add_header Service-Worker-Allowed "/";
}Last updated
Was this helpful?