Skip to content

Commit

Permalink
fix: dev server should redirect when a page doesn't exist (#7841)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Jul 28, 2023
1 parent 3b42227 commit 2275c7d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-bats-shake.md
@@ -0,0 +1,5 @@
---
'astro': patch
---

Allow to return a redirect in dev mode when the original route is not present in the file system.
26 changes: 19 additions & 7 deletions packages/astro/src/vite-plugin-astro-server/route.ts
Expand Up @@ -31,6 +31,7 @@ import { preload } from './index.js';
import { getComponentMetadata } from './metadata.js';
import { handle404Response, writeSSRResult, writeWebResponse } from './response.js';
import { getScriptsForURL } from './scripts.js';
import { isEndpointResult } from '../core/render/core.js';

const clientLocalsSymbol = Symbol.for('astro.locals');

Expand Down Expand Up @@ -140,7 +141,7 @@ type HandleRoute = {
incomingRequest: http.IncomingMessage;
incomingResponse: http.ServerResponse;
manifest: SSRManifest;
status?: number;
status?: 404 | 500;
};

export async function handleRoute({
Expand Down Expand Up @@ -216,7 +217,7 @@ export async function handleRoute({
const onRequest = options.middleware?.onRequest as MiddlewareResponseHandler | undefined;

const result = await tryRenderRoute(route.type, renderContext, env, mod, onRequest);
if (route.type === 'endpoint' && !(result instanceof Response)) {
if (isEndpointResult(result, route.type)) {
if (result.type === 'response') {
if (result.response.headers.get('X-Astro-Response') === 'Not-Found') {
const fourOhFourRoute = await matchRoute('/404', env, manifestData);
Expand Down Expand Up @@ -254,7 +255,7 @@ export async function handleRoute({
attachToResponse(response, result.cookies);
await writeWebResponse(incomingResponse, response);
}
} else if (result instanceof Response) {
} else {
if (result.status === 404) {
const fourOhFourRoute = await matchRoute('/404', env, manifestData);
return handleRoute({
Expand All @@ -273,13 +274,24 @@ export async function handleRoute({
}

let response = result;
// Response.status is read-only, so a clone is required to override
if (status && response.status !== status) {

if (
// We are in a recursion, and it's possible that this function is called itself with a status code
// By default, the status code passed via parameters is computed by the matched route.
//
// By default, we should give priority to the status code passed, although it's possible that
// the `Response` emitted by the user is a redirect. If so, then return the returned response.
response.status < 400 &&
response.status >= 300
) {
await writeSSRResult(request, response, incomingResponse);
return;
} else if (status && response.status !== status && (status === 404 || status === 500)) {
// Response.status is read-only, so a clone is required to override
response = new Response(result.body, { ...result, status });
}
await writeSSRResult(request, response, incomingResponse);
}
// unreachable
}

interface GetScriptsAndStylesParams {
Expand Down Expand Up @@ -360,7 +372,7 @@ async function getScriptsAndStyles({ env, filePath }: GetScriptsAndStylesParams)
return { scripts, styles, links, metadata };
}

function getStatus(matchedRoute?: MatchedRoute): number | undefined {
function getStatus(matchedRoute?: MatchedRoute): 404 | 500 | undefined {
if (!matchedRoute) return 404;
if (matchedRoute.route.route === '/404') return 404;
if (matchedRoute.route.route === '/500') return 500;
Expand Down

0 comments on commit 2275c7d

Please sign in to comment.