Skip to content

Commit

Permalink
fix(mobile): prevent already-bundled JS from getting cached by Servic…
Browse files Browse the repository at this point in the history
…e Worker

This change excludes all JS files, except for the production-ready bundle,
from the tree that the ServiceWorkerPlugin receives.
  • Loading branch information
jeffbcross committed May 17, 2016
1 parent 7ab7d72 commit 9d18f74
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions lib/broccoli/angular2-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,6 @@ class Angular2App extends BroccoliPlugin {
merged = this._getBundleTree(merged);
}

if (this.ngConfig.apps[0].mobile) {
var ServiceWorkerPlugin = require('@angular/service-worker').ServiceWorkerPlugin;
var swTree = new ServiceWorkerPlugin(merged);
merged = BroccoliMergeTrees([merged, swTree], {
overwrite: true
});
}

return new BroccoliFunnel(merged, {
destDir: this._destDir,
overwrite: true
Expand Down Expand Up @@ -416,6 +408,7 @@ class Angular2App extends BroccoliPlugin {
var indexContent = fs.readFileSync(indexFile, 'utf8');
var scriptTagVendorFiles = indexContent.match(/vendor\/[^"']*\.js/gi);
var vendorTree = this._getVendorNpmTree();
var assetsTree = this._getAssetsTree();

var scriptTree = new BroccoliFunnel(preBundleTree, {
include: scriptTagVendorFiles
Expand Down Expand Up @@ -452,9 +445,28 @@ class Angular2App extends BroccoliPlugin {
bundleTree = uglify(bundleTree, {
mangle: false
});
}


// Required here since the package isn't installed for non-mobile apps.
var ServiceWorkerPlugin = require('@angular/service-worker').ServiceWorkerPlugin;
// worker.js is needed so it can be copied to dist
var workerJsTree = new BroccoliFunnel(jsTree, {
include: ['vendor/@angular/service-worker/dist/worker.js']
});
/**
* ServiceWorkerPlugin will automatically pre-fetch and cache every file
* in the tree it receives, so it should only receive the app bundle,
* and non-JS static files from the app. The plugin also needs to have
* the worker.js file available so it can copy it to dist.
**/
var swTree = new ServiceWorkerPlugin(BroccoliMergeTrees([
bundleTree,
assetsTree,
workerJsTree
]));
bundleTree = BroccoliMergeTrees([bundleTree, swTree], {
overwrite: true
});
}

return BroccoliMergeTrees([nonJsTree, scriptTree, bundleTree], { overwrite: true });
}
Expand Down

0 comments on commit 9d18f74

Please sign in to comment.