From 71b7a6bb1ca65d2a148bad7d3f2ad6e03bb1348c Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Tue, 3 Aug 2021 21:06:29 -0700 Subject: [PATCH] [docs] add documentation for fetch (#2079) --- documentation/docs/03-loading.md | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/documentation/docs/03-loading.md b/documentation/docs/03-loading.md index 3454c2c6f7ca..cb275ed23cbd 100644 --- a/documentation/docs/03-loading.md +++ b/documentation/docs/03-loading.md @@ -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.