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:
parent
b1827a3cf2
commit
0077b4c5a4
2 changed files with 17 additions and 18 deletions
|
@ -30,7 +30,10 @@ export default async function preview(config: AstroConfig, { logging }: PreviewO
|
|||
const trailingSlash = config.devOptions.trailingSlash
|
||||
/** Base request URL. */
|
||||
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.
|
||||
const server = http.createServer((req, res) => {
|
||||
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 hasTrailingSlash = isRoot || pathname.endsWith('/');
|
||||
|
||||
function err(message: string) {
|
||||
function sendError(message: string) {
|
||||
res.statusCode = 404;
|
||||
res.end(notFoundTemplate(pathname, message));
|
||||
};
|
||||
|
||||
switch(true) {
|
||||
switch (true) {
|
||||
case hasTrailingSlash && trailingSlash == 'never' && !isRoot:
|
||||
err('Prohibited trailing slash');
|
||||
break;
|
||||
case !hasTrailingSlash && trailingSlash == 'always' && !isRoot:
|
||||
err('Required trailing slash');
|
||||
break;
|
||||
sendError('Not Found (devOptions.trailingSlash is set to "never")');
|
||||
return;
|
||||
default: {
|
||||
// HACK: rewrite req.url so that sirv finds the file
|
||||
req.url = '/' + req.url?.replace(baseURL.pathname,'')
|
||||
sirv(fileURLToPath(config.dist), {
|
||||
maxAge: 0,
|
||||
onNoMatch: () => {
|
||||
err('Path not found')
|
||||
}
|
||||
})(req,res)
|
||||
}}
|
||||
req.url = '/' + req.url?.replace(baseURL.pathname, '')
|
||||
staticFileServer(req, res, () => sendError('Not Found'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let { hostname, port } = config.devOptions;
|
||||
|
@ -122,7 +119,9 @@ export default async function preview(config: AstroConfig, { logging }: PreviewO
|
|||
port,
|
||||
server: httpServer!,
|
||||
stop: async () => {
|
||||
httpServer.close();
|
||||
await new Promise((resolve, reject) => {
|
||||
httpServer.close((err) => err ? reject(err) : resolve(undefined));
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ describe('CSS', function () {
|
|||
|
||||
// test HTML and CSS contents for accuracy
|
||||
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 bundledCSS;
|
||||
|
|
Loading…
Add table
Reference in a new issue