Loading…

Install a Progressive Web App (PWA) on Laravel

Install a Progressive Web App (PWA) on Laravel

What You'll Learn:

  • Setting up PWA package for Laravel
  • Configuring service workers and manifest files
  • Implementing offline functionality
  • Adding push notifications
  • Testing and debugging your PWA
  • Best practices and optimization

Why Build a PWA with Laravel?

Lightning Fast

Service workers cache resources for instant loading

Offline Support

Works without internet connection

App-like Experience

Native mobile app feel in browser

Push Notifications

Engage users even when app is closed

Prerequisites

Before we start, make sure you have:

  • Laravel 8.0 or higher installed
  • Composer installed on your system
  • Basic knowledge of Laravel and JavaScript
  • HTTPS enabled (required for PWA features)
  • A code editor (VS Code recommended)

Step-by-Step PWA Installation

Follow these 7 steps to transform your Laravel app into a Progressive Web App

1

Install Laravel PWA Package

We'll use the popular silviolleite/laravelpwa package to get started quickly:

Terminal
composer require silviolleite/laravelpwa

This package provides all the necessary tools and configurations for PWA implementation in Laravel.

2

Publish PWA Assets

Publish the package configuration and assets:

Terminal
php artisan vendor:publish --provider="LaravelPWA\Providers\LaravelPWAServiceProvider"

This creates the necessary configuration files and assets in your Laravel project.

3

Configure PWA Manifest

Edit the config/laravelpwa.php file to customize your PWA settings:

config/laravelpwa.php
'manifest' => [
    'name' => 'Your App Name',
    'short_name' => 'AppName',
    'start_url' => '/',
    'background_color' => '#ffffff',
    'theme_color' => '#6366f1',
    'display' => 'standalone',
    'orientation' => 'portrait',
    'status_bar' => 'black',
    'icons' => [
        '72x72' => '/images/icons/icon-72x72.png',
        '96x96' => '/images/icons/icon-96x96.png',
        '128x128' => '/images/icons/icon-128x128.png',
        '144x144' => '/images/icons/icon-144x144.png',
        '152x152' => '/images/icons/icon-152x152.png',
        '192x192' => '/images/icons/icon-192x192.png',
        '384x384' => '/images/icons/icon-384x384.png',
        '512x512' => '/images/icons/icon-512x512.png',
    ]
]
4

Add PWA Meta Tags

Add the PWA meta tags to your main layout file (typically resources/views/layouts/app.blade.php):

resources/views/layouts/app.blade.php
<!-- PWA Meta Tags -->
@laravelPWA
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#6366f1">
<link rel="apple-touch-icon" href="/images/icons/icon-192x192.png">

Place this in the <head> section of your layout.

5

Create App Icons

Generate PWA icons in the required sizes and place them in public/images/icons/:

Required Icon Sizes:

  • 72x72px
  • 96x96px
  • 128x128px
  • 144x144px

Additional Sizes:

  • 152x152px
  • 192x192px
  • 384x384px
  • 512x512px
Tip: Use tools like PWA Builder or Favicon.io to generate all icon sizes automatically.
6

Configure Service Worker

The package automatically generates a service worker. Customize it in config/laravelpwa.php:

config/laravelpwa.php
'serviceworker' => [
    'enabled' => true,
    'source' => '/serviceworker.js',
    'scope' => '/',
    'exclude_routes' => ['api/*'],
    'cache_on_update' => true,
    'cache_urls' => [
        '/offline',
        '/css/app.css',
        '/js/app.js',
    ],
]
7

Test Your PWA

Clear your application cache and test the PWA:

Terminal
php artisan config:cache
php artisan route:cache
php artisan view:cache
Open your application in Chrome and look for the "Install App" prompt in the address bar.

Advanced PWA Features

1.Creating an Offline Page

Create a fallback page for offline users. Add this route to web.php:

Route::get('/offline', function () {
return view('offline');
});

Then create resources/views/offline.blade.php:

<!DOCTYPE html>
<html>
<head>
<title>You're Offline</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="offline-container">
<h1>You're Offline</h1>
<p>Please check your internet connection and try again.</p>
</div>
</body>
</html>

2.Adding Push Notifications

Enable push notifications by updating your service worker configuration:

'enable_notifications' => true,
'gcm_sender_id' => 'your-gcm-sender-id',
'gcm_public_key' => 'your-gcm-public-key',
'gcm_private_key' => 'your-gcm-private-key',

You'll need to set up Firebase Cloud Messaging (FCM) for push notifications to work.


3.Custom Install Prompt

Add a custom install button to encourage users to install your PWA:

<button id="install-button" class="btn btn-primary" style="display: none;">
Install App
</button>

<script>
let deferredPrompt;
const installButton = document.getElementById('install-button');

window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault();
deferredPrompt = e;
installButton.style.display = 'block';
});

installButton.addEventListener('click', () => {
installButton.style.display = 'none';
deferredPrompt.prompt();
deferredPrompt.userChoice.then((choiceResult) => {
deferredPrompt = null;
});
});
</script>

Testing and Debugging

Chrome DevTools

Use Application tab to inspect manifest, service workers, and storage

Lighthouse

Run PWA audits to check installability and performance

PWA Checklist:

  • HTTPS enabled
  • Web app manifest with required fields
  • Service worker registered
  • Icons in multiple sizes
  • Offline fallback page
  • Responsive design

PWA Best Practices

Performance Optimization

  • Cache static assets aggressively
  • Use service worker for API caching
  • Implement lazy loading for images
  • Minimize bundle sizes

User Experience

  • Design for mobile-first
  • Provide clear offline messaging
  • Implement smooth animations
  • Add loading states and skeleton screens

Security

  • Always use HTTPS
  • Implement Content Security Policy
  • Validate all user inputs
  • Keep dependencies updated

Common Issues and Solutions

Issue Solution
Install prompt not showing Check HTTPS, manifest validity, and service worker registration
Service worker not updating Clear browser cache or use "Update on reload" in DevTools
Icons not displaying Verify icon paths and sizes in manifest file
Offline page not working Ensure offline route is cached in service worker

Congratulations! 🎉

You've successfully installed and configured a Progressive Web App on your Laravel application. Your users can now:

Install your app on their devices
Use your app offline
Enjoy faster loading times
Receive push notifications

Remember to test your PWA thoroughly across different devices and browsers to ensure optimal user experience.


Got an Idea or Question?

Looking to build, enhance, or fix your website? At Mirage Code, our experienced team can turn your vision into reality. Let's bring your ideas to life

No commitment required. See exactly what's holding your website back.

About Mirage Code

Mirage Code specializes in building scalable, high-converting websites for businesses of all sizes. Our team combines technical expertise with conversion optimization strategies to deliver websites that not only look great but also drive real business results.

Ready to discuss your project? Contact us today for a free consultation and discover how we can transform your website into a powerful conversion machine.

(0) Comments

No comments yet. Be the first to comment!

Leave a Reply