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: fix route mismatching #7319

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions .changeset/witty-grapes-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': major
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
---

Regex sometimes fails to match. I splice the complete path and match it to ensure that it is completely correct.
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 7 additions & 1 deletion packages/astro/src/core/render/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getParams } from '../routing/params.js';
import type { RenderContext } from './context.js';
import type { Environment } from './environment.js';
import { createResult } from './result.js';
import { callGetStaticPaths, findPathItemByKey, RouteCache } from './route-cache.js';
import { callGetStaticPaths, findPathItemByKey, RouteCache, isEqualRoute } from './route-cache.js';

interface GetParamsAndPropsOptions {
mod: ComponentInstance;
Expand Down Expand Up @@ -89,6 +89,12 @@ export async function getParamsAndProps(
routeCacheEntry = await callGetStaticPaths({ mod, route, isValidate: true, logging, ssr });
routeCache.set(route, routeCacheEntry);
}
// related to https://github.com/withastro/astro/issues/6968
const state = isEqualRoute(routeCacheEntry.staticPaths, route, pathname);
if (state !== undefined) {
params = state;
}

const matchedStaticPath = findPathItemByKey(routeCacheEntry.staticPaths, params, route);
if (!matchedStaticPath && (ssr ? route.prerender : true)) {
return GetParamsAndPropsError.NoMatchingStaticPath;
Expand Down
45 changes: 45 additions & 0 deletions packages/astro/src/core/render/route-cache.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { object } from 'zod';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import should be removed

import type {
ComponentInstance,
GetStaticPathsItem,
Expand Down Expand Up @@ -126,3 +127,47 @@ export function findPathItemByKey(
}
debug('findPathItemByKey', `Unexpected cache miss looking for ${paramsKey}`);
}

// the regular expression to match it such as 'example-[foo]-[bar]' and {params: {foo: 'hell-no', bar: 'world2'}}
// I spliced the routing and then compared
export function isEqualRoute(
staticPaths: GetStaticPathsResultKeyed,
route: RouteData,
pathname: string
): Params | undefined {
for (let index = 0; index < staticPaths.length; index++) {
const arg = staticPaths[index];
let substr = '';
if (arg.params) {
route?.segments.forEach((segs) => {
substr += '/';
segs?.forEach((seg) => {
if (!seg.dynamic) {
substr += seg.content;
} else {
// such as ...slug
if (seg.content.startsWith('...')) {
substr += arg.params[seg.content.slice(3)];
} else {
substr += arg.params[seg.content];
}
}
});
});
if (pathname.endsWith(substr)) {
const params = Object.keys(arg.params as Params);
// related to routing-priority.test.js /empty-slug/undefined
// if the params is undefined, it will be 'undefined'
return params.reduce((obj, key: string) => {
if (arg.params?.[key] === undefined) {
obj[key] = 'undefined';
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
} else {
obj[key] = arg.params[key] as string;
}
return obj;
}, {} as Params);
}
}
}
return;
}
4 changes: 4 additions & 0 deletions packages/astro/test/astro-get-static-paths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,8 @@ describe('getStaticPaths - dev calls', () => {
);
}
});
it('Validation regularity loopholes', async () => {
let res = await fixture.fetch(`/test/example-hell-no-world2`);
expect(res.status).to.equal(200);
})
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
export function getStaticPaths() {
return [
{params: {foo: 'hell-no', bar: 'world2'}}
];
}
---
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width" />
<title>Astro</title>
</head>
<body>
<h1>Reproduction page</h1>
<p>
11
</p>
</body>
</html>