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: astro preview does not serve custom 404 (4113) #4189

Merged
merged 3 commits into from
Aug 8, 2022
Merged
Changes from 2 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
14 changes: 13 additions & 1 deletion packages/astro/src/core/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { AddressInfo } from 'net';
import type { AstroConfig } from '../../@types/astro';
import type { LogOptions } from '../logger/core';

import fs from 'fs';
import http from 'http';
import { performance } from 'perf_hooks';
import sirv from 'sirv';
Expand Down Expand Up @@ -77,7 +78,18 @@ export default async function preview(
default: {
// HACK: rewrite req.url so that sirv finds the file
req.url = '/' + req.url?.replace(baseURL.pathname, '');
staticFileServer(req, res, () => sendError('Not Found'));
staticFileServer(req, res, () => {
const errorPagePath = fileURLToPath(config.outDir + '/404.html');
if (fs.existsSync(errorPagePath)) {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/html;charset=utf-8');
res.end(fs.readFileSync(errorPagePath));
} else {
staticFileServer(req, res, () => {
sendError('Not Found');
});
}
});
return;
}
}
Expand Down