Skip to content
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
8 changes: 5 additions & 3 deletions packages/astro/test/units/routing/trailing-slash.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ 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 })`,
'/src/pages/index.ts': `export const GET = ({ request }) =>
Response.json({ pathname: new URL(request.url).pathname });`,
};

describe('trailingSlash', () => {
Expand Down Expand Up @@ -65,7 +67,7 @@ describe('trailingSlash', () => {
'astro:config:setup': ({ injectRoute }) => {
injectRoute({
pattern: '/',
entrypoint: './src/pages/api.ts',
entrypoint: './src/pages/index.ts',
});
injectRoute({
pattern: '/injected',
Expand Down Expand Up @@ -167,8 +169,8 @@ describe('trailingSlash', () => {
url: '/base',
});
baseContainer.handle(req, res);
const json = await text();
assert.equal(json, '{"success":true}');
const { pathname } = JSON.parse(await text());
assert.equal(pathname, '/base');
});

it('should not match root path with trailing slash when base is set and trailingSlash is never', async () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/internal-helpers/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export function collapseDuplicateTrailingSlashes(path: string, trailingSlash: bo
}

export function removeTrailingForwardSlash(path: string) {
return path.endsWith('/') ? path.slice(0, path.length - 1) : path;
if (path.endsWith('/') && path !== '/' && path.lastIndexOf('/') === 0) {
return path.slice(0, -1);
}
return path.length > 1 && path.endsWith('/') ? path.slice(0, -1) : path;
}

export function removeLeadingForwardSlash(path: string) {
Expand Down
Loading