Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: endpoints in dev server should never have trailing slash #10216

Closed
wants to merge 1 commit into from
Closed
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/dry-suns-protect.md
Original file line number Diff line number Diff line change
@@ -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"`
3 changes: 2 additions & 1 deletion packages/astro/src/core/routing/manifest/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ function createFileBasedRoutes(
} else {
components.push(item.file);
const component = item.file;
const { trailingSlash } = settings.config;
// Endpoints should NOT 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)
Expand Down
23 changes: 23 additions & 0 deletions packages/astro/test/astro-endpoint.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
6 changes: 6 additions & 0 deletions packages/astro/test/fixtures/astro-endpoint/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from 'astro/config';

// https://astro.build/config
export default defineConfig({
trailingSlash: "always"
});
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/astro-endpoint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/astro-endpoint",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const GET = () => Response.json({ success: true });
Empty file.
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading