chore: move service worker generation code to its own controller file

This commit is contained in:
Julian Lam
2024-09-25 12:40:56 -04:00
parent 979954c29e
commit c153c4694b
3 changed files with 51 additions and 45 deletions

View File

@@ -34,6 +34,7 @@ Controllers.globalMods = require('./globalmods');
Controllers.mods = require('./mods');
Controllers.sitemap = require('./sitemap');
Controllers.osd = require('./osd');
Controllers['service-worker'] = require('./service-worker');
Controllers['404'] = require('./404');
Controllers.errors = require('./errors');
Controllers.composer = require('./composer');

View File

@@ -0,0 +1,49 @@
'use strict';
const { readFile, access, constants } = require('fs/promises');
const path = require('path');
const nconf = require('nconf');
const plugins = require('../plugins');
const Controller = module.exports;
Controller.generate = async (req, res) => {
const swPath = path.join(__dirname, '../../build/public/src/service-worker.js');
let swContents = await readFile(swPath, { encoding: 'utf-8' });
res.status(200)
.type('application/javascript')
.set('Service-Worker-Allowed', `${nconf.get('relative_path')}/`);
/**
* Allow plugins to append their own scripts for the service worker to import
* expects: URLs in passed-in Set, either absolute or relative to plugin static directory root (/assets/plugins)
* see: https://docs.nodebb.org/development/plugins/statics
*/
let scripts = new Set();
({ scripts } = await plugins.hooks.fire('filter:service-worker.scripts', { scripts }));
if (!scripts.size) {
res.sendFile(swPath);
} else {
const urls = await Promise.all(Array
.from(scripts)
.map(async (pathname) => {
try {
const url = new URL(pathname, `${nconf.get('url')}/assets/plugins/`);
if (url.href.startsWith(nconf.get('url'))) {
const fullPath = path.resolve(__dirname, '../../build/public/plugins', url.pathname.replace(`${nconf.get('relative_path')}/assets/plugins/`, ''));
await access(fullPath, constants.R_OK);
}
return url;
} catch (e) {
return null;
}
}));
const payload = urls.map(urlObj => urlObj.href).join("', '");
swContents += `\nimportScripts('${payload}')`;
res.send(swContents);
}
};

View File

@@ -1,11 +1,5 @@
'use strict';
const { readFile, access, constants } = require('fs/promises');
const path = require('path');
const nconf = require('nconf');
const plugins = require('../plugins');
module.exports = function (app, middleware, controllers) {
app.get('/sitemap.xml', controllers.sitemap.render);
app.get('/sitemap/pages.xml', controllers.sitemap.getPages);
@@ -15,43 +9,5 @@ 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', async (req, res) => {
const swPath = path.join(__dirname, '../../build/public/src/service-worker.js');
let swContents = await readFile(swPath, { encoding: 'utf-8' });
res.status(200)
.type('application/javascript')
.set('Service-Worker-Allowed', `${nconf.get('relative_path')}/`);
/**
* Allow plugins to append their own scripts for the service worker to import
* expects: URLs in passed-in Set, either absolute or relative to plugin static directory root (/assets/plugins)
* see: https://docs.nodebb.org/development/plugins/statics
*/
let scripts = new Set();
({ scripts } = await plugins.hooks.fire('filter:service-worker.scripts', { scripts }));
if (!scripts.size) {
res.sendFile(swPath);
} else {
const urls = await Promise.all(Array
.from(scripts)
.map(async (pathname) => {
try {
const url = new URL(pathname, `${nconf.get('url')}/assets/plugins/`);
if (url.href.startsWith(nconf.get('url'))) {
const fullPath = path.resolve(__dirname, '../../build/public/plugins', url.pathname.replace(`${nconf.get('relative_path')}/assets/plugins/`, ''));
await access(fullPath, constants.R_OK);
}
return url;
} catch (e) {
return null;
}
}));
const payload = urls.map(urlObj => urlObj.href).join("', '");
swContents += `\nimportScripts('${payload}')`;
res.send(swContents);
}
});
app.get('/service-worker.js', controllers['service-worker'].generate);
};