Skip to content

Commit 367067f

Browse files
ejmurrabenmccannRich Harris
authored
fix: allow embedding two pages generated into the same page in "embedded" mode (#9610)
* added route leaf hash to global init namespace * changeset * update changelog message * revert leaf hash commit * updated with latest suggestions * workong on test case * added int test for two embedded pages inlined into the same page * Update .changeset/late-toes-cry.md Co-authored-by: Ben McCann <[email protected]> * lint * delete test html pages * updated test case * update lockfile * simplify embed tests * remove some unused files * fix tsconfig * alphabetize * prettier * think i fixed it --------- Co-authored-by: Ben McCann <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent 84a5250 commit 367067f

File tree

16 files changed

+151
-4
lines changed

16 files changed

+151
-4
lines changed

.changeset/late-toes-cry.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: allow embedding two pages generated into the same page in "embedded" mode

packages/kit/src/runtime/server/page/render.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,9 @@ export async function render_response({
292292
const blocks = [];
293293

294294
const properties = [
295-
`env: ${s(public_env)}`,
296295
paths.assets && `assets: ${s(paths.assets)}`,
297296
`base: ${base_expression}`,
298-
`element: document.currentScript.parentElement`
297+
`env: ${s(public_env)}`
299298
].filter(Boolean);
300299

301300
if (chunks) {
@@ -318,7 +317,9 @@ export async function render_response({
318317
${properties.join(',\n\t\t\t\t\t\t')}
319318
};`);
320319

321-
const args = [`app`, `${global}.element`];
320+
const args = [`app`, `element`];
321+
322+
blocks.push(`const element = document.currentScript.parentElement;`);
322323

323324
if (page_config.ssr) {
324325
const serialized = { form: 'null', error: 'null' };
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.custom-out-dir
2+
!.env
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "test-options",
3+
"private": true,
4+
"version": "0.0.1",
5+
"scripts": {
6+
"dev": "vite dev",
7+
"build": "vite build",
8+
"preview": "vite preview",
9+
"check": "svelte-kit sync && tsc && svelte-check",
10+
"test": "pnpm test:dev && pnpm test:build",
11+
"test:dev": "cross-env DEV=true playwright test",
12+
"test:build": "playwright test"
13+
},
14+
"devDependencies": {
15+
"@sveltejs/kit": "workspace:^",
16+
"cross-env": "^7.0.3",
17+
"svelte": "^3.56.0",
18+
"svelte-check": "^3.0.2",
19+
"typescript": "^4.9.4",
20+
"vite": "^4.2.0"
21+
},
22+
"type": "module"
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { config as default } from '../../utils.js';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
%sveltekit.head%
2+
<div>%sveltekit.body%</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import { setup } from '../../../../setup.js';
3+
4+
setup();
5+
</script>
6+
7+
<slot />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/** @type {import('@sveltejs/kit').Load} */
2+
export async function load({ fetch }) {
3+
return {
4+
a: fetch('/embed/a').then((x) => x.text()),
5+
b: fetch('/embed/b').then((x) => x.text())
6+
};
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
export let data;
3+
</script>
4+
5+
{@html data.a}
6+
{@html data.b}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import { browser } from '$app/environment';
3+
</script>
4+
5+
<h2 data-testid="a">a ({browser ? 'browser' : 'server'})</h2>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import { browser } from '$app/environment';
3+
</script>
4+
5+
<h2 data-testid="b">b ({browser ? 'browser' : 'server'})</h2>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** @type {import('@sveltejs/kit').Config} */
2+
const config = {
3+
kit: {
4+
embedded: true
5+
}
6+
};
7+
8+
export default config;
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { expect } from '@playwright/test';
2+
import { test } from '../../../utils.js';
3+
4+
/** @typedef {import('@playwright/test').Response} Response */
5+
6+
test.describe.configure({ mode: 'parallel' });
7+
8+
test.describe('embed', () => {
9+
test('serves embedded components in page', async ({ page, javaScriptEnabled }) => {
10+
await page.goto('/embed');
11+
12+
if (javaScriptEnabled) {
13+
expect(await page.textContent('[data-testid="a"]')).toBe('a (browser)');
14+
expect(await page.textContent('[data-testid="b"]')).toBe('b (browser)');
15+
} else {
16+
expect(await page.textContent('[data-testid="a"]')).toBe('a (server)');
17+
expect(await page.textContent('[data-testid="b"]')).toBe('b (server)');
18+
}
19+
});
20+
});
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"checkJs": true,
5+
"esModuleInterop": true,
6+
"noEmit": true,
7+
"paths": {
8+
"@sveltejs/kit": ["../../../types"],
9+
"$lib": ["./src/lib"],
10+
"$lib/*": ["./src/lib/*"],
11+
"types": ["../../../types/internal"]
12+
},
13+
"resolveJsonModule": true
14+
},
15+
"extends": "./.svelte-kit/tsconfig.json"
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as path from 'node:path';
2+
import { sveltekit } from '@sveltejs/kit/vite';
3+
4+
/** @type {import('vite').UserConfig} */
5+
const config = {
6+
build: {
7+
minify: false
8+
},
9+
clearScreen: false,
10+
plugins: [sveltekit()],
11+
server: {
12+
fs: {
13+
allow: [path.resolve('../../../src')]
14+
}
15+
}
16+
};
17+
18+
export default config;

pnpm-lock.yaml

+22-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)