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

Commit d719b5e

Browse files
committed
feat(worker): support onlyWithoutExtension in route definition
1 parent ef69a36 commit d719b5e

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

service-worker/worker/src/plugins/routes/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface RouteMap {
1313

1414
interface RouteConfig {
1515
prefix?: boolean;
16+
onlyWithoutExtension?: boolean;
1617
}
1718

1819
interface RouteRedirectionManifest {
@@ -31,6 +32,11 @@ export class RouteRedirectionImpl implements Plugin<RouteRedirectionImpl> {
3132
return this.worker.manifest['routing'] as RouteRedirectionManifest;
3233
}
3334

35+
private hasExtension(path: string): boolean {
36+
const lastSegment = path.substr(path.lastIndexOf('/') + 1);
37+
return lastSegment.indexOf('.') !== -1;
38+
}
39+
3440
setup(operations: Operation[]): void {
3541
// No setup needed.
3642
}
@@ -47,9 +53,12 @@ export class RouteRedirectionImpl implements Plugin<RouteRedirectionImpl> {
4753
}
4854
const matchesRoutingTable = Object.keys(manifest.routes).some(route => {
4955
const config = manifest.routes[route];
50-
return config.prefix
56+
const matchesPath = config.prefix
5157
? path.indexOf(route) === 0
5258
: path === route;
59+
const matchesPathAndExtension = matchesPath &&
60+
(!config.onlyWithoutExtension || !this.hasExtension(path));
61+
return matchesPathAndExtension;
5362
});
5463
if (matchesRoutingTable) {
5564
ops.unshift(rewriteUrlInstruction(this.worker, req, base + manifest.index));

service-worker/worker/src/test/unit/worker.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ const ROUTING_MANIFEST = JSON.stringify({
3434
routes: {
3535
'/goodbye.txt': {
3636
prefix: false
37+
},
38+
'/prefix': {
39+
prefix: true,
40+
onlyWithoutExtension: true,
3741
}
3842
}
3943
}
@@ -194,20 +198,26 @@ describe('ngsw', () => {
194198
beforeAll(done => {
195199
driver.mockUrl(MANIFEST_URL, ROUTING_MANIFEST);
196200
driver.mockUrl('/hello.txt', 'Hello world!');
197-
driver.mockUrl('/goodbye.txt', 'Should never be fetched!');
198201
driver.startup();
199202
driver
200203
.triggerInstall()
201204
.then(() => driver.triggerActivate())
202205
.then(() => driver.waitForReady())
203206
.then(() => driver.unmockAll())
207+
.then(() => driver.mockUrl('/goodbye.txt', 'Should never be fetched!'))
208+
.then(() => driver.mockUrl('/prefix/test.json', 'Some json'))
204209
.then(done, err => errored(err, done));
205210
});
206211
it('successfully falls back', done => Promise
207212
.resolve(null)
208213
.then(() => expectServed(driver, '/hello.txt', 'Hello world!'))
209214
.then(() => expectServed(driver, '/goodbye.txt', 'Hello world!'))
210215
.then(done, err => errored(err, done)))
216+
it('successfully falls back prefixed routes', done => Promise
217+
.resolve(null)
218+
.then(() => expectServed(driver, '/prefix/test', 'Hello world!'))
219+
.then(() => expectServed(driver, '/prefix/test.json', 'Some json'))
220+
.then(done, err => errored(err, done)));
211221
});;
212222
sequence('index fallback', () => {
213223
let driver: TestWorkerDriver = new TestWorkerDriver(createServiceWorker);

0 commit comments

Comments
 (0)