0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-20 22:12:38 -05:00

improve preview reliability (#2739)

This commit is contained in:
Fred K. Schott 2022-03-09 10:56:22 -08:00 committed by GitHub
parent b1827a3cf2
commit 0077b4c5a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 18 deletions

View file

@ -30,7 +30,10 @@ export default async function preview(config: AstroConfig, { logging }: PreviewO
const trailingSlash = config.devOptions.trailingSlash const trailingSlash = config.devOptions.trailingSlash
/** Base request URL. */ /** Base request URL. */
let baseURL = new URL(config.buildOptions.site || '/', defaultOrigin); let baseURL = new URL(config.buildOptions.site || '/', defaultOrigin);
const staticFileServer = sirv(fileURLToPath(config.dist), {
etag: true,
maxAge: 0,
})
// Create the preview server, send static files out of the `dist/` directory. // Create the preview server, send static files out of the `dist/` directory.
const server = http.createServer((req, res) => { const server = http.createServer((req, res) => {
const requestURL = new URL(req.url as string, defaultOrigin); const requestURL = new URL(req.url as string, defaultOrigin);
@ -48,28 +51,22 @@ export default async function preview(config: AstroConfig, { logging }: PreviewO
const isRoot = pathname === '/'; const isRoot = pathname === '/';
const hasTrailingSlash = isRoot || pathname.endsWith('/'); const hasTrailingSlash = isRoot || pathname.endsWith('/');
function err(message: string) { function sendError(message: string) {
res.statusCode = 404; res.statusCode = 404;
res.end(notFoundTemplate(pathname, message)); res.end(notFoundTemplate(pathname, message));
}; };
switch(true) { switch (true) {
case hasTrailingSlash && trailingSlash == 'never' && !isRoot: case hasTrailingSlash && trailingSlash == 'never' && !isRoot:
err('Prohibited trailing slash'); sendError('Not Found (devOptions.trailingSlash is set to "never")');
break; return;
case !hasTrailingSlash && trailingSlash == 'always' && !isRoot:
err('Required trailing slash');
break;
default: { default: {
// HACK: rewrite req.url so that sirv finds the file // HACK: rewrite req.url so that sirv finds the file
req.url = '/' + req.url?.replace(baseURL.pathname,'') req.url = '/' + req.url?.replace(baseURL.pathname, '')
sirv(fileURLToPath(config.dist), { staticFileServer(req, res, () => sendError('Not Found'));
maxAge: 0, return;
onNoMatch: () => { }
err('Path not found') }
}
})(req,res)
}}
}); });
let { hostname, port } = config.devOptions; let { hostname, port } = config.devOptions;
@ -122,7 +119,9 @@ export default async function preview(config: AstroConfig, { logging }: PreviewO
port, port,
server: httpServer!, server: httpServer!,
stop: async () => { stop: async () => {
httpServer.close(); await new Promise((resolve, reject) => {
httpServer.close((err) => err ? reject(err) : resolve(undefined));
});
}, },
}; };
} }

View file

@ -25,7 +25,7 @@ describe('CSS', function () {
// test HTML and CSS contents for accuracy // test HTML and CSS contents for accuracy
describe('build', () => { describe('build', () => {
this.timeout(30000); // test needs a little more time in CI this.timeout(45000); // test needs a little more time in CI
let $; let $;
let bundledCSS; let bundledCSS;