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: handle requests for double slash in dev #12733

Merged
merged 8 commits into from
Jan 16, 2025
Merged
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/twenty-cherries-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a bug that caused the dev server to return an error if requesting "//"
4 changes: 2 additions & 2 deletions packages/astro/src/core/routing/3xx.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type RedirectTemplate = {
from: string;
from?: string;
location: string | URL;
status: number;
};
Expand All @@ -14,6 +14,6 @@ export function redirectTemplate({ status, location, from }: RedirectTemplate) {
<meta name="robots" content="noindex">
<link rel="canonical" href="${location}">
<body>
<a href="${location}">Redirecting from <code>${from}</code> to <code>${location}</code></a>
<a href="${location}">Redirecting ${from ? `from <code>${from}</code> ` : ''}to <code>${location}</code></a>
</body>`;
}
9 changes: 7 additions & 2 deletions packages/astro/src/vite-plugin-astro-server/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { appendForwardSlash } from '@astrojs/internal-helpers/path';
import { bold } from 'kleur/colors';
import type { Logger } from '../core/logger/core.js';
import notFoundTemplate, { subpathNotUsedTemplate } from '../template/4xx.js';
import { writeHtmlResponse } from './response.js';
import { writeHtmlResponse, writeRedirectResponse } from './response.js';

const manySlashes = /\/{2,}$/;

export function baseMiddleware(
settings: AstroSettings,
Expand All @@ -21,7 +23,10 @@ export function baseMiddleware(

return function devBaseMiddleware(req, res, next) {
const url = req.url!;

if (manySlashes.test(url)) {
const destination = url.replace(manySlashes, '/');
return writeRedirectResponse(res, 301, destination);
}
let pathname: string;
try {
pathname = decodeURI(new URL(url, 'http://localhost').pathname);
Expand Down
12 changes: 12 additions & 0 deletions packages/astro/src/vite-plugin-astro-server/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Readable } from 'node:stream';
import { getSetCookiesFromResponse } from '../core/cookies/index.js';
import { getViteErrorPayload } from '../core/errors/dev/index.js';
import notFoundTemplate from '../template/4xx.js';
import { redirectTemplate } from '../core/routing/3xx.js';

export async function handle404Response(
origin: string,
Expand Down Expand Up @@ -53,6 +54,17 @@ export function writeHtmlResponse(res: http.ServerResponse, statusCode: number,
res.end();
}

export function writeRedirectResponse(res: http.ServerResponse, statusCode: number, location: string) {
const html = redirectTemplate({ status: statusCode, location });
res.writeHead(statusCode, {
Location: location,
'Content-Type': 'text/html',
'Content-Length': Buffer.byteLength(html, 'utf-8'),
});
res.write(html);
res.end();
}

export async function writeWebResponse(res: http.ServerResponse, webResponse: Response) {
const { status, headers, body, statusText } = webResponse;

Expand Down
13 changes: 13 additions & 0 deletions packages/astro/test/dev-routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ describe('Development Routing', () => {
assert.equal(response.status, 200);
});

it('redirects when loading double slash', async () => {
const response = await fixture.fetch('//', { redirect: 'manual' });
assert.equal(response.status, 301);
assert.equal(response.headers.get('Location'), '/');
});

it('redirects when loading multiple slashes', async () => {
const response = await fixture.fetch('/////', { redirect: 'manual' });
assert.equal(response.status, 301);
assert.equal(response.headers.get('Location'), '/');
});


it('404 when loading invalid dynamic route', async () => {
const response = await fixture.fetch('/2');
assert.equal(response.status, 404);
Expand Down
Loading