Skip to content

Commit 23978dd

Browse files
authored
fix: remove special handling for injected endpoint slashes (#13111)
1 parent cbd056c commit 23978dd

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

.changeset/itchy-roses-walk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes a bug that caused injected endpoint routes to return not found when trailingSlash was set to always

packages/astro/src/core/routing/manifest/create.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,7 @@ function createInjectedRoutes({ settings, cwd }: CreateRouteManifestParams): Rou
276276
});
277277

278278
const type = resolved.endsWith('.astro') ? 'page' : 'endpoint';
279-
const isPage = type === 'page';
280-
const trailingSlash = isPage ? config.trailingSlash : 'never';
279+
const { trailingSlash } = config;
281280

282281
const pattern = getPattern(segments, settings.config.base, trailingSlash);
283282
const generate = getRouteGenerator(segments, trailingSlash);

packages/astro/test/units/routing/trailing-slash.test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111

1212
const fileSystem = {
1313
'/src/pages/api.ts': `export const GET = () => Response.json({ success: true })`,
14+
'/src/pages/dot.json.ts': `export const GET = () => Response.json({ success: true })`,
1415
};
1516

1617
describe('trailingSlash', () => {
@@ -24,6 +25,23 @@ describe('trailingSlash', () => {
2425
trailingSlash: 'always',
2526
output: 'server',
2627
adapter: testAdapter(),
28+
integrations: [
29+
{
30+
name: 'test',
31+
hooks: {
32+
'astro:config:setup': ({ injectRoute }) => {
33+
injectRoute({
34+
pattern: '/injected',
35+
entrypoint: './src/pages/api.ts',
36+
});
37+
injectRoute({
38+
pattern: '/injected.json',
39+
entrypoint: './src/pages/api.ts',
40+
});
41+
},
42+
},
43+
},
44+
],
2745
});
2846
container = await createContainer({
2947
settings,
@@ -55,4 +73,47 @@ describe('trailingSlash', () => {
5573
assert.equal(html.includes(`<span class="statusMessage">Not found</span>`), true);
5674
assert.equal(res.statusCode, 404);
5775
});
76+
77+
it('should match an injected route when request has a trailing slash', async () => {
78+
const { req, res, text } = createRequestAndResponse({
79+
method: 'GET',
80+
url: '/injected/',
81+
});
82+
container.handle(req, res);
83+
const json = await text();
84+
assert.equal(json, '{"success":true}');
85+
});
86+
87+
it('should NOT match an injected route when request lacks a trailing slash', async () => {
88+
const { req, res, text } = createRequestAndResponse({
89+
method: 'GET',
90+
url: '/injected',
91+
});
92+
container.handle(req, res);
93+
const html = await text();
94+
assert.equal(html.includes(`<span class="statusMessage">Not found</span>`), true);
95+
assert.equal(res.statusCode, 404);
96+
});
97+
98+
it('should match the API route when request has a trailing slash, with a file extension', async () => {
99+
const { req, res, text } = createRequestAndResponse({
100+
method: 'GET',
101+
url: '/dot.json/',
102+
});
103+
container.handle(req, res);
104+
const json = await text();
105+
assert.equal(json, '{"success":true}');
106+
});
107+
108+
it('should NOT match the API route when request lacks a trailing slash, with a file extension', async () => {
109+
const { req, res, text } = createRequestAndResponse({
110+
method: 'GET',
111+
url: '/dot.json',
112+
});
113+
container.handle(req, res);
114+
const html = await text();
115+
assert.equal(html.includes(`<span class="statusMessage">Not found</span>`), true);
116+
assert.equal(res.statusCode, 404);
117+
});
118+
58119
});

0 commit comments

Comments
 (0)