From bd82819e668a13bf78cf97ba1103ce9582cf01ba Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Fri, 25 Oct 2024 13:45:16 +0200 Subject: [PATCH] docs: more Svelte 5 conversion (#12871) --------- Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: Willow (GHOST) Co-authored-by: Sri Senthil Balaji J <50240805+SymphonySimper@users.noreply.github.com> Co-authored-by: Tee Ming --- .../docs/20-core-concepts/10-routing.md | 45 ++++++++++++------- .../docs/20-core-concepts/20-load.md | 33 +++++++------- .../docs/20-core-concepts/30-form-actions.md | 25 +++++------ .../20-core-concepts/50-state-management.md | 18 ++++---- .../25-build-and-deploy/90-adapter-vercel.md | 4 +- .../docs/30-advanced/10-advanced-routing.md | 6 +-- .../docs/30-advanced/65-snapshots.md | 2 +- .../docs/30-advanced/67-shallow-routing.md | 2 +- documentation/docs/98-reference/26-$lib.md | 16 ++++++- packages/kit/src/exports/public.d.ts | 15 +++---- packages/kit/types/index.d.ts | 15 +++---- 11 files changed, 104 insertions(+), 77 deletions(-) diff --git a/documentation/docs/20-core-concepts/10-routing.md b/documentation/docs/20-core-concepts/10-routing.md index 0c48a2b2c2b7..66d2af54bb61 100644 --- a/documentation/docs/20-core-concepts/10-routing.md +++ b/documentation/docs/20-core-concepts/10-routing.md @@ -37,17 +37,22 @@ A `+page.svelte` component defines a page of your app. By default, pages are ren Home ``` +Pages can receive data from `load` functions via the `data` prop. + ```svelte

{data.title}

{@html data.content}
``` +> [!LEGACY] In Svelte 4 +> In Svelte 4, you'd use `export let data` instead + > [!NOTE] Note that SvelteKit uses `` elements to navigate between routes, rather than a framework-specific `` component. ### +page.js @@ -153,21 +158,29 @@ But in many apps, there are elements that should be visible on _every_ page, suc To create a layout that applies to every page, make a file called `src/routes/+layout.svelte`. The default layout (the one that SvelteKit uses if you don't bring your own) looks like this... -```html - +```svelte + + +{@render children()} ``` -...but we can add whatever markup, styles and behaviour we want. The only requirement is that the component includes a `` for the page content. For example, let's add a nav bar: +...but we can add whatever markup, styles and behaviour we want. The only requirement is that the component includes a `@render` tag for the page content. For example, let's add a nav bar: + +```svelte + + -```html -/// file: src/routes/+layout.svelte - +{@render children()} ``` If we create pages for `/`, `/about` and `/settings`... @@ -196,8 +209,8 @@ We can create a layout that only applies to pages below `/settings` (while inher ```svelte

Settings

@@ -208,7 +221,7 @@ We can create a layout that only applies to pages below `/settings` (while inher {/each} - +{@render children()} ``` You can see how `data` is populated by looking at the `+layout.js` example in the next section just below. @@ -239,8 +252,8 @@ Data returned from a layout's `load` function is also available to all its child ```svelte @@ -370,13 +383,13 @@ export async function fallback({ request }) { Throughout the examples above, we've been importing types from a `$types.d.ts` file. This is a file SvelteKit creates for you in a hidden directory if you're using TypeScript (or JavaScript with JSDoc type annotations) to give you type safety when working with your root files. -For example, annotating `export let data` with `PageData` (or `LayoutData`, for a `+layout.svelte` file) tells TypeScript that the type of `data` is whatever was returned from `load`: +For example, annotating `let { data } = $props()` with `PageData` (or `LayoutData`, for a `+layout.svelte` file) tells TypeScript that the type of `data` is whatever was returned from `load`: ```svelte ``` diff --git a/documentation/docs/20-core-concepts/20-load.md b/documentation/docs/20-core-concepts/20-load.md index 3e50259a3308..d6739fca0c86 100644 --- a/documentation/docs/20-core-concepts/20-load.md +++ b/documentation/docs/20-core-concepts/20-load.md @@ -24,14 +24,17 @@ export function load({ params }) { ```svelte

{data.post.title}

{@html data.post.content}
``` +> [!LEGACY] In Svelte 4 +> In Svelte 4, you'd use `export let data` instead + Thanks to the generated `$types` module, we get full type safety. A `load` function in a `+page.js` file runs both on the server and in the browser (unless combined with `export const ssr = false`, in which case it will [only run in the browser](page-options#ssr)). If your `load` function should _always_ run on the server (because it uses private environment variables, for example, or accesses a database) then it would go in a `+page.server.js` instead. @@ -85,13 +88,13 @@ export async function load() { ```svelte
- - + + {@render children()}