diff --git a/apps/svelte.dev/content/blog/2020-07-17-svelte-and-typescript.md b/apps/svelte.dev/content/blog/2020-07-17-svelte-and-typescript.md index a6e07242ec..580507114f 100644 --- a/apps/svelte.dev/content/blog/2020-07-17-svelte-and-typescript.md +++ b/apps/svelte.dev/content/blog/2020-07-17-svelte-and-typescript.md @@ -66,18 +66,18 @@ You first need to set up [`svelte-preprocess`](https://github.com/sveltejs/svelt In a Rollup project, that would look like this — note that we also need to install `@rollup/plugin-typescript` so that Rollup can handle `.ts` files: -```diff -+ import autoPreprocess from 'svelte-preprocess'; -+ import typescript from '@rollup/plugin-typescript'; +```js ++++import autoPreprocess from 'svelte-preprocess'; +import typescript from '@rollup/plugin-typescript';+++ export default { - ..., - plugins: [ - svelte({ -+ preprocess: autoPreprocess() - }), -+ typescript({ sourceMap: !production }) - ] + // ..., + plugins: [ + svelte({ + +++preprocess: autoPreprocess()+++ + }), + +++typescript({ sourceMap: !production })+++ + ] } ``` diff --git a/apps/svelte.dev/content/blog/2023-03-09-zero-config-type-safety.md b/apps/svelte.dev/content/blog/2023-03-09-zero-config-type-safety.md index f7e53d122c..998b4a1cdb 100644 --- a/apps/svelte.dev/content/blog/2023-03-09-zero-config-type-safety.md +++ b/apps/svelte.dev/content/blog/2023-03-09-zero-config-type-safety.md @@ -28,7 +28,7 @@ const database = { } }; // ---cut--- -// src/routes/blog/[slug]/+page.server.ts +/// file: src/routes/blog/[slug]/+page.server.ts import type { ServerLoadEvent } from '@sveltejs/kit'; export async function load(event: ServerLoadEvent) { @@ -42,16 +42,16 @@ This works, but we can do better. Notice that we accidentally wrote `event.param This is where our automatic type generation comes in. Every route directory has a hidden `$types.d.ts` file with route-specific types: -```diff -// src/routes/blog/[slug]/+page.server.ts --import type { ServerLoadEvent } from '@sveltejs/kit'; -+import type { PageServerLoadEvent } from './$types'; +```js +/// file: src/routes/blog/[slug]/+page.server.ts +---import type { ServerLoadEvent } from '@sveltejs/kit';--- ++++import type { PageServerLoadEvent } from './$types';+++ export async function load(event: PageServerLoadEvent) { - return { -- post: await database.getPost(event.params.post) -+ post: await database.getPost(event.params.slug) - }; + return { + ---post: await database.getPost(event.params.post)--- + +++post: await database.getPost(event.params.slug)+++ + }; } ``` @@ -60,7 +60,7 @@ This reveals our typo, as it now errors on the `params.post` property access. Be After we have loaded our data, we want to display it in our `+page.svelte`. The same type generation mechanism ensures that the type of `data` is correct: ```svelte - +/// file: src/routes/blog/[slug]/+page.svelte ``` diff --git a/apps/svelte.dev/content/blog/2023-08-31-view-transitions.md b/apps/svelte.dev/content/blog/2023-08-31-view-transitions.md index 1d934dd1f4..6e1fc58a10 100644 --- a/apps/svelte.dev/content/blog/2023-08-31-view-transitions.md +++ b/apps/svelte.dev/content/blog/2023-08-31-view-transitions.md @@ -14,7 +14,7 @@ However, until now, you couldn’t easily use this API in a SvelteKit app, since You can trigger a view transition by calling `document.startViewTransition` and passing a callback that updates the DOM somehow. For our purposes today, SvelteKit will update the DOM as the user navigates. Once the callback finishes, the browser will transition to the new page state — by default, it does a crossfade between the old and the new states. ```js -// @errors: 2339 +// @errors: 2339 2304 const domUpdate = async () => {}; // ---cut--- document.startViewTransition(async () => { @@ -162,7 +162,7 @@ Now, the header will not transition in and out on navigation, but the rest of th > [!DETAILS] Fixing the types > Since `startViewTransition` is not supported by all browsers, your IDE may not know that it exists. To make the errors go away and get the correct typings, add the following to your `app.d.ts`: > -> ```ts +> ```dts > declare global { > // preserve any customizations you have here > namespace App { diff --git a/apps/svelte.dev/content/blog/2023-09-20-runes.md b/apps/svelte.dev/content/blog/2023-09-20-runes.md index 09da6b024c..b4e719895d 100644 --- a/apps/svelte.dev/content/blog/2023-09-20-runes.md +++ b/apps/svelte.dev/content/blog/2023-09-20-runes.md @@ -54,11 +54,11 @@ Runes are symbols that influence the Svelte compiler. Whereas Svelte today uses For example, to declare a piece of reactive state, we can use the `$state` rune: -```diff +```svelte -- ``` @@ -119,35 +119,34 @@ This works, but it's pretty weird! We've found that the store API can get rather With runes, things get much simpler: -```diff +```js /// file: counter.svelte.js --import { writable } from 'svelte/store'; +---import { writable } from 'svelte/store';--- export function createCounter() { -- const { subscribe, update } = writable(0); -+ let count = $state(0); + ---const { subscribe, update } = writable(0);--- + +++let count = $state(0);+++ return { -- subscribe, -- increment: () => update((n) => n + 1) -+ get count() { return count }, -+ increment: () => count += 1 - }; + ---subscribe,--- + ---increment: () => update((n) => n + 1)--- + +++get count() { return count },+++ + +++increment: () => count += 1+++ + }; } ``` -```diff +```svelte ``` diff --git a/apps/svelte.dev/content/docs/kit/20-core-concepts/20-load.md b/apps/svelte.dev/content/docs/kit/20-core-concepts/20-load.md index 46d8deec2f..4d1959e557 100644 --- a/apps/svelte.dev/content/docs/kit/20-core-concepts/20-load.md +++ b/apps/svelte.dev/content/docs/kit/20-core-concepts/20-load.md @@ -110,26 +110,26 @@ export async function load() { Data returned from layout `load` functions is available to child `+layout.svelte` components and the `+page.svelte` component as well as the layout that it 'belongs' to. -```diff +```svelte /// file: src/routes/blog/[slug]/+page.svelte

{data.post.title}

{@html data.post.content}
-+{#if next} -+

Next post: {next.title}

-+{/if} ++++{#if next} +

Next post: {next.title}

+{/if}+++ ``` > [!NOTE] If multiple `load` functions return data with the same key, the last one 'wins' — the result of a layout `load` returning `{ a: 1, b: 2 }` and a page `load` returning `{ b: 3, c: 4 }` would be `{ a: 1, b: 3, c: 4 }`. @@ -381,16 +381,21 @@ In `+page.js` or `+layout.js` it will return data from parent `+layout.js` files Take care not to introduce waterfalls when using `await parent()`. Here, for example, `getData(params)` does not depend on the result of calling `parent()`, so we should call it first to avoid a delayed render. -```diff +```js /// file: +page.js +// @filename: ambient.d.ts +declare function getData(params: Record): Promise<{ meta: any }> + +// @filename: index.js +// ---cut--- /** @type {import('./$types').PageLoad} */ export async function load({ params, parent }) { -- const parentData = await parent(); + ---const parentData = await parent();--- const data = await getData(params); -+ const parentData = await parent(); + +++const parentData = await parent();+++ return { - ...data + ...data, meta: { ...parentData.meta, ...data.meta } }; } diff --git a/apps/svelte.dev/content/docs/kit/20-core-concepts/30-form-actions.md b/apps/svelte.dev/content/docs/kit/20-core-concepts/30-form-actions.md index b5bb6ea5cf..ce23f7c43d 100644 --- a/apps/svelte.dev/content/docs/kit/20-core-concepts/30-form-actions.md +++ b/apps/svelte.dev/content/docs/kit/20-core-concepts/30-form-actions.md @@ -54,17 +54,17 @@ We can also invoke the action from other pages (for example if there's a login w Instead of one `default` action, a page can have as many named actions as it needs: -```diff +```js /// file: src/routes/login/+page.server.js /** @type {import('./$types').Actions} */ export const actions = { -- default: async (event) => { -+ login: async (event) => { +--- default: async (event) => {--- ++++ login: async (event) => {+++ // TODO log the user in }, -+ register: async (event) => { -+ // TODO register the user -+ } ++++ register: async (event) => { + // TODO register the user + }+++ }; ``` @@ -82,10 +82,9 @@ To invoke a named action, add a query parameter with the name prefixed by a `/` As well as the `action` attribute, we can use the `formaction` attribute on a button to `POST` the same form data to a different action than the parent `
`: -```diff +```svelte /// file: src/routes/login/+page.svelte -- -+ + -+ + ++++++
``` @@ -106,8 +105,14 @@ As well as the `action` attribute, we can use the `formaction` attribute on a bu Each action receives a `RequestEvent` object, allowing you to read the data with `request.formData()`. After processing the request (for example, logging the user in by setting a cookie), the action can respond with data that will be available through the `form` property on the corresponding page and through `$page.form` app-wide until the next update. ```js -// @errors: 2304 /// file: src/routes/login/+page.server.js +// @filename: ambient.d.ts +declare module '$lib/server/db'; + +// @filename: index.js +// ---cut--- +import * as db from '$lib/server/db'; + /** @type {import('./$types').PageServerLoad} */ export async function load({ cookies }) { const user = await db.getUserFromSession(cookies.get('sessionid')); @@ -153,9 +158,15 @@ export const actions = { If the request couldn't be processed because of invalid data, you can return validation errors — along with the previously submitted form values — back to the user so that they can try again. The `fail` function lets you return an HTTP status code (typically 400 or 422, in the case of validation errors) along with the data. The status code is available through `$page.status` and the data through `form`: -```diff +```js /// file: src/routes/login/+page.server.js -+import { fail } from '@sveltejs/kit'; +// @filename: ambient.d.ts +declare module '$lib/server/db'; + +// @filename: index.js +// ---cut--- ++++import { fail } from '@sveltejs/kit';+++ +import * as db from '$lib/server/db'; /** @type {import('./$types').Actions} */ export const actions = { @@ -164,15 +175,15 @@ export const actions = { const email = data.get('email'); const password = data.get('password'); -+ if (!email) { -+ return fail(400, { email, missing: true }); -+ } ++++ if (!email) { + return fail(400, { email, missing: true }); + }+++ const user = await db.getUser(email); -+ if (!user || user.password !== hash(password)) { -+ return fail(400, { email, incorrect: true }); -+ } ++++ if (!user || user.password !== db.hash(password)) { + return fail(400, { email, incorrect: true }); + }+++ cookies.set('sessionid', await db.createSession(user), { path: '/' }); @@ -186,15 +197,14 @@ export const actions = { > [!NOTE] Note that as a precaution, we only return the email back to the page — not the password. -```diff +```svelte /// file: src/routes/login/+page.svelte
-+ {#if form?.missing}

The email field is required

{/if} -+ {#if form?.incorrect}

Invalid credentials!

{/if} ++++ {#if form?.missing}

The email field is required

{/if} + {#if form?.incorrect}

Invalid credentials!

{/if}+++