Skip to content

Commit

Permalink
fix(#2981): keeps astro preview server alive (#3004)
Browse files Browse the repository at this point in the history
* fix(#2981): keeps  server alive

* chore: adding changeset

* keep alive until the 'close' or 'error' event fires
  • Loading branch information
Tony Sullivan authored Apr 6, 2022
1 parent ba90ef5 commit 9724d84
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/big-yaks-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix bug causing `astro preview` server to close immediately
3 changes: 2 additions & 1 deletion packages/astro/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ export async function cli(args: string[]) {

case 'preview': {
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) {
return throwAndExit(err);
}
Expand Down
10 changes: 10 additions & 0 deletions packages/astro/src/core/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface PreviewServer {
host?: string;
port: number;
server: http.Server;
closed(): Promise<void>;
stop(): Promise<void>;
}

Expand Down Expand Up @@ -133,9 +134,18 @@ export default async function preview(
// Start listening on `hostname:port`.
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 {
host,
port,
closed,
server: httpServer!,
stop: async () => {
await new Promise((resolve, reject) => {
Expand Down

0 comments on commit 9724d84

Please sign in to comment.