Skip to content

Commit

Permalink
🚀 Allow ServiceWorker /ghost/ scope (#7967)
Browse files Browse the repository at this point in the history
ServiceWorkers can only control the scope from which they have been served. Our service workers live, like all other files, in an `asset` folder - and could in theory only work on other files in there.

This commit fake-serves our service workers from `/ghost/`, thus allowing them to give everything offline powers.
  • Loading branch information
felixrieseberg authored and kevinansfield committed Feb 11, 2017
1 parent a341bbd commit 53bd7da
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/server/admin/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ module.exports = function setupAdminApp() {
{maxAge: utils.ONE_YEAR_MS, fallthrough: false}
));

// Service Worker for offline support
adminApp.get(/^\/(sw.js|sw-registration.js)$/, require('./serviceworker'));

// Render error page in case of maintenance
adminApp.use(maintenance);

Expand Down
16 changes: 16 additions & 0 deletions core/server/admin/serviceworker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var debug = require('debug')('ghost:admin:serviceworker'),
path = require('path');

// Route: index
// Path: /ghost/sw.js|sw-registration.js
// Method: GET
module.exports = function adminController(req, res) {
/*jslint unparam:true*/
debug('serviceworker called');

var sw = path.join(__dirname, '..', '..', 'built', 'assets', 'sw.js'),
swr = path.join(__dirname, '..', '..', 'built', 'assets', 'sw-registration.js'),
fileToSend = req.url === '/sw.js' ? sw : swr;

res.sendFile(fileToSend);
};

0 comments on commit 53bd7da

Please sign in to comment.