This requires PHP 8.x or higher.
Generating ServiceWorker scripts is easy if you are on Node. There you you have many tools to do that for you.
On something like PHP, that is a different story.
So this is going to be a Tool/Class for PHP to generate and automatically Update a ServiceWorker, based on a few parameters you can set.
The Generator will keep track of all files that are registered in its generated ServiceWorker.
To do so a sw. lock
file is created next to the script, that is using the ServiceWorgerGenerator
.
The sw.lock
file helps the Generator to keep track of all possible previous states a ServiceWorker could have been in.
That way it is possible to make the ServiceWorker cleanup the Caches generated by previous versions of itself.
Should anything regarding the registered files change, the ServiceWorker will update itself and clean up the old data.
Here is an overview of when such a ServiceWorker-Update is triggered.
function | if added | if deleted | on content change |
---|---|---|---|
fileCacheFirst |
✅ | ✅ | ✅ |
patternFallback |
✅ | ✅ | if content of the fallback file changes |
enableDirectoryIndexCache |
✅ | ✅ | ❌ |
fileCacheOnDemand |
✅ | ✅ | cached file is deleted, and only reloaded on next re quest unless ignoreOnDemandCacheOnRebuild is set before printing the ServiceWorker. |
dirCacheOnDemand |
✅ | ✅ | cached file is deleted, and only reloaded on next re quest unless ignoreOnDemandCacheOnRebuild is set before printing the ServiceWorker. |
cacheName |
always exists (has a default State, if not defined) | delete not possible | ✅ |
dirCacheFirst |
if any file is added to the given directory | if any file is removed from the given directory | if any of the directorys files binary content changes |
-
Standalone (No external dependencies at all)
-
Needs to be able to generate a valid service worker script to:
- ✅ precache static files in the client browser and deliver them in a "cacheFirst" approach.
- ✅ store files in an "cacheFirst" fashion, and use the cache as a fallback for Offline / error cases.
- ✅ store files in an "onDemand" fashion, and deliver them cacheFirst . (means cache them only once they have been requested)
- ✅ store files in an "onDemand" fashion, and use the cache as a fallback for Offline / error cases.
-
Maintain a list of files, currently handled by the Service-Worker
-
Only recreate the Service-Worker if things actually change in the project.
Add more functionality, like "Push Notification" handling.
Create a sw.php
file.
- in the file, create an instance of the class.
- configure what directories/files are handled how by the ServiceWorker
printAndExit()
and the Configuration.
<?php
require_once __DIR__ . "/lib/ServiceWorkerGenerator.php";
(new rogoss\ServiceWorkerGenerator())
->cacheName("PHPSWGen_Test") // Define what prefix the cache in the browser will receive
// Needs to be set, if you have multiple ServiceWorkers in different scopes on the same server.
->fileCacheFirst("./index.html") // precache the index.html and deliver it CacheFirst
->dirCacheFirst("./vendor") // precache every file in the folder ./vendor and deliver it CacheFirst
->enableDirectoryIndexCache("/") // Adds a PATH directly to the precache CacheFirst list
// The Path is called directly by the ServiceWorker
->fileCacheOnDemand("onDemand/odm1.txt") // Register File to be cached after it has been requested for the first time (and then delivered CacheFirst after that)
->dirCacheOnDemand("onDemand/dir") // Register a Directorys files to be cached after they have been requested for the first time (and then delivered CacheFirst after that)
->ignoreOnDemandCacheOnRebuild() // by default, the CacheOnDemand Files/Dirs are cleared out every time, the ServiceWorker updates (Requireing them to be downloaded after every update).
// calling this function will make all Files/Dirs that are cached on demand persistent until you change the cacheName, or remove this function call.
->patternFallback("\.svg$", "./img/PhWifiSlashBold.svg") // All requests that end in .svg (or .SVG, it is not case sensitive)
// that are not answered with 2xx or 3xx by the Server
// Will be answered by ServiceWorker with the "./img/PhWifiSlashBold.svg"
->printAndExit() // Finish the generation and end the PHP-Script
;
All Setter - Functions return $this
, which means, you can chain all setters as shown above.