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
This commit is contained in:
George Kalpakas 2019-11-18 22:32:28 +02:00 committed by Alex Rickabaugh
parent 4d03ec0e85
commit acd8734937
2 changed files with 38 additions and 0 deletions

View File

@ -45,6 +45,12 @@ export class Generator {
Promise<Object[]> {
const seenMap = new Set<string>();
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('/');

View File

@ -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.'));
}
});
});