* fix(#2981): keeps server alive * chore: adding changeset * keep alive until the 'close' or 'error' event fires
This commit is contained in:
parent
ba90ef53d3
commit
9724d844b3
3 changed files with 17 additions and 1 deletions
5
.changeset/big-yaks-invite.md
Normal file
5
.changeset/big-yaks-invite.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix bug causing `astro preview` server to close immediately
|
|
@ -138,7 +138,8 @@ export async function cli(args: string[]) {
|
||||||
|
|
||||||
case 'preview': {
|
case 'preview': {
|
||||||
try {
|
try {
|
||||||
return await preview(config, { logging }); // this will keep running
|
const server = await preview(config, { logging });
|
||||||
|
return await server.closed(); // keep alive until the server is closed
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return throwAndExit(err);
|
return throwAndExit(err);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ export interface PreviewServer {
|
||||||
host?: string;
|
host?: string;
|
||||||
port: number;
|
port: number;
|
||||||
server: http.Server;
|
server: http.Server;
|
||||||
|
closed(): Promise<void>;
|
||||||
stop(): Promise<void>;
|
stop(): Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,9 +134,18 @@ export default async function preview(
|
||||||
// Start listening on `hostname:port`.
|
// Start listening on `hostname:port`.
|
||||||
await startServer(startServerTime);
|
await startServer(startServerTime);
|
||||||
|
|
||||||
|
// Resolves once the server is closed
|
||||||
|
function closed() {
|
||||||
|
return new Promise<void>((resolve, reject) => {
|
||||||
|
httpServer!.addListener('close', resolve);
|
||||||
|
httpServer!.addListener('error', reject);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
host,
|
host,
|
||||||
port,
|
port,
|
||||||
|
closed,
|
||||||
server: httpServer!,
|
server: httpServer!,
|
||||||
stop: async () => {
|
stop: async () => {
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
|
|
Loading…
Reference in a new issue