From f463729891819723f3cb769a0a406594450fce59 Mon Sep 17 00:00:00 2001 From: Leonardo Matos Date: Mon, 12 Feb 2024 01:26:37 -0300 Subject: [PATCH] fix(storefront): Keep restarting CMS preview dev server on WebContainer [#320] From learn.svelte.dev implementation https://github.com/sveltejs/learn.svelte.dev/blob/034037fcfe896873354560d9770e54a56e5af7d8/src/lib/client/adapters/webcontainer/index.js Next release to check if dev server starts on production, not starting nor sending any error on localhost inside "parent" Astro dev server --- .../storefront/src/decap-cms/gen-preview-container.ts | 2 +- packages/storefront/src/decap-cms/preview/webcontainer.ts | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/storefront/src/decap-cms/gen-preview-container.ts b/packages/storefront/src/decap-cms/gen-preview-container.ts index 7f200e303..060015d0e 100644 --- a/packages/storefront/src/decap-cms/gen-preview-container.ts +++ b/packages/storefront/src/decap-cms/gen-preview-container.ts @@ -38,11 +38,11 @@ export const genPreviewContainer = ({ React, cmsConfig, ghToken }: { .getElementById(serverIframeId) as HTMLIFrameElement; initWebcontainer({ repo, ghToken, cliTextarea }) .then(({ webcontainerInstance, startDevServer }) => { - startDevServer(); webcontainerInstance.on('server-ready', (port, url) => { console.log({ port, url }); previewIframe.src = `${url}${previewIframe.dataset.url}`; }); + startDevServer(); }); } }; diff --git a/packages/storefront/src/decap-cms/preview/webcontainer.ts b/packages/storefront/src/decap-cms/preview/webcontainer.ts index 74914b3f0..56eb7f4d1 100644 --- a/packages/storefront/src/decap-cms/preview/webcontainer.ts +++ b/packages/storefront/src/decap-cms/preview/webcontainer.ts @@ -71,15 +71,15 @@ export const initWebcontainer = async ({ repo, ghToken, cliTextarea }: { const cliArgs = args.reduce((acc, opt) => `${acc} ${opt}`, ''); const cli = `$ ${command}${cliArgs}\n`; cliTextarea.value += cli; - const cmd = await webcontainerInstance.spawn(command, args); + const proc = await webcontainerInstance.spawn(command, args); if (import.meta.env.DEV || (window as any).DEBUG) { - cmd.output.pipeTo(new WritableStream({ + proc.output.pipeTo(new WritableStream({ write(stdout) { console.debug?.('webcontainer', { stdout }); }, })); } - if (await cmd.exit !== 0) { + if (await proc.exit !== 0) { throw new Error(`${command} failed`); } }; @@ -93,6 +93,8 @@ export const initWebcontainer = async ({ repo, ghToken, cliTextarea }: { ); const startDevServer = async () => { await exec('npm', ['--prefix', ssrDir, 'run', 'dev']); + // Keep restarting dev server (can crash) + startDevServer(); }; return { webcontainerInstance,