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.
This commit is contained in:
Opliko
2021-02-02 01:40:14 +01:00
committed by GitHub
parent 897d29ecd6
commit 2bc74cffe6
2 changed files with 4 additions and 3 deletions

View File

@@ -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) {

View File

@@ -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'));
});
};