Skip to content

Commit 66557a8

Browse files
authored
get non-webcontainer version working, and embeddable on cross-origin-isolated site (sveltejs#84)
1 parent b4f0146 commit 66557a8

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/routes/backend/_apps.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ const sveltekit = path.resolve(sveltekit_pkg_file, '..', sveltekit_pkg.bin['svel
3131
const apps = new Map();
3232
globalThis.__apps = apps;
3333

34+
const hooks_src = `/** @type {import('@sveltejs/kit').Handle} */
35+
export async function handle({ event, resolve }) {
36+
const response = await resolve(event);
37+
38+
response.headers.set('cross-origin-opener-policy', 'same-origin');
39+
response.headers.set('cross-origin-embedder-policy', 'require-corp');
40+
response.headers.set('cross-origin-resource-policy', 'cross-origin');
41+
42+
return response;
43+
}`;
44+
3445
/**
3546
* @param {{
3647
* files: import('$lib/types').FileStub[]
@@ -40,6 +51,14 @@ export async function create({ files }) {
4051
const id = String(Date.now());
4152
const filenames = write_files(id, files);
4253

54+
// TODO this enables embedding on cross-origin sites, which is
55+
// necessary for the JSNation talk, but will currently break if an app
56+
// already has a src/hooks.js file (though it could be worked
57+
// around easily enough if necessary)
58+
if (!files.find((stub) => stub.name === '/src/hooks.js')) {
59+
fs.writeFileSync(`.apps/${id}/src/hooks.js`, hooks_src);
60+
}
61+
4362
const port = await ports.find(3001);
4463

4564
apps.set(id, {

src/routes/tutorial/[slug]/_/Loading.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<div class="loading">
77
{#if initial}
8-
<p>Initializing WebContainer. This may take a few seconds</p>
8+
<p>initializing... this may take a few seconds</p>
99
{/if}
1010

1111
<svg width="107" height="128" viewBox="0 0 107 128">

src/routes/tutorial/[slug]/index.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@
138138
if (adapter) {
139139
await adapter.reset(Object.values(b));
140140
} else {
141-
const module = await import('$lib/client/adapters/webcontainer/index.js');
141+
const module = import.meta.env.VITE_USE_FILESYSTEM
142+
? await import('$lib/client/adapters/filesystem/index.js')
143+
: await import('$lib/client/adapters/webcontainer/index.js');
144+
142145
adapter = await module.create(Object.values(b));
143146
}
144147

0 commit comments

Comments
 (0)