Skip to content

Commit 9d18f74

Browse files
committed
fix(mobile): prevent already-bundled JS from getting cached by Service Worker
This change excludes all JS files, except for the production-ready bundle, from the tree that the ServiceWorkerPlugin receives.
1 parent 7ab7d72 commit 9d18f74

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

lib/broccoli/angular2-app.js

+22-10
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,6 @@ class Angular2App extends BroccoliPlugin {
159159
merged = this._getBundleTree(merged);
160160
}
161161

162-
if (this.ngConfig.apps[0].mobile) {
163-
var ServiceWorkerPlugin = require('@angular/service-worker').ServiceWorkerPlugin;
164-
var swTree = new ServiceWorkerPlugin(merged);
165-
merged = BroccoliMergeTrees([merged, swTree], {
166-
overwrite: true
167-
});
168-
}
169-
170162
return new BroccoliFunnel(merged, {
171163
destDir: this._destDir,
172164
overwrite: true
@@ -416,6 +408,7 @@ class Angular2App extends BroccoliPlugin {
416408
var indexContent = fs.readFileSync(indexFile, 'utf8');
417409
var scriptTagVendorFiles = indexContent.match(/vendor\/[^"']*\.js/gi);
418410
var vendorTree = this._getVendorNpmTree();
411+
var assetsTree = this._getAssetsTree();
419412

420413
var scriptTree = new BroccoliFunnel(preBundleTree, {
421414
include: scriptTagVendorFiles
@@ -452,9 +445,28 @@ class Angular2App extends BroccoliPlugin {
452445
bundleTree = uglify(bundleTree, {
453446
mangle: false
454447
});
455-
}
456-
457448

449+
// Required here since the package isn't installed for non-mobile apps.
450+
var ServiceWorkerPlugin = require('@angular/service-worker').ServiceWorkerPlugin;
451+
// worker.js is needed so it can be copied to dist
452+
var workerJsTree = new BroccoliFunnel(jsTree, {
453+
include: ['vendor/@angular/service-worker/dist/worker.js']
454+
});
455+
/**
456+
* ServiceWorkerPlugin will automatically pre-fetch and cache every file
457+
* in the tree it receives, so it should only receive the app bundle,
458+
* and non-JS static files from the app. The plugin also needs to have
459+
* the worker.js file available so it can copy it to dist.
460+
**/
461+
var swTree = new ServiceWorkerPlugin(BroccoliMergeTrees([
462+
bundleTree,
463+
assetsTree,
464+
workerJsTree
465+
]));
466+
bundleTree = BroccoliMergeTrees([bundleTree, swTree], {
467+
overwrite: true
468+
});
469+
}
458470

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

0 commit comments

Comments
 (0)