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
5 changes: 5 additions & 0 deletions .changeset/itchy-roses-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a bug that caused injected endpoint routes to return not found when trailingSlash was set to always
3 changes: 1 addition & 2 deletions packages/astro/src/core/routing/manifest/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ function createInjectedRoutes({ settings, cwd }: CreateRouteManifestParams): Rou
});

const type = resolved.endsWith('.astro') ? 'page' : 'endpoint';
const isPage = type === 'page';
const trailingSlash = isPage ? config.trailingSlash : 'never';
const { trailingSlash } = config;

const pattern = getPattern(segments, settings.config.base, trailingSlash);
const generate = getRouteGenerator(segments, trailingSlash);
Expand Down
61 changes: 61 additions & 0 deletions packages/astro/test/units/routing/trailing-slash.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {

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

describe('trailingSlash', () => {
Expand All @@ -24,6 +25,23 @@ describe('trailingSlash', () => {
trailingSlash: 'always',
output: 'server',
adapter: testAdapter(),
integrations: [
{
name: 'test',
hooks: {
'astro:config:setup': ({ injectRoute }) => {
injectRoute({
pattern: '/injected',
entrypoint: './src/pages/api.ts',
});
injectRoute({
pattern: '/injected.json',
entrypoint: './src/pages/api.ts',
});
},
},
},
],
});
container = await createContainer({
settings,
Expand Down Expand Up @@ -55,4 +73,47 @@ describe('trailingSlash', () => {
assert.equal(html.includes(`<span class="statusMessage">Not found</span>`), true);
assert.equal(res.statusCode, 404);
});

it('should match an injected route when request has a trailing slash', async () => {
const { req, res, text } = createRequestAndResponse({
method: 'GET',
url: '/injected/',
});
container.handle(req, res);
const json = await text();
assert.equal(json, '{"success":true}');
});

it('should NOT match an injected route when request lacks a trailing slash', async () => {
const { req, res, text } = createRequestAndResponse({
method: 'GET',
url: '/injected',
});
container.handle(req, res);
const html = await text();
assert.equal(html.includes(`<span class="statusMessage">Not found</span>`), true);
assert.equal(res.statusCode, 404);
});

it('should match the API route when request has a trailing slash, with a file extension', async () => {
const { req, res, text } = createRequestAndResponse({
method: 'GET',
url: '/dot.json/',
});
container.handle(req, res);
const json = await text();
assert.equal(json, '{"success":true}');
});

it('should NOT match the API route when request lacks a trailing slash, with a file extension', async () => {
const { req, res, text } = createRequestAndResponse({
method: 'GET',
url: '/dot.json',
});
container.handle(req, res);
const html = await text();
assert.equal(html.includes(`<span class="statusMessage">Not found</span>`), true);
assert.equal(res.statusCode, 404);
});

});