refactor(service-worker): move common code into method (#23339)

PR Close #23339
This commit is contained in:
George Kalpakas 2018-04-12 15:03:20 +03:00 committed by Igor Minar
parent d1e716b2cb
commit 1e1c7fd408

View File

@ -52,15 +52,6 @@ export class Generator {
hashTable[joinUrls(this.baseHref, file)] = hash; hashTable[joinUrls(this.baseHref, file)] = hash;
}, Promise.resolve()); }, Promise.resolve());
// Figure out the patterns.
const patterns = (group.resources.urls || [])
.map(
glob => glob.startsWith('/') || glob.indexOf('://') !== -1 ?
glob :
joinUrls(this.baseHref, glob))
.map(glob => globToRegex(glob));
return { return {
name: group.name, name: group.name,
installMode: group.installMode || 'prefetch', installMode: group.installMode || 'prefetch',
@ -69,22 +60,16 @@ export class Generator {
.concat(plainFiles) .concat(plainFiles)
.concat(versionedFiles) .concat(versionedFiles)
.map(url => joinUrls(this.baseHref, url)), .map(url => joinUrls(this.baseHref, url)),
patterns, patterns: (group.resources.urls || []).map(url => urlToRegex(url, this.baseHref)),
}; };
})); }));
} }
private processDataGroups(config: Config): Object[] { private processDataGroups(config: Config): Object[] {
return (config.dataGroups || []).map(group => { return (config.dataGroups || []).map(group => {
const patterns = group.urls
.map(
glob => glob.startsWith('/') || glob.indexOf('://') !== -1 ?
glob :
joinUrls(this.baseHref, glob))
.map(glob => globToRegex(glob));
return { return {
name: group.name, name: group.name,
patterns, patterns: group.urls.map(url => urlToRegex(url, this.baseHref)),
strategy: group.cacheConfig.strategy || 'performance', strategy: group.cacheConfig.strategy || 'performance',
maxSize: group.cacheConfig.maxSize, maxSize: group.cacheConfig.maxSize,
maxAge: parseDurationToMs(group.cacheConfig.maxAge), maxAge: parseDurationToMs(group.cacheConfig.maxAge),
@ -123,6 +108,14 @@ function matches(file: string, patterns: {positive: boolean, regex: RegExp}[]):
return res; return res;
} }
function urlToRegex(url: string, baseHref: string): string {
if (!url.startsWith('/') && url.indexOf('://') === -1) {
url = joinUrls(baseHref, url);
}
return globToRegex(url);
}
function joinUrls(a: string, b: string): string { function joinUrls(a: string, b: string): string {
if (a.endsWith('/') && b.startsWith('/')) { if (a.endsWith('/') && b.startsWith('/')) {
return a + b.substr(1); return a + b.substr(1);
@ -130,4 +123,4 @@ function joinUrls(a: string, b: string): string {
return a + '/' + b; return a + '/' + b;
} }
return a + b; return a + b;
} }