Skip to content
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
9 changes: 9 additions & 0 deletions packages/astro/src/core/routing/params.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { hasFileExtension } from '@astrojs/internal-helpers/path';
import type { GetStaticPathsItem } from '../../types/public/common.js';
import type { AstroConfig } from '../../types/public/index.js';
import type { RouteData } from '../../types/public/internal.js';
Expand All @@ -15,6 +16,14 @@ export function stringifyParams(
route: RouteData,
trailingSlash: AstroConfig['trailingSlash'],
) {
// Endpoint routes with file extensions (e.g. [slug].png.ts) should never
// append a trailing slash, matching the pattern generated in create-manifest.ts
// by trailingSlashForPath(). Without this, the generated path (e.g. /og/foo.png/)
// won't match the route pattern (e.g. /^\/og\/(.*?)\.png$/) and getParams() fails.
if (route.type === 'endpoint' && hasFileExtension(route.route)) {
trailingSlash = 'never';
}

// validate parameter values then stringify each value
const validatedParams: Record<string, string> = {};
for (const [key, value] of Object.entries(params)) {
Expand Down
45 changes: 45 additions & 0 deletions packages/astro/test/units/routing/get-params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import { createComponent, render } from '../../../dist/runtime/server/index.js';
import { getParams } from '../../../dist/core/render/params-and-props.js';
import { stringifyParams } from '../../../dist/core/routing/params.js';
import { dynamicPart, makeRoute, spreadPart, staticPart } from './test-helpers.ts';
import { createTestApp, createPage } from '../mocks.ts';

Expand Down Expand Up @@ -167,6 +168,50 @@ describe('getParams', () => {
});
});

describe('stringifyParams', () => {
it('should not append trailing slash for file extension endpoint routes with trailingSlash always (issue #17306)', () => {
const route = makeRoute({
route: '/og/[...slug].png',
segments: [[staticPart('og')], [spreadPart('...slug'), staticPart('.png')]],
trailingSlash: 'never',
pathname: undefined,
type: 'endpoint',
});

const result = stringifyParams({ slug: '概率论/参数估计' }, route, 'always');
assert.equal(result, '/og/概率论/参数估计.png');
// Verify the generated path matches the route pattern
assert.ok(route.pattern.test(result), 'generated path should match route pattern');
});

it('should not append trailing slash for single dynamic file extension endpoint', () => {
const route = makeRoute({
route: '/api/[name].json',
segments: [[staticPart('api')], [dynamicPart('name'), staticPart('.json')]],
trailingSlash: 'never',
pathname: undefined,
type: 'endpoint',
});

const result = stringifyParams({ name: 'bar' }, route, 'always');
assert.equal(result, '/api/bar.json');
assert.ok(route.pattern.test(result), 'generated path should match route pattern');
});

it('should still append trailing slash for endpoints without file extensions', () => {
const route = makeRoute({
route: '/api/[name]',
segments: [[staticPart('api')], [dynamicPart('name')]],
trailingSlash: 'always',
pathname: undefined,
type: 'endpoint',
});

const result = stringifyParams({ name: 'bar' }, route, 'always');
assert.equal(result, '/api/bar/');
});
});

describe('Params rendered via App', () => {
const paramPage = createComponent((result: any, props: any, slots: any) => {
const Astro = result.createAstro(props, slots);
Expand Down
Loading