feat(service-worker): add support for ?
in SW config globbing (#24105)
The globbing is used in the following sections: - `assetGroups` > `resources` > `files`/`versionedFiles` - `assetGroups` > `resources` > `urls` - `dataGroups` > `urls` - `navigationUrls` Query params are ignored for `files`/`versionedFiles` and `navigationUrls`, but they are still taken into account for `assetGroups`/`dataGroups` `urls`. To avoid a breaking change, `?` is matched literally for these patterns. PR Close #24105
This commit is contained in:

committed by
Miško Hevery

parent
94076c934c
commit
250527ca68
@ -75,7 +75,7 @@ export class Generator {
|
||||
installMode: group.installMode || 'prefetch',
|
||||
updateMode: group.updateMode || group.installMode || 'prefetch',
|
||||
urls: matchedFiles.map(url => joinUrls(this.baseHref, url)),
|
||||
patterns: (group.resources.urls || []).map(url => urlToRegex(url, this.baseHref)),
|
||||
patterns: (group.resources.urls || []).map(url => urlToRegex(url, this.baseHref, true)),
|
||||
};
|
||||
}));
|
||||
}
|
||||
@ -84,7 +84,7 @@ export class Generator {
|
||||
return (config.dataGroups || []).map(group => {
|
||||
return {
|
||||
name: group.name,
|
||||
patterns: group.urls.map(url => urlToRegex(url, this.baseHref)),
|
||||
patterns: group.urls.map(url => urlToRegex(url, this.baseHref, true)),
|
||||
strategy: group.cacheConfig.strategy || 'performance',
|
||||
maxSize: group.cacheConfig.maxSize,
|
||||
maxAge: parseDurationToMs(group.cacheConfig.maxAge),
|
||||
@ -132,12 +132,12 @@ function matches(file: string, patterns: {positive: boolean, regex: RegExp}[]):
|
||||
return res;
|
||||
}
|
||||
|
||||
function urlToRegex(url: string, baseHref: string): string {
|
||||
function urlToRegex(url: string, baseHref: string, literalQuestionMark?: boolean): string {
|
||||
if (!url.startsWith('/') && url.indexOf('://') === -1) {
|
||||
url = joinUrls(baseHref, url);
|
||||
}
|
||||
|
||||
return globToRegex(url);
|
||||
return globToRegex(url, literalQuestionMark);
|
||||
}
|
||||
|
||||
function joinUrls(a: string, b: string): string {
|
||||
|
@ -6,17 +6,26 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
const WILD_SINGLE = '[^\\/]*';
|
||||
const QUESTION_MARK = '[^/]';
|
||||
const WILD_SINGLE = '[^/]*';
|
||||
const WILD_OPEN = '(?:.+\\/)?';
|
||||
|
||||
const TO_ESCAPE = [
|
||||
const TO_ESCAPE_BASE = [
|
||||
{replace: /\./g, with: '\\.'},
|
||||
{replace: /\?/g, with: '\\?'},
|
||||
{replace: /\+/g, with: '\\+'},
|
||||
{replace: /\*/g, with: WILD_SINGLE},
|
||||
];
|
||||
const TO_ESCAPE_WILDCARD_QM = [
|
||||
...TO_ESCAPE_BASE,
|
||||
{replace: /\?/g, with: QUESTION_MARK},
|
||||
];
|
||||
const TO_ESCAPE_LITERAL_QM = [
|
||||
...TO_ESCAPE_BASE,
|
||||
{replace: /\?/g, with: '\\?'},
|
||||
];
|
||||
|
||||
export function globToRegex(glob: string): string {
|
||||
export function globToRegex(glob: string, literalQuestionMark = false): string {
|
||||
const toEscape = literalQuestionMark ? TO_ESCAPE_LITERAL_QM : TO_ESCAPE_WILDCARD_QM;
|
||||
const segments = glob.split('/').reverse();
|
||||
let regex: string = '';
|
||||
while (segments.length > 0) {
|
||||
@ -28,7 +37,7 @@ export function globToRegex(glob: string): string {
|
||||
regex += '.*';
|
||||
}
|
||||
} else {
|
||||
const processed = TO_ESCAPE.reduce(
|
||||
const processed = toEscape.reduce(
|
||||
(segment, escape) => segment.replace(escape.replace, escape.with), segment);
|
||||
regex += processed;
|
||||
if (segments.length > 0) {
|
||||
|
Reference in New Issue
Block a user