Skip to content

Commit

Permalink
Warn if %sveltekit.body% is a direct child of <body> (#7652)
Browse files Browse the repository at this point in the history
* 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 <git@chor.date>

* update docs

Co-authored-by: Conduitry <git@chor.date>
  • Loading branch information
Rich-Harris and Conduitry authored Nov 15, 2022
1 parent 90f87cc commit 2bc6262
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-apes-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

Add `style="display: contents"` to wrapper element by default
5 changes: 5 additions & 0 deletions .changeset/slimy-laws-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Warn if `%sveltekit.body%` is direct child of `<body>`
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The `src` directory contains the meat of your project.
- `routes` contains the [routes](/docs/routing) of your application
- `app.html` is your page template — an HTML document containing the following placeholders:
- `%sveltekit.head%``<link>` and `<script>` elements needed by the app, plus any `<svelte:head>` content
- `%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
- `%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
- `%sveltekit.assets%` — either [`paths.assets`](/docs/configuration#paths), if specified, or a relative path to [`paths.base`](/docs/configuration#paths)
- `%sveltekit.nonce%` — a [CSP](/docs/configuration#csp) nonce for manually included links and scripts, if used
- `error.html` (optional) is the page that is rendered when everything else fails. It can contain the following placeholders:
Expand Down
2 changes: 1 addition & 1 deletion packages/create-svelte/templates/default/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
%sveltekit.head%
</head>
<body data-sveltekit-prefetch>
<div>%sveltekit.body%</div>
<div style="display: contents">%sveltekit.body%</div>
</body>
</html>
2 changes: 1 addition & 1 deletion packages/create-svelte/templates/skeleton/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
%sveltekit.head%
</head>
<body>
<div>%sveltekit.body%</div>
<div style="display: contents">%sveltekit.body%</div>
</body>
</html>
6 changes: 6 additions & 0 deletions packages/kit/src/runtime/client/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export async function start({ env, hydrate, paths, target, trailing_slash }) {
set_public_env(env);
set_paths(paths);

if (__SVELTEKIT_DEV__ && target === document.body) {
console.warn(
`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>`
);
}

const client = create_client({
target,
base: paths.base,
Expand Down

0 comments on commit 2bc6262

Please sign in to comment.