From acd8734937ca53a2e9405493c672127fa53f33df Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Mon, 18 Nov 2019 22:32:28 +0200 Subject: [PATCH] fix(service-worker): throw when using the unsupported `versionedFiles` option in config (#33903) In 5d5c94d83, the deprecated `versionedFiles` option from the SW asset-group configuration in `ngsw-config.json`. As a result, the option would be silently ignored and the runtime behavior of the SW would change (i.e. some files might not be cached and available offline any more). This change could be easily go unnoticed by the developer. This commit ensures this does not happen by throwing a build-time error, when detecting the unsupported `versionedFiles` option with an error message prompting the user to use the `files` option instead. Jira issue: [FW-1727](https://angular-team.atlassian.net/browse/FW-1727) PR Close #33903 --- .../service-worker/config/src/generator.ts | 6 ++++ .../config/test/generator_spec.ts | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/packages/service-worker/config/src/generator.ts b/packages/service-worker/config/src/generator.ts index 1445496962..18c70ee273 100644 --- a/packages/service-worker/config/src/generator.ts +++ b/packages/service-worker/config/src/generator.ts @@ -45,6 +45,12 @@ export class Generator { Promise { const seenMap = new Set(); return Promise.all((config.assetGroups || []).map(async(group) => { + if ((group.resources as any).versionedFiles) { + throw new Error( + `Asset-group '${group.name}' in 'ngsw-config.json' uses the 'versionedFiles' option, ` + + 'which is no longer supported. Use \'files\' instead.'); + } + const fileMatcher = globListToMatcher(group.resources.files || []); const allFiles = await this.fs.list('/'); diff --git a/packages/service-worker/config/test/generator_spec.ts b/packages/service-worker/config/test/generator_spec.ts index 546be3a4ae..38b608c502 100644 --- a/packages/service-worker/config/test/generator_spec.ts +++ b/packages/service-worker/config/test/generator_spec.ts @@ -7,6 +7,7 @@ */ import {Generator} from '../src/generator'; +import {AssetGroup} from '../src/in'; import {MockFilesystem} from '../testing/mock'; describe('Generator', () => { @@ -149,4 +150,35 @@ describe('Generator', () => { hashTable: {}, }); }); + + it('throws if the obsolete `versionedFiles` is used', async() => { + const fs = new MockFilesystem({ + '/index.html': 'This is a test', + '/main.js': 'This is a JS file', + }); + const gen = new Generator(fs, '/test'); + + try { + await gen.process({ + index: '/index.html', + assetGroups: [{ + name: 'test', + resources: { + files: [ + '/*.html', + ], + versionedFiles: [ + '/*.js', + ], + } as AssetGroup['resources'] & + {versionedFiles: string[]}, + }], + }); + throw new Error('Processing should have failed due to \'versionedFiles\'.'); + } catch (err) { + expect(err).toEqual(new Error( + 'Asset-group \'test\' in \'ngsw-config.json\' uses the \'versionedFiles\' option, ' + + 'which is no longer supported. Use \'files\' instead.')); + } + }); });