Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] explain how to break out of root layout #7617

Closed
wants to merge 3 commits into from

Conversation

dummdidumm
Copy link
Member

This comes up so often on Discord etc that it warrants its own section with some explanatory content.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Nov 11, 2022

⚠️ No Changeset found

Latest commit: 4072348

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@benmccann benmccann added the documentation Improvements or additions to documentation label Nov 12, 2022
Rich-Harris and others added 2 commits November 13, 2022 21:57
Co-authored-by: gtmnayan <50981692+gtm-nayan@users.noreply.github.com>
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
Copy link
Member

@Rich-Harris Rich-Harris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This honestly feels rather duplicative to me — can't we add remarks to the existing sections? The (group) section already includes an /admin route which would be a perfect example of this, and the page@ section is all about breaking out of the layout hierarchy, so it feels like a more appropriate venue for this

Comment on lines +228 to +230
#### Breaking out of the root layout

There is no way to break out of the root layout. You can be sure that it's always present in your app and for example put app-wide UI or behavior in it - and if you don't define one, it falls back to a simple `<slot />` component. If you want some pages to _not_ have the same layout as the rest, then you have two options:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to phrase this differently — it's framed as a limitation rather than a deliberate design choice, and it's weird to have a 'how to do X' section that begins 'you can't'

Suggested change
#### Breaking out of the root layout
There is no way to break out of the root layout. You can be sure that it's always present in your app and for example put app-wide UI or behavior in it - and if you don't define one, it falls back to a simple `<slot />` component. If you want some pages to _not_ have the same layout as the rest, then you have two options:
#### Ignoring all layouts
The root layout applies to every page of your app. If you want some pages to _not_ have the same layout as the rest, then you have two options:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I phrased it specifically that way because on Discord people phrase their questions more around "breaking out of" than "ignoring" - but I agree that it's somewhat clunky the way I've written it.

Comment on lines +244 to +258
Option 2: Add an exception to your root `+layout.svelte` where you don't render the surrounding UI on specific pages:

```svelte
/// file: src/routes/+layout.svelte
<script>
import { page } from '$app/stores';
</script>

{#if $page.url.pathname !== '/login'}
<slot />
{:else}
<div>Surrounding UI</div>
<slot />
{/if}
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this is a pattern we don't really want to promote — it's the sort of thing that can get out of hand quite quickly, and it introduces sort of a mismatch between the UI code (which has if statements) and data fetching code (which doesn't). It also defeats code-splitting — in a more realistic example, one of the branches would use a component, which we'd be importing in both cases

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For simplicity's sake I think this solution is actually perfectly fine for a number of use cases. I agree though that this gets out of hand at some point, so we should point that out.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just remembered this bug: #2527

We absolutely don't want to encourage this pattern

@dummdidumm
Copy link
Member Author

This honestly feels rather duplicative to me — can't we add remarks to the existing sections? The (group) section already includes an /admin route which would be a perfect example of this, and the page@ section is all about breaking out of the layout hierarchy, so it feels like a more appropriate venue for this

The page@ section already had a sentence on that previously, and it seems people just glanced over it. That's why I decided to give this special case more room, since it's such a commonly asked thing. The placement of the section also has a somewhat nice segway to the "alternatives" section. I would be ok with moving it inside the page@ section though, but with much more text and code snippets than what it has now.

@Rich-Harris Rich-Harris mentioned this pull request Nov 14, 2022
@Rich-Harris
Copy link
Member

I had a crack at a shorter version in #7651

@dummdidumm
Copy link
Member Author

Closing in favor of #7651

@dummdidumm dummdidumm closed this Nov 15, 2022
@dummdidumm dummdidumm deleted the root-layout-docs branch November 15, 2022 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants