0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

fix: regression when calculating params and pattern (#9051)

* fix: regression when calculating params and pattern

* changeset
This commit is contained in:
Emanuele Stoppa 2023-11-10 13:49:27 +00:00 committed by GitHub
parent fb94d575af
commit 15b84ccb98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 58 additions and 30 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix a regression where endpoints were incorrectly processed during SSG build when `trailingSlash: "always"`

View file

@ -31,11 +31,14 @@ export async function callGetStaticPaths({
ssr,
}: CallGetStaticPathsOptions): Promise<GetStaticPathsResultKeyed> {
const cached = routeCache.get(route);
if (cached?.staticPaths) return cached.staticPaths;
if (mod) {
validateDynamicRouteModule(mod, { ssr, route });
if (!mod) {
throw new Error('This is an error caused by Astro and not your code. Please file an issue.');
}
if (cached?.staticPaths) {
return cached.staticPaths;
}
validateDynamicRouteModule(mod, { ssr, route });
// No static paths in SSR mode. Return an empty RouteCacheEntry.
if (ssr && !route.prerender) {
@ -47,24 +50,20 @@ export async function callGetStaticPaths({
let staticPaths: GetStaticPathsResult = [];
// Add a check here to make TypeScript happy.
// This is already checked in validateDynamicRouteModule().
if (mod) {
if (!mod.getStaticPaths) {
throw new Error('Unexpected Error.');
}
if (mod) {
// Calculate your static paths.
staticPaths = await mod.getStaticPaths({
// Q: Why the cast?
// A: So users downstream can have nicer typings, we have to make some sacrifice in our internal typings, which necessitate a cast here
paginate: generatePaginateFunction(route) as PaginateFunction,
rss() {
throw new AstroError(AstroErrorData.GetStaticPathsRemovedRSSHelper);
},
});
}
if (!mod.getStaticPaths) {
throw new Error('Unexpected Error.');
}
// Calculate your static paths.
staticPaths = await mod.getStaticPaths({
// Q: Why the cast?
// A: So users downstream can have nicer typings, we have to make some sacrifice in our internal typings, which necessitate a cast here
paginate: generatePaginateFunction(route) as PaginateFunction,
rss() {
throw new AstroError(AstroErrorData.GetStaticPathsRemovedRSSHelper);
},
});
validateGetStaticPathsResult(staticPaths, logger, route);
const keyedStaticPaths = staticPaths as GetStaticPathsResultKeyed;

View file

@ -60,9 +60,12 @@ function getParts(part: string, file: string) {
return result;
}
function getPattern(segments: RoutePart[][], config: AstroConfig) {
function getPattern(
segments: RoutePart[][],
config: AstroConfig,
addTrailingSlash: AstroConfig['trailingSlash']
) {
const base = config.base;
const addTrailingSlash = config.trailingSlash;
const pathname = segments
.map((segment) => {
if (segment.length === 1 && segment[0].spread) {
@ -325,7 +328,7 @@ export function createRouteManifest(
components.push(item.file);
const component = item.file;
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 pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
? `/${segments.map((segment) => segment[0].content).join('/')}`
@ -386,7 +389,7 @@ export function createRouteManifest(
const isPage = type === 'page';
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 pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
? `/${segments.map((segment) => segment[0].content).join('/')}`
@ -433,7 +436,7 @@ export function createRouteManifest(
return getParts(s, from);
});
const pattern = getPattern(segments, settings.config);
const pattern = getPattern(segments, settings.config, trailingSlash);
const generate = getRouteGenerator(segments, trailingSlash);
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
? `/${segments.map((segment) => segment[0].content).join('/')}`
@ -551,7 +554,7 @@ export function createRouteManifest(
pathname,
route,
segments,
pattern: getPattern(segments, config),
pattern: getPattern(segments, config, config.trailingSlash),
type: 'fallback',
});
}
@ -624,7 +627,7 @@ export function createRouteManifest(
pathname,
route,
segments,
pattern: getPattern(segments, config),
pattern: getPattern(segments, config, config.trailingSlash),
type: 'fallback',
});
}

View file

@ -4,7 +4,7 @@ import sitemap from '@astrojs/sitemap';
export default defineConfig({
integrations: [sitemap()],
site: 'http://example.com',
redirects: {
'/redirect': '/'
},
redirects: {
'/redirect': '/'
},
})

View 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,
},
};
});
}

View file

@ -10,6 +10,7 @@ describe('getStaticPaths support', () => {
before(async () => {
fixture = await loadFixture({
root: './fixtures/static/',
trailingSlash: 'always',
});
await fixture.build();
@ -33,4 +34,9 @@ describe('getStaticPaths support', () => {
it('includes numerical pages', () => {
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.');
});
});