fix: #9127, scope service worker to relative_path for the forum (#9239)

Adds a `Service-Worker-Allowed` header on `assets/src/service-worker.js` URL and uses `scope` option during registration to ensure the service worker is correctly scoped to the entire forum and only the forum.
v1.18.x
Opliko 4 years ago committed by GitHub
parent 897d29ecd6
commit 2bc74cffe6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -788,7 +788,7 @@ app.cacheBuster = null;
function registerServiceWorker() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register(config.relative_path + '/assets/src/service-worker.js')
navigator.serviceWorker.register(config.relative_path + '/assets/src/service-worker.js', { scope: config.relative_path + '/' })
.then(function () {
console.info('ServiceWorker registration succeeded.');
}).catch(function (err) {

@ -1,6 +1,7 @@
'use strict';
const path = require('path');
const nconf = require('nconf');
module.exports = function (app, middleware, controllers) {
app.get('/sitemap.xml', controllers.sitemap.render);
@ -11,7 +12,7 @@ module.exports = function (app, middleware, controllers) {
app.get('/manifest.webmanifest', controllers.manifest);
app.get('/css/previews/:theme', controllers.admin.themes.get);
app.get('/osd.xml', controllers.osd.handle);
app.get('/service-worker.js', function (req, res) {
res.status(200).type('application/javascript').sendFile(path.join(__dirname, '../../public/src/service-worker.js'));
app.get('/assets/src/service-worker.js', function (req, res) {
res.status(200).type('application/javascript').set('Service-Worker-Allowed', nconf.get('relative_path') + '/').sendFile(path.join(__dirname, '../../public/src/service-worker.js'));
});
};

Loading…
Cancel
Save