✅
PWA Bundle
1.1.x
1.1.x
  • PHPWA Bundle
  • How To Create A PWA?
  • How To Install/Remove A PWA?
  • Bundle Installation
  • Deployment
  • The Manifest
    • Application Information
      • Scope
      • ID
      • Direction and Language
      • Orientation
      • Description
      • IARC Rating ID
      • Categories
    • Icons
    • Screenshots
    • Shortcuts
    • Protocol Handlers
    • File Handlers
    • Share Target
    • Complete Example
  • The Service Worker
    • Configuration
    • Content Security Policy
    • Custom Service Worker Rule
    • Workbox
      • Site Manifest Cache
      • Asset Caching
      • Resource Caching
      • Image Caching
      • Font Caching
      • Offline Fallbacks
      • BackgoundSync
      • CDN and Versions
      • Cache Cleaning
      • Custom Cache Strategy
    • Push Notifications
    • Complete Example
  • Symfony UX
    • Connection Status
    • Prefetch on demand
    • Sync Broadcast
    • BackgroundSync Form
  • Image Management
    • Icons
    • Screenshots
  • Experimental Features
    • Non-Standard Parameters
      • Launch Handler
      • Display Override
      • Related Applications
      • EDGE Side Panel
    • Translations
    • Widgets (Win10+)
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
Export as PDF
  1. The Service Worker

Custom Service Worker Rule

If needed, you can define custom sections in the service worker appended by your own services and depending on your application configuration or requirements.

To do so, you can create a service that implements SpomkyLabs\PwaBundle\ServiceWorkerRule\ServiceWorkerRule.

The method process shall return valid JS as a string. This script will be executed by browsers.

<?php

declare(strict_types=1);

namespace Acme;

use SpomkyLabs\PwaBundle\ServiceWorkerRule\ServiceWorkerRule;

final readonly class MyCustomRule implements ServiceWorkerRule
{
    private Workbox $workbox;

    public function __construct(
        ServiceWorker $serviceWorker,
    ) {
        $this->workbox = $serviceWorker->workbox;
    }

    public function process(bool $debug = false): string
    {
        return <<<HELLO_WORLD
// This will be added to the Service Worker
console.log('FOO-BAR from the Service Worker!');
HELLO_WORLD;
    }
}
PreviousContent Security PolicyNextWorkbox

Last updated 1 year ago

Was this helpful?