diff --git a/packages/service-worker/config/src/generator.ts b/packages/service-worker/config/src/generator.ts index 228ea858ed..399d733e67 100644 --- a/packages/service-worker/config/src/generator.ts +++ b/packages/service-worker/config/src/generator.ts @@ -27,13 +27,15 @@ export class Generator { constructor(readonly fs: Filesystem, private baseHref: string) {} async process(config: Config): Promise { - const hashTable = {}; + const unorderedHashTable = {}; + const assetGroups = await this.processAssetGroups(config, unorderedHashTable); + return { configVersion: 1, appData: config.appData, - index: joinUrls(this.baseHref, config.index), - assetGroups: await this.processAssetGroups(config, hashTable), - dataGroups: this.processDataGroups(config), hashTable, + index: joinUrls(this.baseHref, config.index), assetGroups, + dataGroups: this.processDataGroups(config), + hashTable: withOrderedKeys(unorderedHashTable), navigationUrls: processNavigationUrls(this.baseHref, config.navigationUrls), }; } @@ -45,16 +47,17 @@ export class Generator { const fileMatcher = globListToMatcher(group.resources.files || []); const versionedMatcher = globListToMatcher(group.resources.versionedFiles || []); - const allFiles = (await this.fs.list('/')); - - const versionedFiles = allFiles.filter(versionedMatcher).filter(file => !seenMap.has(file)); - versionedFiles.forEach(file => seenMap.add(file)); + const allFiles = await this.fs.list('/'); const plainFiles = allFiles.filter(fileMatcher).filter(file => !seenMap.has(file)); plainFiles.forEach(file => seenMap.add(file)); + const versionedFiles = allFiles.filter(versionedMatcher).filter(file => !seenMap.has(file)); + versionedFiles.forEach(file => seenMap.add(file)); + // Add the hashes. - await[...versionedFiles, ...plainFiles].reduce(async(previous, file) => { + const matchedFiles = [...plainFiles, ...versionedFiles].sort(); + await matchedFiles.reduce(async(previous, file) => { await previous; const hash = await this.fs.hash(file); hashTable[joinUrls(this.baseHref, file)] = hash; @@ -64,10 +67,7 @@ export class Generator { name: group.name, installMode: group.installMode || 'prefetch', updateMode: group.updateMode || group.installMode || 'prefetch', - urls: ([] as string[]) - .concat(plainFiles) - .concat(versionedFiles) - .map(url => joinUrls(this.baseHref, url)), + urls: matchedFiles.map(url => joinUrls(this.baseHref, url)), patterns: (group.resources.urls || []).map(url => urlToRegex(url, this.baseHref)), }; })); @@ -141,3 +141,9 @@ function joinUrls(a: string, b: string): string { } return a + b; } + +function withOrderedKeys(unorderedObj: T): T { + const orderedObj = {} as T; + Object.keys(unorderedObj).sort().forEach(key => orderedObj[key] = unorderedObj[key]); + return orderedObj; +} diff --git a/packages/service-worker/config/test/generator_spec.ts b/packages/service-worker/config/test/generator_spec.ts index d7252af471..d4fabf98f9 100644 --- a/packages/service-worker/config/test/generator_spec.ts +++ b/packages/service-worker/config/test/generator_spec.ts @@ -74,8 +74,8 @@ import {MockFilesystem} from '../testing/mock'; installMode: 'prefetch', updateMode: 'prefetch', urls: [ - '/test/index.html', '/test/foo/test.html', + '/test/index.html', '/test/test.txt', ], patterns: [ @@ -102,9 +102,9 @@ import {MockFilesystem} from '../testing/mock'; {positive: false, regex: '^http:\\/\\/example\\.com\\/excluded$'}, ], hashTable: { - '/test/test.txt': '18f6f8eb7b1c23d2bb61bff028b83d867a9e4643', + '/test/foo/test.html': '18f6f8eb7b1c23d2bb61bff028b83d867a9e4643', '/test/index.html': 'a54d88e06612d820bc3be72877c74f257b561b19', - '/test/foo/test.html': '18f6f8eb7b1c23d2bb61bff028b83d867a9e4643' + '/test/test.txt': '18f6f8eb7b1c23d2bb61bff028b83d867a9e4643' } }); done();