diff --git a/.changeset/dry-suns-protect.md b/.changeset/dry-suns-protect.md new file mode 100644 index 0000000000000..1204142622e5e --- /dev/null +++ b/.changeset/dry-suns-protect.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Fixes a regression where the dev server was returning a 404 when hitting an endpoint, when the app was configured with `trailingSlash: "always"` diff --git a/packages/astro/src/core/routing/manifest/create.ts b/packages/astro/src/core/routing/manifest/create.ts index 804cacc83fde5..28c842a59f7b9 100644 --- a/packages/astro/src/core/routing/manifest/create.ts +++ b/packages/astro/src/core/routing/manifest/create.ts @@ -383,7 +383,8 @@ function createFileBasedRoutes( } else { components.push(item.file); const component = item.file; - const { trailingSlash } = settings.config; + // Endpoints should have trailing slash + const trailingSlash = item.isPage ? settings.config.trailingSlash : 'never'; const pattern = getPattern(segments, settings.config, trailingSlash); const generate = getRouteGenerator(segments, trailingSlash); const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic) diff --git a/packages/astro/test/astro-endpoint.test.js b/packages/astro/test/astro-endpoint.test.js new file mode 100644 index 0000000000000..b3ef105dc22b7 --- /dev/null +++ b/packages/astro/test/astro-endpoint.test.js @@ -0,0 +1,23 @@ +import { before, it, after, describe } from 'node:test'; +import { loadFixture } from './test-utils.js'; +import assert from 'node:assert/strict'; + +describe('Endpoint in dev, with trailing slash set to always', () => { + let fixture; + let devServer; + + before(async () => { + fixture = await loadFixture({ root: './fixtures/astro-endpoint/' }); + devServer = await fixture.startDevServer(); + }); + + after(async () => { + await devServer.stop(); + }); + + it('should render without issues', async () => { + let result = await fixture.fetch('/file.json'); + + assert.equal(result.status, 200); + }); +}); diff --git a/packages/astro/test/fixtures/astro-endpoint/astro.config.mjs b/packages/astro/test/fixtures/astro-endpoint/astro.config.mjs new file mode 100644 index 0000000000000..8058fa63a39fd --- /dev/null +++ b/packages/astro/test/fixtures/astro-endpoint/astro.config.mjs @@ -0,0 +1,6 @@ +import { defineConfig } from 'astro/config'; + +// https://astro.build/config +export default defineConfig({ + trailingSlash: "always" +}); diff --git a/packages/astro/test/fixtures/astro-endpoint/package.json b/packages/astro/test/fixtures/astro-endpoint/package.json new file mode 100644 index 0000000000000..f57f66e1f6955 --- /dev/null +++ b/packages/astro/test/fixtures/astro-endpoint/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/astro-endpoint", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/astro-endpoint/src/pages/file.json.js b/packages/astro/test/fixtures/astro-endpoint/src/pages/file.json.js new file mode 100644 index 0000000000000..03f1de5cf3ddb --- /dev/null +++ b/packages/astro/test/fixtures/astro-endpoint/src/pages/file.json.js @@ -0,0 +1 @@ +export const GET = () => Response.json({ success: true }); diff --git a/packages/astro/test/fixtures/astro-endpoint/src/pages/index.astro b/packages/astro/test/fixtures/astro-endpoint/src/pages/index.astro new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1cd930ed3b79c..4ff3a2725bf2d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2006,6 +2006,12 @@ importers: specifier: ^4.2.5 version: 4.2.8 + packages/astro/test/fixtures/astro-endpoint: + dependencies: + astro: + specifier: workspace:* + version: link:../../.. + packages/astro/test/fixtures/astro-envs: dependencies: '@astrojs/vue':