Skip to content

Commit

Permalink
Fix mozilla#634: Simplify service worker caching (mozilla#682)
Browse files Browse the repository at this point in the history
* Fix mozilla#634: simplify service worker caching

* Rework staticFileGlobs to ignore dist/nls, add runtimeCaching for nls, fonts

* Fix review issues, rework ?disableExtensions logic

* Compress dist/ and bramble-sw.js, call uglify after build-extensions
  • Loading branch information
humphd authored and Rajat Dhyani committed Apr 20, 2017
1 parent 45f06ff commit f128c50
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 176 deletions.
9 changes: 0 additions & 9 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,3 @@ code contributions, reviewing pull requests, and providing feedback and suggesti
direction of the project.

Even if you're not a committer, you're still welcome to give feedback on any pull request!

## Adding New Files

When run in production (i.e., the resulting `dist/` dir from running `grunt build-browser-compressed`),
Bramble uses a Service Worker to cache and serve the app offline. In order to do this, the
`swPrecache` grunt task generates a Service Worker ready to cache and serve all the necessary
files. This file list is generated statically at build time based on the contents of the
`sw-cache-file-list.json` file. If you add new files (e.g., a new default extension), make sure
you add URL entries to this cache list.
35 changes: 27 additions & 8 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,16 @@ module.exports = function (grunt) {
cwd: 'dist/',
src: ['**/*'],
dest: 'dist/'
},
// We need to compress the bramble-sw.js service worker file after compressing dist/
sw: {
options: {
mode: "gzip"
},
expand: true,
cwd: 'dist/',
src: 'bramble-sw.js',
dest: 'dist'
}
},

Expand Down Expand Up @@ -471,14 +481,22 @@ module.exports = function (grunt) {
grunt.registerMultiTask('swPrecache', function() {
var done = this.async();
var rootDir = this.data.rootDir;
var files = (function() {
return (require('./sw-cache-file-list.json')).files;
}());

var config = {
cacheId: 'bramble',
logger: grunt.log.writeln,
staticFileGlobs: files,
staticFileGlobs: [
// Avoid caching dist/nls/**/*, but take everything else in dist/
'dist/{extensions,styles,thirdparty}/**/*',
'dist/*.*'
],
runtimeCaching: [{
urlPattern: /^https:\/\/fonts\.googleapis\.com\/css/,
handler: 'fastest'
}, {
urlPattern: /\/dist\/nls\//,
handler: 'fastest'
}],
stripPrefix: 'dist/',
ignoreUrlParametersMatching: [/./]
};
Expand Down Expand Up @@ -530,15 +548,16 @@ module.exports = function (grunt) {
'build',
'requirejs:iframe',
'exec:localize-dist',
'uglify',
'build-extensions'
'build-extensions',
'uglify'
]);

// task: build dist/ for browser, pre-compressed with gzip and SW precache
grunt.registerTask('build-browser-compressed', [
'build-browser',
'compress',
'swPrecache'
'compress:dist',
'swPrecache',
'compress:sw'
]);

// task: undo changes to the src/nls directory
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ should host Bramble's iframe, see `src/hosted.js`.
# Extension Loading

Bramble loads a set of extensions defined in `src/extensions/bramble-extensions.json`. You can
add remove from this list. You can also temporarily disable extensions by using `?disableExtensions`.
For example, to disable QuickView and CSSCodeHints load Bramble with `?disableExtensions=QuickView,CSSCodeHints` on the URL.
alter which extensions Bramble loads by adding or removing items from this list. You can also
temporarily disable extensions by using `?disableExtensions`. For example: to disable QuickView
and CSSCodeHints, load Bramble with `?disableExtensions=QuickView,CSSCodeHints` on the URL.

--------------

Expand Down
12 changes: 6 additions & 6 deletions src/extensions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ This is an array of objects with the following form:
```

Here `path` refers to the path under `src/` where the extension's dir lives.
The optional `copy` array includes file path globs to be used when copying
files from `src/` to `dist/` for this extension at build time. Many extensions
have no external dependencies, other than the `main.js` file and any modules it
loads. If this is the case, you don't need to include `copy`. It will typically
include things like stylesheets, images, and other resources that get loaded
dynamically at runtime and aren't packaged using requirejs.
The optional `copy` array includes file path `globs` to be used when copying
files from `src/` to `dist/` for this extension at build time. Some extensions
have no external dependencies, other than the `main.js` file. If this is the
case, you don't need to include `copy`. However, most have some secondary
resources, including things like stylesheets, images, etc. These need to get
included in the `copy` array.
39 changes: 20 additions & 19 deletions src/utils/BrambleExtensionLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,28 @@ define(function (require, exports, module) {

// Disable any extensions we found on the query string's ?disableExtensions= param
function _processDefaults(disableExtensions) {
var brambleExtensions = extensionInfo.map(function(info) {
return info.path;
});

if(disableExtensions) {
disableExtensions.split(",").forEach(function (ext) {
ext = ext.trim();
var idx = brambleExtensions.indexOf(ext);
if (idx > -1) {
console.log('[Brackets] Disabling default extension `' + ext + '`');
brambleExtensions.splice(idx, 1);
}
disableExtensions = disableExtensions ? disableExtensions.trim().split(/\s*,\s*/) : [];

var brambleExtensions = [];
extensionInfo.forEach(function(info) {
var extPath = info.path;
var extBasename = Path.basename(extPath);

// Skip this extension if we've been instructed to disable via URL.
// Support both 'extensions/default/Autosave' and 'Autosave' forms.
if(disableExtensions.indexOf(extBasename) > -1 ||
disableExtensions.indexOf(extPath) > -1 ) {
console.log("[Bramble] Skipping loading of extension " + extBasename + " at " + extPath);
return;
}

brambleExtensions.push({
name: extPath,
path: Path.join(basePath, extPath)
});
}

return brambleExtensions.map(function (ext) {
return {
name: ext,
path: Path.join(basePath, ext)
};
});

return brambleExtensions;
}

exports.getExtensionList = function(params) {
Expand Down
132 changes: 0 additions & 132 deletions sw-cache-file-list.json

This file was deleted.

0 comments on commit f128c50

Please sign in to comment.