-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Implement new layouts system #6174
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
Merged
Merged
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
2a237eb
start updating tests
Rich-Harris f47c3fb
fix patterns
Rich-Harris d145557
get unit tests passing
Rich-Harris de383f9
fixes
Rich-Harris faee0df
changeset
Rich-Harris d6fba37
prevent conflicts
Rich-Harris bb5ed6e
helpful error on named layouts
Rich-Harris 02efc1e
fiddle about with data structures
Rich-Harris d2d3000
vanquish red squigglies
Rich-Harris bdaa71c
update some unit tests
Rich-Harris 6419645
update tests
Rich-Harris 6e04821
fixes
Rich-Harris ffb8a8a
fix
Rich-Harris 907f080
merge master
Rich-Harris 8229811
fix test
Rich-Harris 0a72369
update write_types
Rich-Harris 84e266f
precompute depth
Rich-Harris 2bd5203
remove some now-obsolete tests
Rich-Harris 0b52b1d
tidy up
Rich-Harris ce66e0d
import typescript at the top of the module, once
Rich-Harris 74433e5
remove unused argument
Rich-Harris e9bf361
rename functions to make stuff slightly clearer
Rich-Harris 3287b04
fix tests
Rich-Harris e4aa3a2
fix
Rich-Harris afe4cd4
make write_types truly incremental
Rich-Harris 7322c10
reuse compact helper
Rich-Harris cb262ff
docs
Rich-Harris 0ea2976
posixify
Rich-Harris fa5c424
more windows stuff
Rich-Harris 0acae38
oops
Rich-Harris 8ea3c40
optimise client manifest size a bit
Rich-Harris 891b319
fix
Rich-Harris 872a291
oops
Rich-Harris eef4be5
fall back to the server if loading fails
Rich-Harris 314ebe3
small tweaks and some comments
dummdidumm 5687d7a
fix filesystem router API check
dummdidumm 4bf6184
comment, split into methods for better separation
dummdidumm cb47d7f
remove console.log
dummdidumm fbb40a7
docs tweak
dummdidumm 0945723
Update packages/kit/src/utils/array.js
dummdidumm 0018d97
add note about when to use groups
dummdidumm 2d421b5
Update .changeset/many-forks-sniff.md
dummdidumm d39e33c
Update packages/kit/src/runtime/client/ambient.d.ts
dummdidumm 1b106fa
Update packages/kit/src/runtime/client/parse.js
dummdidumm 8bba42d
Update packages/kit/src/utils/array.js
dummdidumm dcf2ace
am I doing this right?
dummdidumm 7c74aa9
Thank you Conduitry
dummdidumm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@sveltejs/kit': patch | ||
| --- | ||
|
|
||
| [breaking] implement new layout system (see the PR for migration instructions) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,75 +126,95 @@ assert.equal( | |
|
|
||
| To express a `%` character, use `%25`, otherwise the result will be malformed. | ||
|
|
||
| ### Named layouts | ||
| ### Advanced layouts | ||
|
|
||
| Some parts of your app might need something other than the default layout. For these cases you can create _named layouts_... | ||
| By default, the _layout hierarchy_ mirrors the _route hierarchy_. In some cases, that might not be what you want. | ||
|
|
||
| ```svelte | ||
| /// file: src/routes/+layout-foo.svelte | ||
| <div class="foo"> | ||
| <slot></slot> | ||
| </div> | ||
| ``` | ||
| #### (group) | ||
|
|
||
| ...and then use them by referencing the layout name (`foo`, in the example above) in the filename: | ||
| Perhaps you have some routes that are 'app' routes that should have one layout (e.g. `/dashboard` or `/item`), and others that are 'marketing' routes that should have a different layout (`/blog` or `/testimonials`). We can group these routes with a directory whose name is wrapped in parentheses — unlike normal directories, `(app)` and `(marketing)` do not affect the URL pathname of the routes inside them: | ||
|
|
||
| ```svelte | ||
| /// file: src/routes/my-special-page/[email protected] | ||
| <h1>I am inside +layout-foo</h1> | ||
| ```diff | ||
| src/routes/ | ||
| +│ (app)/ | ||
| │ ├ dashboard/ | ||
| │ ├ item/ | ||
| │ └ +layout.svelte | ||
| +│ (marketing)/ | ||
| │ ├ about/ | ||
| │ ├ testimonials/ | ||
| │ └ +layout.svelte | ||
| ├ admin/ | ||
| └ +layout.svelte | ||
| ``` | ||
|
|
||
| > Named layout should only be referenced from Svelte files | ||
|
|
||
| Named layouts are very powerful, but it can take a minute to get your head round them. Don't worry if this doesn't make sense all at once. | ||
| You can also put a `+page` directly inside a `(group)`, for example if `/` should be an `(app)` or a `(marketing)` page. | ||
|
|
||
| #### Scoping | ||
| #### +page@ | ||
|
|
||
| Named layouts can be created at any depth, and will apply to any components in the same subtree. For example, `+layout-foo` will apply to `/x/one` and `/x/two`, but not `/x/three` or `/four`: | ||
| Conversely, some routes of your app might need to break out of the layout hierarchy. Let's add an `/item/[id]/embed` route inside the `(app)` group from the previous example: | ||
|
|
||
| ```bash | ||
| ```diff | ||
| src/routes/ | ||
| ├ x/ | ||
| │ ├ +layout-foo.svelte | ||
| │ ├ one/[email protected] # ✅ page has `@foo` | ||
| │ ├ two/[email protected] # ✅ page has `@foo` | ||
| │ └ three/+page.svelte # ❌ page does not have `@foo` | ||
| └ four/[email protected] # ❌ page has `@foo`, but +layout-foo is not 'in scope' | ||
| ├ (app)/ | ||
| │ ├ item/ | ||
| │ │ ├ [id]/ | ||
| │ │ │ ├ embed/ | ||
| +│ │ │ │ └ +page.svelte | ||
| │ │ │ └ +layout.svelte | ||
| │ │ └ +layout.svelte | ||
| │ └ +layout.svelte | ||
| └ +layout.svelte | ||
| ``` | ||
|
|
||
| #### Inheritance chains | ||
|
|
||
| Layouts can themselves choose to inherit from named layouts, from the same directory or a parent directory. For example, `x/y/[email protected]` is the default layout for `/x/y` (meaning `/x/y/one`, `/x/y/two` and `/x/y/three` all inherit from it) because it has no name. Because it specifies `@root`, it will inherit directly from the nearest `+layout-root.svelte`, skipping `+layout.svelte` and `x/+layout.svelte`. | ||
| Ordinarily, this would inherit the root layout, the `(app)` layout, the `item` layout and the `[id]` layout. We can reset to one of those layouts by appending `@` followed by the segment name — or, for the root layout, the empty string. In this example, we can choose from `[email protected]`, `+page@(app).svelte`, `[email protected]` or `+page@[id].svelte`: | ||
|
|
||
| ``` | ||
| ```diff | ||
| src/routes/ | ||
| ├ x/ | ||
| │ ├ y/ | ||
| │ │ ├ [email protected] | ||
| │ │ ├ one/+page.svelte | ||
| │ │ ├ two/+page.svelte | ||
| │ │ └ three/+page.svelte | ||
| ├ (app)/ | ||
| │ ├ item/ | ||
| │ │ ├ [id]/ | ||
| │ │ │ ├ embed/ | ||
| +│ │ │ │ └ +page@(app).svelte | ||
| │ │ │ └ +layout.svelte | ||
| │ │ └ +layout.svelte | ||
| │ └ +layout.svelte | ||
| ├ +layout.svelte | ||
| └ +layout-root.svelte | ||
| └ +layout.svelte | ||
| ``` | ||
|
|
||
| > In the case where `+layout-root.svelte` contains a lone `<slot />`, this effectively means we're able to 'reset' to a blank layout for any page or nested layout in the app by adding `@root`. | ||
| #### +layout@ | ||
|
|
||
| If no parent is specified, a layout will inherit from the nearest default (i.e. unnamed) layout _above_ it in the tree. In some cases, it's helpful for a named layout to inherit from a default layout _alongside_ it in the tree, such as `+layout-root.svelte` inheriting from `+layout.svelte`. We can do this by explicitly specifying `@default`, allowing `/x/y/one` and siblings to use the app's default layout without using `x/+layout.svelte`: | ||
| Like pages, layouts can _themselves_ break out of their parent layout hierarchy, using the same technique. For example, a `+layout@.svelte` component would reset the hierarchy for all its child routes. | ||
|
|
||
| ```diff | ||
| src/routes/ | ||
| ├ x/ | ||
| │ ├ y/ | ||
| │ │ ├ [email protected] | ||
| │ │ ├ one/+page.svelte | ||
| │ │ ├ two/+page.svelte | ||
| │ │ └ three/+page.svelte | ||
| │ └ +layout.svelte | ||
| ├ +layout.svelte | ||
| -└ +layout-root.svelte | ||
| +└ [email protected] | ||
| #### When to use layout groups | ||
|
|
||
| Not all use cases are suited for layout grouping, nor should you feel compelled to use them. It might be that your use case would result in complex `(group)` nesting, or that you don't want to introduce a `(group)` for a single outlier. It's perfectly fine to use other means such as composition (reusable `load` functions or Svelte components) or if-statements to achieve what you want. The following example shows a layout that rewinds to the root layout and reuses components and functions that other layouts can also use: | ||
|
|
||
| ```svelte | ||
| /// file: src/routes/nested/route/[email protected] | ||
| <script> | ||
| import ReusableLayout from '$lib/ReusableLayout.svelte'; | ||
| export let data; | ||
| </script> | ||
|
|
||
| <ReusableLayout {data}> | ||
| <slot /> | ||
| </ReusableLayout> | ||
| ``` | ||
|
|
||
| > `default` is a reserved name — in other words, you can't have a `+layout-default.svelte` file. | ||
| ```js | ||
| /// file: src/routes/nested/route/+layout.js | ||
| // @filename: ambient.d.ts | ||
| declare module "$lib/reusable-load-function" { | ||
| export function reusableLoad(event: import('@sveltejs/kit').LoadEvent): Promise<Record<string, any>>; | ||
| } | ||
| // @filename: index.js | ||
| // ---cut--- | ||
| import { reusableLoad } from '$lib/reusable-load-function'; | ||
|
|
||
| /** @type {import('./$types').PageLoad} */ | ||
| export function load(event) { | ||
| // Add additional logic here, if needed | ||
| return reusableLoad(event); | ||
| } | ||
| ``` | ||
Conduitry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.