Installation
composer require laravelir/pwa
Publish the config file:
php artisan vendor:publish --provider="Laravelir\Pwa\PwaServiceProvider"
Basic Configuration
Edit config/pwa.php to define:
manifest (path to your web app manifest)service_worker (path to your service worker script)icons (array of icon paths for different resolutions)theme_color and background_color (for PWA theme consistency)First Use Case: Add PWA Meta Tags
Include the PWA service provider in config/app.php:
'providers' => [
Laravelir\Pwa\PwaServiceProvider::class,
],
Add the PWA tag to your app.blade.php (or layout file):
@pwa
Use Laravel’s blade directives to dynamically set manifest properties:
@pwa([
'name' => config('app.name'),
'short_name' => Str::limit(config('app.name'), 12),
'start_url' => url('/'),
'display' => 'standalone',
])
Disable PWA for specific routes or user roles:
// In a middleware or controller
if (auth()->check() && auth()->user()->is_admin) {
return response()->view('admin.dashboard')->withoutPwa();
}
Trigger service worker updates via Laravel events (e.g., after deployment):
// In a command or event listener
event(new \Laravelir\Pwa\Events\ServiceWorkerUpdated);
Leverage Laravel Mix/Vite to version assets and update the manifest dynamically:
// mix.js
mix.version();
@pwa([
'icons' => [
asset('images/icon-192x192.png?v=' . config('app.version')),
asset('images/icon-512x512.png?v=' . config('app.version')),
],
])
Extend your webpack.mix.js to compile PWA assets:
mix.js('resources/js/service-worker.js', 'public/js')
.postCss('resources/css/pwa.css', 'public/css', [
require('postcss-import'),
require('tailwindcss'),
]);
<body>:
<script src="{{ asset('js/service-worker.js') }}"></script>
php artisan pwa:test
name or icons) will cause PWA installation to fail.php artisan pwa:validate
// In your service-worker.js
const CACHE_NAME = `cache-v${appVersion}`;
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cache) => {
if (cache !== CACHE_NAME) {
return caches.delete(cache);
}
})
);
})
);
});
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((response) => {
return response || caches.match('/offline');
})
);
});
Create a blade template at resources/views/offline.blade.php.console.log in the worker to debug:
self.addEventListener('install', (event) => {
self.clients.matchAll().then((clients) => {
clients.forEach((client) => {
client.postMessage('Service worker installed!');
});
});
});
php artisan vendor:publish --tag=pwa-assets
Edit resources/views/vendor/pwa/service-worker.blade.php.trust-proxy middleware in development:
TrustProxies::middlewares(['fideloper/proxy']);
How can I help you explore Laravel packages today?