From 9a45076a3257b5a327c6f067fc1d7867900ab898 Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Wed, 3 Jan 2024 19:53:45 +0000 Subject: [PATCH 1/4] fix(routing): applies trailingSlash on endpoints --- packages/astro/src/core/routing/manifest/create.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/core/routing/manifest/create.ts b/packages/astro/src/core/routing/manifest/create.ts index f87d5fcd2511..247a01ad1896 100644 --- a/packages/astro/src/core/routing/manifest/create.ts +++ b/packages/astro/src/core/routing/manifest/create.ts @@ -328,7 +328,7 @@ export function createRouteManifest( } else { components.push(item.file); const component = item.file; - const trailingSlash = item.isPage ? settings.config.trailingSlash : 'never'; + const { trailingSlash } = 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) From b2490819f97e1f0cb3ab5b051f984d56733303c5 Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Wed, 3 Jan 2024 19:54:58 +0000 Subject: [PATCH 2/4] add changeset --- .changeset/funny-lobsters-promise.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/funny-lobsters-promise.md diff --git a/.changeset/funny-lobsters-promise.md b/.changeset/funny-lobsters-promise.md new file mode 100644 index 000000000000..e6621d0f1c7d --- /dev/null +++ b/.changeset/funny-lobsters-promise.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Fixes an issue where configuring trailingSlash had no effect on how API routes. From e0b4079637675d838e179b547099c6c1063fed8d Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Wed, 3 Jan 2024 20:09:36 +0000 Subject: [PATCH 3/4] add test --- .../test/units/routing/trailing-slash.test.js | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 packages/astro/test/units/routing/trailing-slash.test.js diff --git a/packages/astro/test/units/routing/trailing-slash.test.js b/packages/astro/test/units/routing/trailing-slash.test.js new file mode 100644 index 000000000000..c707c7215a5d --- /dev/null +++ b/packages/astro/test/units/routing/trailing-slash.test.js @@ -0,0 +1,59 @@ +import { + createBasicSettings, + createFs, + createRequestAndResponse, + defaultLogger, +} from '../test-utils.js'; +import { fileURLToPath } from 'node:url'; +import { expect } from 'chai'; +import { createContainer } from '../../../dist/core/dev/container.js'; +import testAdapter from '../../test-adapter.js'; + +const root = new URL('../../fixtures/api-routes/', import.meta.url); +const fileSystem = { + '/src/pages/api.ts': `export const GET = () => Response.json({ success: true })`, +}; + +describe('trailingSlash', () => { + let container; + let settings; + + before(async () => { + const fs = createFs(fileSystem, root); + settings = await createBasicSettings({ + root: fileURLToPath(root), + trailingSlash: 'always', + output: 'server', + adapter: testAdapter(), + }); + container = await createContainer({ + fs, + settings, + logger: defaultLogger, + }); + }); + + after(async () => { + await container.close(); + }); + + it('should match the API route when request has a trailing slash', async () => { + const { req, res, text } = createRequestAndResponse({ + method: 'GET', + url: '/api/', + }); + container.handle(req, res); + const json = await text(); + expect(json).to.equal('{"success":true}'); + }); + + it('should NOT match the API route when request lacks a trailing slash', async () => { + const { req, res, text } = createRequestAndResponse({ + method: 'GET', + url: '/api', + }); + container.handle(req, res); + expect(await text()).to.equal(''); + expect(res.statusCode).to.equal(404); + }); +}); From e0374e8cf1f3dbdc31368e17989025db901c7ce7 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Thu, 4 Jan 2024 10:59:23 +0000 Subject: [PATCH 4/4] Update .changeset/funny-lobsters-promise.md Co-authored-by: Arsh <69170106+lilnasy@users.noreply.github.com> --- .changeset/funny-lobsters-promise.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/funny-lobsters-promise.md b/.changeset/funny-lobsters-promise.md index e6621d0f1c7d..6b77e442ccbe 100644 --- a/.changeset/funny-lobsters-promise.md +++ b/.changeset/funny-lobsters-promise.md @@ -2,4 +2,4 @@ "astro": patch --- -Fixes an issue where configuring trailingSlash had no effect on how API routes. +Fixes an issue where configuring trailingSlash had no effect on API routes.