Skip to content

Commit

Permalink
fix: regression when calculating params and pattern (#9051)
Browse files Browse the repository at this point in the history
* fix: regression when calculating params and pattern

* changeset
  • Loading branch information
ematipico authored Nov 10, 2023
1 parent fb94d57 commit 15b84cc
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-cobras-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix a regression where endpoints were incorrectly processed during SSG build when `trailingSlash: "always"`
39 changes: 19 additions & 20 deletions packages/astro/src/core/render/route-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ 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) {
const entry: GetStaticPathsResultKeyed = Object.assign([], { keyed: new Map() });
Expand All @@ -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;
Expand Down
17 changes: 10 additions & 7 deletions packages/astro/src/core/routing/manifest/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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('/')}`
Expand Down Expand Up @@ -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('/')}`
Expand Down Expand Up @@ -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('/')}`
Expand Down Expand Up @@ -551,7 +554,7 @@ export function createRouteManifest(
pathname,
route,
segments,
pattern: getPattern(segments, config),
pattern: getPattern(segments, config, config.trailingSlash),
type: 'fallback',
});
}
Expand Down Expand Up @@ -624,7 +627,7 @@ export function createRouteManifest(
pathname,
route,
segments,
pattern: getPattern(segments, config),
pattern: getPattern(segments, config, config.trailingSlash),
type: 'fallback',
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import sitemap from '@astrojs/sitemap';
export default defineConfig({
integrations: [sitemap()],
site: 'http://example.com',
redirects: {
'/redirect': '/'
},
redirects: {
'/redirect': '/'
},
})
Original file line number Diff line number Diff line change
@@ -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,
},
};
});
}
6 changes: 6 additions & 0 deletions packages/integrations/sitemap/test/staticPaths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('getStaticPaths support', () => {
before(async () => {
fixture = await loadFixture({
root: './fixtures/static/',
trailingSlash: 'always',
});
await fixture.build();

Expand All @@ -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.');
});
});

0 comments on commit 15b84cc

Please sign in to comment.