mirror of
https://github.com/withastro/astro.git
synced 2025-01-06 22:10:10 -05:00
fix: regression when calculating params and pattern (#9051)
* fix: regression when calculating params and pattern * changeset
This commit is contained in:
parent
fb94d575af
commit
15b84ccb98
6 changed files with 58 additions and 30 deletions
5
.changeset/grumpy-cobras-attend.md
Normal file
5
.changeset/grumpy-cobras-attend.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix a regression where endpoints were incorrectly processed during SSG build when `trailingSlash: "always"`
|
|
@ -31,11 +31,14 @@ export async function callGetStaticPaths({
|
||||||
ssr,
|
ssr,
|
||||||
}: CallGetStaticPathsOptions): Promise<GetStaticPathsResultKeyed> {
|
}: CallGetStaticPathsOptions): Promise<GetStaticPathsResultKeyed> {
|
||||||
const cached = routeCache.get(route);
|
const cached = routeCache.get(route);
|
||||||
if (cached?.staticPaths) return cached.staticPaths;
|
if (!mod) {
|
||||||
|
throw new Error('This is an error caused by Astro and not your code. Please file an issue.');
|
||||||
if (mod) {
|
|
||||||
validateDynamicRouteModule(mod, { ssr, route });
|
|
||||||
}
|
}
|
||||||
|
if (cached?.staticPaths) {
|
||||||
|
return cached.staticPaths;
|
||||||
|
}
|
||||||
|
|
||||||
|
validateDynamicRouteModule(mod, { ssr, route });
|
||||||
|
|
||||||
// No static paths in SSR mode. Return an empty RouteCacheEntry.
|
// No static paths in SSR mode. Return an empty RouteCacheEntry.
|
||||||
if (ssr && !route.prerender) {
|
if (ssr && !route.prerender) {
|
||||||
|
@ -47,12 +50,10 @@ export async function callGetStaticPaths({
|
||||||
let staticPaths: GetStaticPathsResult = [];
|
let staticPaths: GetStaticPathsResult = [];
|
||||||
// Add a check here to make TypeScript happy.
|
// Add a check here to make TypeScript happy.
|
||||||
// This is already checked in validateDynamicRouteModule().
|
// This is already checked in validateDynamicRouteModule().
|
||||||
if (mod) {
|
|
||||||
if (!mod.getStaticPaths) {
|
if (!mod.getStaticPaths) {
|
||||||
throw new Error('Unexpected Error.');
|
throw new Error('Unexpected Error.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mod) {
|
|
||||||
// Calculate your static paths.
|
// Calculate your static paths.
|
||||||
staticPaths = await mod.getStaticPaths({
|
staticPaths = await mod.getStaticPaths({
|
||||||
// Q: Why the cast?
|
// Q: Why the cast?
|
||||||
|
@ -62,8 +63,6 @@ export async function callGetStaticPaths({
|
||||||
throw new AstroError(AstroErrorData.GetStaticPathsRemovedRSSHelper);
|
throw new AstroError(AstroErrorData.GetStaticPathsRemovedRSSHelper);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
validateGetStaticPathsResult(staticPaths, logger, route);
|
validateGetStaticPathsResult(staticPaths, logger, route);
|
||||||
|
|
||||||
|
|
|
@ -60,9 +60,12 @@ function getParts(part: string, file: string) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPattern(segments: RoutePart[][], config: AstroConfig) {
|
function getPattern(
|
||||||
|
segments: RoutePart[][],
|
||||||
|
config: AstroConfig,
|
||||||
|
addTrailingSlash: AstroConfig['trailingSlash']
|
||||||
|
) {
|
||||||
const base = config.base;
|
const base = config.base;
|
||||||
const addTrailingSlash = config.trailingSlash;
|
|
||||||
const pathname = segments
|
const pathname = segments
|
||||||
.map((segment) => {
|
.map((segment) => {
|
||||||
if (segment.length === 1 && segment[0].spread) {
|
if (segment.length === 1 && segment[0].spread) {
|
||||||
|
@ -325,7 +328,7 @@ export function createRouteManifest(
|
||||||
components.push(item.file);
|
components.push(item.file);
|
||||||
const component = item.file;
|
const component = item.file;
|
||||||
const trailingSlash = item.isPage ? settings.config.trailingSlash : 'never';
|
const trailingSlash = item.isPage ? settings.config.trailingSlash : 'never';
|
||||||
const pattern = getPattern(segments, settings.config);
|
const pattern = getPattern(segments, settings.config, trailingSlash);
|
||||||
const generate = getRouteGenerator(segments, trailingSlash);
|
const generate = getRouteGenerator(segments, trailingSlash);
|
||||||
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
|
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
|
||||||
? `/${segments.map((segment) => segment[0].content).join('/')}`
|
? `/${segments.map((segment) => segment[0].content).join('/')}`
|
||||||
|
@ -386,7 +389,7 @@ export function createRouteManifest(
|
||||||
const isPage = type === 'page';
|
const isPage = type === 'page';
|
||||||
const trailingSlash = isPage ? config.trailingSlash : 'never';
|
const trailingSlash = isPage ? config.trailingSlash : 'never';
|
||||||
|
|
||||||
const pattern = getPattern(segments, settings.config);
|
const pattern = getPattern(segments, settings.config, trailingSlash);
|
||||||
const generate = getRouteGenerator(segments, trailingSlash);
|
const generate = getRouteGenerator(segments, trailingSlash);
|
||||||
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
|
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
|
||||||
? `/${segments.map((segment) => segment[0].content).join('/')}`
|
? `/${segments.map((segment) => segment[0].content).join('/')}`
|
||||||
|
@ -433,7 +436,7 @@ export function createRouteManifest(
|
||||||
return getParts(s, from);
|
return getParts(s, from);
|
||||||
});
|
});
|
||||||
|
|
||||||
const pattern = getPattern(segments, settings.config);
|
const pattern = getPattern(segments, settings.config, trailingSlash);
|
||||||
const generate = getRouteGenerator(segments, trailingSlash);
|
const generate = getRouteGenerator(segments, trailingSlash);
|
||||||
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
|
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
|
||||||
? `/${segments.map((segment) => segment[0].content).join('/')}`
|
? `/${segments.map((segment) => segment[0].content).join('/')}`
|
||||||
|
@ -551,7 +554,7 @@ export function createRouteManifest(
|
||||||
pathname,
|
pathname,
|
||||||
route,
|
route,
|
||||||
segments,
|
segments,
|
||||||
pattern: getPattern(segments, config),
|
pattern: getPattern(segments, config, config.trailingSlash),
|
||||||
type: 'fallback',
|
type: 'fallback',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -624,7 +627,7 @@ export function createRouteManifest(
|
||||||
pathname,
|
pathname,
|
||||||
route,
|
route,
|
||||||
segments,
|
segments,
|
||||||
pattern: getPattern(segments, config),
|
pattern: getPattern(segments, config, config.trailingSlash),
|
||||||
type: 'fallback',
|
type: 'fallback',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
15
packages/integrations/sitemap/test/fixtures/static/src/pages/[lang]/manifest.ts
vendored
Normal file
15
packages/integrations/sitemap/test/fixtures/static/src/pages/[lang]/manifest.ts
vendored
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
export const GET: APIRoute = async ({ params }) => {
|
||||||
|
const { lang } = params;
|
||||||
|
|
||||||
|
return new Response(`I'm a route in the "${lang}" language.`);
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function getStaticPaths() {
|
||||||
|
return ['it', 'en'].map((language) => {
|
||||||
|
return {
|
||||||
|
params: {
|
||||||
|
lang: language,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
|
@ -10,6 +10,7 @@ describe('getStaticPaths support', () => {
|
||||||
before(async () => {
|
before(async () => {
|
||||||
fixture = await loadFixture({
|
fixture = await loadFixture({
|
||||||
root: './fixtures/static/',
|
root: './fixtures/static/',
|
||||||
|
trailingSlash: 'always',
|
||||||
});
|
});
|
||||||
await fixture.build();
|
await fixture.build();
|
||||||
|
|
||||||
|
@ -33,4 +34,9 @@ describe('getStaticPaths support', () => {
|
||||||
it('includes numerical pages', () => {
|
it('includes numerical pages', () => {
|
||||||
expect(urls).to.include('http://example.com/123/');
|
expect(urls).to.include('http://example.com/123/');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should render the endpoint', async () => {
|
||||||
|
const page = await fixture.readFile('./it/manifest');
|
||||||
|
expect(page).to.contain('I\'m a route in the "it" language.');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue