diff options
author | 2025-01-31 14:57:18 +0000 | |
---|---|---|
committer | 2025-01-31 14:57:18 +0000 | |
commit | 23978ddfe127bbc3762b6209b42d049588e52a14 (patch) | |
tree | f645a5e28735114ec14c0ffeaeb39d9badfe3e43 | |
parent | cbd056c94ce837e15cf3609a844e85e6459b1560 (diff) | |
download | astro-23978ddfe127bbc3762b6209b42d049588e52a14.tar.gz astro-23978ddfe127bbc3762b6209b42d049588e52a14.tar.zst astro-23978ddfe127bbc3762b6209b42d049588e52a14.zip |
fix: remove special handling for injected endpoint slashes (#13111)
-rw-r--r-- | .changeset/itchy-roses-walk.md | 5 | ||||
-rw-r--r-- | packages/astro/src/core/routing/manifest/create.ts | 3 | ||||
-rw-r--r-- | packages/astro/test/units/routing/trailing-slash.test.js | 61 |
3 files changed, 67 insertions, 2 deletions
diff --git a/.changeset/itchy-roses-walk.md b/.changeset/itchy-roses-walk.md new file mode 100644 index 000000000..361271839 --- /dev/null +++ b/.changeset/itchy-roses-walk.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a bug that caused injected endpoint routes to return not found when trailingSlash was set to always diff --git a/packages/astro/src/core/routing/manifest/create.ts b/packages/astro/src/core/routing/manifest/create.ts index 8fabee6b3..19527851c 100644 --- a/packages/astro/src/core/routing/manifest/create.ts +++ b/packages/astro/src/core/routing/manifest/create.ts @@ -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); diff --git a/packages/astro/test/units/routing/trailing-slash.test.js b/packages/astro/test/units/routing/trailing-slash.test.js index d9ba53b38..bd40e5fbf 100644 --- a/packages/astro/test/units/routing/trailing-slash.test.js +++ b/packages/astro/test/units/routing/trailing-slash.test.js @@ -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', () => { @@ -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, @@ -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); + }); + }); |