Skip to content

Commit

Permalink
fix: endpoints in dev server should never have trailing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Feb 23, 2024
1 parent 7fab7fd commit 6400c29
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 1 deletion.
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 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.

0 comments on commit 6400c29

Please sign in to comment.