Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 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/full-cloths-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a bug that caused index pages to not be found when a `base` was set with `trailingSlash: never`
2 changes: 1 addition & 1 deletion packages/astro/src/core/routing/manifest/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function getPattern(
const trailing =
addTrailingSlash && segments.length ? getTrailingSlashPattern(addTrailingSlash) : '$';
let initial = '\\/';
if (addTrailingSlash === 'never' && base !== '/') {
if (addTrailingSlash === 'never' && base !== '/' && pathname !== '') {
initial = '';
}
return new RegExp(`^${pathname || initial}${trailing}`);
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/vite-plugin-astro-server/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function baseMiddleware(

if (pathname.startsWith(devRoot)) {
req.url = url.replace(devRoot, devRootReplacement);
req.url ||= '/';
return next();
}

Expand Down
4 changes: 1 addition & 3 deletions packages/astro/test/ssr-trailing-slash.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,7 @@ describe('Redirecting trailing slashes in SSR', () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/mybase');
const response = await app.render(request);
// Should not redirect, but will 404 since we don't have an index page
assert.notEqual(response.status, 301);
assert.notEqual(response.status, 308);
assert.equal(response.status, 200);
});

it('Redirects to remove trailing slash on sub-paths with base', async () => {
Expand Down
7 changes: 4 additions & 3 deletions packages/astro/test/units/routing/manifest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function assertRouteRelations(routes, relations) {
}

describe('routing - createRoutesList', () => {
it('using trailingSlash: "never" does not match the index route when it contains a trailing slash', async () => {
it('using trailingSlash: "never" matches the index route when it contains a trailing slash', async () => {
const fixture = await createFixture({
'/src/pages/index.astro': `<h1>test</h1>`,
});
Expand All @@ -51,8 +51,9 @@ describe('routing - createRoutesList', () => {
settings,
});
const [{ pattern }] = manifest.routes;
assert.equal(pattern.test(''), true);
assert.equal(pattern.test('/'), false);
// Requesting `/` *should* match the index route, because that's what it sees when requesting `/search`.
// If they've requested `/search/` this will have been caught by trailingSlashMiddleware
assert.equal(pattern.test('/'), true);
});

it('endpoint routes are sorted before page routes', async () => {
Expand Down
Loading