I am currently working on a plugin that uses a service worker which gets registered on the front-end by some javascript code and the problem is that the service worker scope is restricted to /cpresources/610c42da/
as the Asset Manager only serves the file from there.
Here's the code which I currently use to integrate with the front-end:
Craft::$app->getView()
->hook('cachecraft', function () {
// Pass the service worker url to the site
Craft::$app->getView()
->registerScript(
'const CACHECRAFT_SERVICE_WORKER = "' .
Craft::$app->assetManager->getPublishedUrl('@skayo/cachecraft/assetbundles/cachecraft/dist/js/service-worker.js', true) .
'";',
1,
[],
'cachecraft_service_worker'
);
// Register asset bundle
Craft::$app->getView()
->registerAssetBundle(CacheCraftAsset::class);
});
So what I need is to serve my service worker from the root (https://example.com/service-worker.js
) of the website.
Is that possible somehow?
Another possible way is to serve the file normally at https://example.com/cpresources/610c42da/service-worker.js
BUT with a Service-Worker-Allowed HTTP Header.
But I don't know how to do that either...
Thanks in advance for any help!
/sw.js
(or whatever) to the actual URL to your resource. – andrew.welch Sep 8 '19 at 18:23The script resource is behind a redirect, which is disallowed.
I hate this so much 😫 So no redirects... Need another solution... – Skayo Sep 8 '19 at 20:37