Skip to content

Commit

Permalink
[docs] add documentation for fetch (#2079)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored Aug 4, 2021
1 parent b3e7c8b commit 71b7a6b
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions documentation/docs/03-loading.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,20 @@ Our example blog page might contain a `load` function like the following. Note t

If `load` returns nothing, SvelteKit will [fall through](#routing-advanced-fallthrough-routes) to other routes until something responds, or will respond with a generic 404.

> `load` only applies to [page](#routing-pages) and [layout](#layouts) components, and can run both on the server (unless [ssr](#ssr-and-javascript-ssr) is disabled) and in the browser.
SvelteKit's `load` receives an implemention of `fetch`, which has the following special properties:
- it has access to cookies on the server
- it can make requests against the app's own endpoints without issuing an HTTP call
- it makes a copy of the response when you use it, and then sends it embedded in the initial page load for hydration

Code called inside `load` blocks:
`load` only applies to [page](#routing-pages) and [layout](#layouts) components (not components they import), and runs on both the server and in the browser with the default rendering options.

- should use the SvelteKit-provided [`fetch`](#loading-input-fetch) wrapper rather than using the native `fetch`, which doesn't have access to cookies on the server and cannot make requests against the app's own endpoints
- should not reference `window`, `document`, or any browser-specific objects
- should not directly reference any API keys or secrets, which will be exposed to the client, but instead call an endpoint that uses any required secrets
> Code called inside `load` blocks:
>
> - should use the SvelteKit-provided [`fetch`](#loading-input-fetch) wrapper rather than using the native `fetch`
> - should not reference `window`, `document`, or any browser-specific objects
> - should not directly reference any API keys or secrets, which will be exposed to the client, but instead call an endpoint that uses any required secrets
It is recommended that you not store pre-request state in global variables, but instead use them only for cross-cutting concerns such as caching and holding database connections.

> Mutating any shared state on the server will affect all clients, not just the current one.
Expand Down

0 comments on commit 71b7a6b

Please sign in to comment.