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(#2981): keeps astro preview server alive #3004

Merged
merged 3 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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