Skip to content

Commit 287ad47

Browse files
authored
fix(routing): urlMatches should reset lastIndex if given /.../g or /.../y (#41470)
global `/.../g` and sticky `/.../y` regular expressions silently adjust `lastIndex` this means that subsequent uses will miss any matches before the new `lastIndex` (until it wraps around) as such, we should reset the `lastIndex` before trying to use it
1 parent ee02af8 commit 287ad47

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

packages/isomorphic/urlMatch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ export function urlMatches(baseURL: string | undefined, urlString: string, match
181181
if (isString(match))
182182
match = new RegExp(resolveGlobToRegexPattern(baseURL, match, webSocketUrl));
183183
if (isRegExp(match)) {
184-
const r = match.test(urlString);
185-
return r;
184+
match.lastIndex = 0;
185+
return match.test(urlString);
186186
}
187187
const url = parseURL(urlString);
188188
if (!url)

tests/page/interception.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,19 @@ it('should work with regular expression passed from a different context', async
299299
expect(intercepted).toBe(true);
300300
});
301301

302+
it('should intercept every request matching a global regexp', async ({ page, server }) => {
303+
await page.goto(server.EMPTY_PAGE);
304+
let intercepted = 0;
305+
await page.route(/\/intercept-me/g, async route => {
306+
++intercepted;
307+
await route.fulfill({ body: 'intercepted' });
308+
});
309+
const url = server.PREFIX + '/intercept-me';
310+
for (let i = 0; i < 3; ++i)
311+
expect(await page.evaluate(u => fetch(u, { cache: 'no-store' }).then(r => r.text()), url)).toBe('intercepted');
312+
expect(intercepted).toBe(3);
313+
});
314+
302315
it('should not break remote worker importScripts', async ({ page, server }) => {
303316
await page.route('**', async route => {
304317
await route.continue();

0 commit comments

Comments
 (0)