Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nextjs parallel routes with catchall isn't supported #69

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vercel/speed-insights",
"version": "1.0.10",
"version": "1.0.11",
"description": "Speed Insights is a tool for measuring web performance and providing suggestions for improvement.",
"keywords": [
"speed-insights",
Expand Down
22 changes: 21 additions & 1 deletion packages/web/src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,21 @@ describe('utils', () => {
const input = '/en/us/next-site';
const params = {
langs: ['en', 'us'],
teamSlug: 'vercel',
};
const expected = '/[...langs]/next-site';
expect(computeRoute(input, params)).toBe(expected);
});

it('handles array segments and individual segments', () => {
const input = '/en/us/next-site';
const params = {
langs: ['en', 'us'],
team: 'next-site',
};
const expected = '/[...langs]/[team]';
expect(computeRoute(input, params)).toBe(expected);
});

it('handles special characters in url', () => {
const input = '/123/test(test';
const params = {
Expand All @@ -73,6 +82,17 @@ describe('utils', () => {
expect(computeRoute(input, params)).toBe(expected);
});

it('parallel routes where params matched both individually and within arrays', () => {
const params = {
catchAll: ['m', 'john', 'p', 'shirt'],
merchantId: 'john',
productSlug: 'shirt',
};
expect(computeRoute('/m/john/p/shirt', params)).toBe(
'/m/[merchantId]/p/[productSlug]',
);
});

describe('edge case handling (same values for multiple params)', () => {
it('replaces based on the priority of the pathParams keys', () => {
const input = '/test/test';
Expand Down
31 changes: 21 additions & 10 deletions packages/web/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,36 @@ export function computeRoute(
}

let result = pathname;

try {
for (const [key, valueOrArray] of Object.entries(pathParams)) {
const isValueArray = Array.isArray(valueOrArray);
const value = isValueArray ? valueOrArray.join('/') : valueOrArray;
const expr = isValueArray ? `...${key}` : key;

const matcher = new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);
if (matcher.test(result)) {
result = result.replace(matcher, `/[${expr}]`);
const entries = Object.entries(pathParams);
// simple keys must be handled first
for (const [key, value] of entries) {
if (!Array.isArray(value)) {
const matcher = turnValueToRegExp(value);
if (matcher.test(result)) {
result = result.replace(matcher, `/[${key}]`);
}
}
}
// array values next
for (const [key, value] of entries) {
if (Array.isArray(value)) {
const matcher = turnValueToRegExp(value.join('/'));
if (matcher.test(result)) {
result = result.replace(matcher, `/[...${key}]`);
}
}
}

return result;
} catch (e) {
return pathname;
}
}

function turnValueToRegExp(value: string): RegExp {
return new RegExp(`/${escapeRegExp(value)}(?=[/?#]|$)`);
}

function escapeRegExp(string: string): string {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
Loading