Skip to content

Commit 912e907

Browse files
Sync kit docs (#972)
sync kit docs Co-authored-by: Rich-Harris <[email protected]>
1 parent 01a731d commit 912e907

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

apps/svelte.dev/content/docs/kit/30-advanced/20-hooks.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,25 @@ During development, if an error occurs because of a syntax error in your Svelte
237237

238238
> [!NOTE] Make sure that `handleError` _never_ throws an error
239239
240+
### init
241+
242+
This function runs once, when the server is created or the app starts in the browser, and is a useful place to do asynchronous work such as initializing a database connection.
243+
244+
> [!NOTE] If your environment supports top-level await, the `init` function is really no different from writing your initialisation logic at the top level of the module, but some environments — most notably, Safari — don't.
245+
246+
```js
247+
/// file: src/hooks.server.js
248+
import * as db from '$lib/server/database';
249+
250+
/** @type {import('@sveltejs/kit').ServerInit} */
251+
export async function init() {
252+
await db.connect();
253+
}
254+
```
255+
256+
> [!NOTE]
257+
> In the browser, asynchronous work in `init` will delay hydration, so be mindful of what you put in there.
258+
240259
## Universal hooks
241260

242261
The following can be added to `src/hooks.js`. Universal hooks run on both server and client (not to be confused with shared hooks, which are environment-specific).

apps/svelte.dev/content/docs/kit/98-reference/[email protected]

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,24 @@ Compress files in `directory` with gzip and brotli, where appropriate. Generates
851851
</div>
852852
</div></div>
853853

854+
## ClientInit
855+
856+
<blockquote class="since note">
857+
858+
Available since 2.10.0
859+
860+
</blockquote>
861+
862+
The [`init`](/docs/kit/hooks#Shared-hooks-init) will be invoked once the app starts in the browser
863+
864+
<div class="ts-block">
865+
866+
```dts
867+
type ClientInit = () => MaybePromise<void>;
868+
```
869+
870+
</div>
871+
854872
## Config
855873

856874
See the [configuration reference](/docs/kit/configuration) for details.
@@ -2347,6 +2365,24 @@ A `[file]: size` map of all assets imported by server code
23472365
</div>
23482366
</div></div>
23492367

2368+
## ServerInit
2369+
2370+
<blockquote class="since note">
2371+
2372+
Available since 2.10.0
2373+
2374+
</blockquote>
2375+
2376+
The [`init`](/docs/kit/hooks#Shared-hooks-init) will be invoked before the server responds to its first request
2377+
2378+
<div class="ts-block">
2379+
2380+
```dts
2381+
type ServerInit = () => MaybePromise<void>;
2382+
```
2383+
2384+
</div>
2385+
23502386
## ServerInitOptions
23512387

23522388
<div class="ts-block">

apps/svelte.dev/content/docs/kit/98-reference/20-$app-navigation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function afterNavigate(
4343

4444
## beforeNavigate
4545

46-
A navigation interceptor that triggers before we navigate to a new URL, whether by clicking a link, calling `goto(...)`, or using the browser back/forward controls.
46+
A navigation interceptor that triggers before we navigate to a URL, whether by clicking a link, calling `goto(...)`, or using the browser back/forward controls.
4747

4848
Calling `cancel()` will prevent the navigation from completing. If `navigation.type === 'leave'` — meaning the user is navigating away from the app (or closing the tab) — calling `cancel` will trigger the native browser unload confirmation dialog. In this case, the navigation may or may not be cancelled depending on the user's response.
4949

0 commit comments

Comments
 (0)