Skip to content

Commit

Permalink
fix(storefront): Keep restarting CMS preview dev server on WebContain…
Browse files Browse the repository at this point in the history
…er [#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
  • Loading branch information
leomp12 committed Feb 12, 2024
1 parent 2e9fe3c commit f463729
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/storefront/src/decap-cms/gen-preview-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
};
Expand Down
8 changes: 5 additions & 3 deletions packages/storefront/src/decap-cms/preview/webcontainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
};
Expand All @@ -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,
Expand Down

0 comments on commit f463729

Please sign in to comment.