Skip to content

Commit 726013c

Browse files
committed
[feat] reload iframe in case of vite dev server restart
1 parent c4bba2e commit 726013c

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

src/lib/client/adapters/filesystem/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,25 @@ export async function create(stubs) {
5151
});
5252

5353
await new Promise((f) => setTimeout(f, 100)); // wait for chokidar
54+
55+
return will_restart_vite_dev_server(stubs);
5456
},
5557

5658
async destroy() {
5759
navigator.sendBeacon(`/backend/destroy?id=${id}`);
5860
}
5961
};
6062
}
63+
64+
/**
65+
* @param {import('$lib/types').Stub[]} stubs
66+
*/
67+
function will_restart_vite_dev_server(stubs) {
68+
return stubs.some(
69+
(stub) =>
70+
stub.type === 'file' &&
71+
(stub.name === '/vite.config.js' ||
72+
stub.name === '/svelte.config.js' ||
73+
stub.name === '/.env')
74+
);
75+
}

src/lib/client/adapters/webcontainer/index.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,7 @@ async function _create(stubs) {
161161
// For some reason, server-ready is fired again when the vite dev server is restarted.
162162
// We need to wait for it to finish before we can continue, else we might
163163
// request files from Vite before it's ready, leading to a timeout.
164-
const will_restart = new_stubs.some(
165-
(stub) =>
166-
stub.type === 'file' &&
167-
(stub.name === '/vite.config.js' ||
168-
stub.name === '/svelte.config.js' ||
169-
stub.name === '/.env')
170-
);
164+
const will_restart = will_restart_vite_dev_server(new_stubs);
171165
const promise = will_restart
172166
? new Promise((fulfil, reject) => {
173167
const error_unsub = vm.on('error', (error) => {
@@ -242,6 +236,8 @@ async function _create(stubs) {
242236
stubs_to_map(stubs, current);
243237

244238
await new Promise((f) => setTimeout(f, 200)); // wait for chokidar
239+
240+
return will_restart_vite_dev_server(stubs);
245241
}
246242

247243
async function destroy() {
@@ -258,6 +254,19 @@ async function _create(stubs) {
258254
};
259255
}
260256

257+
/**
258+
* @param {import('$lib/types').Stub[]} stubs
259+
*/
260+
function will_restart_vite_dev_server(stubs) {
261+
return stubs.some(
262+
(stub) =>
263+
stub.type === 'file' &&
264+
(stub.name === '/vite.config.js' ||
265+
stub.name === '/svelte.config.js' ||
266+
stub.name === '/.env')
267+
);
268+
}
269+
261270
/**
262271
* @param {import('$lib/types').Stub[]} stubs
263272
* @returns {import('@webcontainer/api').FileSystemTree}

src/lib/types/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface Adapter {
2222
base: string;
2323
/** Returns `false` if the reset was in such a way that a reload of the iframe isn't needed */
2424
reset(files: Array<Stub>): Promise<boolean>;
25-
update(file: Array<FileStub>): Promise<void>;
25+
update(file: Array<FileStub>): Promise<boolean>;
2626
destroy(): Promise<void>;
2727
}
2828

src/routes/tutorial/[slug]/+page.svelte

+17-2
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,25 @@
338338
const stub = event.detail;
339339
const index = current_stubs.findIndex((s) => s.name === stub.name);
340340
current_stubs[index] = stub;
341-
adapter?.update([stub]);
341+
adapter?.update([stub]).then((reload) => {
342+
if (reload) {
343+
schedule_iframe_reload();
344+
}
345+
});
342346
update_complete_states([stub]);
343347
}
344348
349+
/** @type {any} */
350+
let reload_timeout;
351+
function schedule_iframe_reload() {
352+
clearTimeout(reload_timeout);
353+
reload_timeout = setTimeout(() => {
354+
if (adapter) {
355+
set_iframe_src(adapter.base);
356+
}
357+
}, 1000);
358+
}
359+
345360
/**
346361
* @param {import('$lib/types').Stub[]} stubs
347362
*/
@@ -356,7 +371,7 @@
356371
}
357372
}
358373
359-
/** @type {NodeJS.Timeout} */
374+
/** @type {any} */
360375
let timeout;
361376
362377
/** @param {MessageEvent} e */

0 commit comments

Comments
 (0)