Skip to content

Commit 2bc6262

Browse files
Warn if %sveltekit.body% is a direct child of <body> (#7652)
* warn about %sveltekit.body% inside <body> - #7585 * add display: contents to wrapper element by default * changeset * Apply suggestions from code review Co-authored-by: Conduitry <[email protected]> * update docs Co-authored-by: Conduitry <[email protected]>
1 parent 90f87cc commit 2bc6262

File tree

6 files changed

+19
-3
lines changed

6 files changed

+19
-3
lines changed

Diff for: .changeset/purple-apes-whisper.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'create-svelte': patch
3+
---
4+
5+
Add `style="display: contents"` to wrapper element by default

Diff for: .changeset/slimy-laws-argue.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Warn if `%sveltekit.body%` is direct child of `<body>`

Diff for: documentation/docs/10-getting-started/30-project-structure.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ The `src` directory contains the meat of your project.
4242
- `routes` contains the [routes](/docs/routing) of your application
4343
- `app.html` is your page template — an HTML document containing the following placeholders:
4444
- `%sveltekit.head%``<link>` and `<script>` elements needed by the app, plus any `<svelte:head>` content
45-
- `%sveltekit.body%` — the markup for a rendered page. Typically this lives inside a `<div>` or other element, rather than directly inside `<body>`, to prevent bugs caused by browser extensions injecting elements that are then destroyed by the hydration process
45+
- `%sveltekit.body%` — the markup for a rendered page. This should live inside a `<div>` or other element, rather than directly inside `<body>`, to prevent bugs caused by browser extensions injecting elements that are then destroyed by the hydration process. SvelteKit will warn you in development if this is not the case
4646
- `%sveltekit.assets%` — either [`paths.assets`](/docs/configuration#paths), if specified, or a relative path to [`paths.base`](/docs/configuration#paths)
4747
- `%sveltekit.nonce%` — a [CSP](/docs/configuration#csp) nonce for manually included links and scripts, if used
4848
- `error.html` (optional) is the page that is rendered when everything else fails. It can contain the following placeholders:

Diff for: packages/create-svelte/templates/default/src/app.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
%sveltekit.head%
88
</head>
99
<body data-sveltekit-prefetch>
10-
<div>%sveltekit.body%</div>
10+
<div style="display: contents">%sveltekit.body%</div>
1111
</body>
1212
</html>

Diff for: packages/create-svelte/templates/skeleton/src/app.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
%sveltekit.head%
88
</head>
99
<body>
10-
<div>%sveltekit.body%</div>
10+
<div style="display: contents">%sveltekit.body%</div>
1111
</body>
1212
</html>

Diff for: packages/kit/src/runtime/client/start.js

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ export async function start({ env, hydrate, paths, target, trailing_slash }) {
1919
set_public_env(env);
2020
set_paths(paths);
2121

22+
if (__SVELTEKIT_DEV__ && target === document.body) {
23+
console.warn(
24+
`Placing %sveltekit.body% directly inside <body> is not recommended, as your app may break for users who have certain browser extensions installed.\n\nConsider wrapping it in an element:\n\n<div style="display: contents">\n %sveltekit.body%\n</div>`
25+
);
26+
}
27+
2228
const client = create_client({
2329
target,
2430
base: paths.base,

0 commit comments

Comments
 (0)