Skip to content
This repository was archived by the owner on Oct 12, 2021. It is now read-only.

Commit 025cec2

Browse files
committed
fix(worker): add base href support to the webpack plugin
1 parent 6ce55ff commit 025cec2

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

service-worker/worker/gulpfile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ gulp.task('task:webpack_test:pack', done => {
132132
new CopyWebpackPlugin([
133133
{from: 'ngsw-manifest.json'},
134134
]),
135-
new AngularServiceWorkerPlugin(),
135+
new AngularServiceWorkerPlugin({baseHref: '/test'}),
136136
]
137137
}, () => done())
138138
});

service-worker/worker/src/build/webpack.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
declare var require, Buffer;
22
const crypto = require('crypto');
33

4+
export interface SwPluginConfig {
5+
manifestFile?: string;
6+
manifestKey?: string;
7+
baseHref?: string;
8+
}
9+
410
/**
511
* Webpack plugin that generates a basic Angular service worker manifest.
612
*/
713
export class AngularServiceWorkerPlugin {
814

9-
constructor(public manifestFile = 'ngsw-manifest.json', public manifestKey = 'static') {}
15+
public manifestFile: string;
16+
public manifestKey: string;
17+
public baseHref: string;
18+
19+
constructor(config?: SwPluginConfig) {
20+
this.manifestFile = (config && config.manifestFile) || 'ngsw-manifest.json';
21+
this.manifestKey = (config && config.manifestKey) || 'static';
22+
this.baseHref = (config && config.baseHref) || '/';
23+
if (!this.baseHref.endsWith('/')) {
24+
this.baseHref += '/';
25+
}
26+
}
1027

1128
apply(compiler) {
1229
// Determine the URL prefix under which all files will be served.
13-
let publicPrefix = compiler.options.output.publicPath || '';
1430
compiler.plugin('emit', (compilation, callback) => {
1531
// Manifest into which assets to be fetched will be recorded. This will either
1632
// be read from the existing template or created fresh.
@@ -40,7 +56,7 @@ export class AngularServiceWorkerPlugin {
4056
.keys(compilation.assets)
4157
.filter(key => key !== this.manifestFile)
4258
.forEach(key => {
43-
let url = `${publicPrefix}/${key}`;
59+
let url = `${this.baseHref}${key}`;
4460
urls[url] = sha1(compilation.assets[key].source());
4561
});
4662

0 commit comments

Comments
 (0)