Skip to content

Commit 425d323

Browse files
Sync kit docs (#1193)
sync kit docs Co-authored-by: svelte-docs-bot[bot] <196124396+svelte-docs-bot[bot]@users.noreply.github.com>
1 parent 8fd43ef commit 425d323

File tree

7 files changed

+31
-16
lines changed

7 files changed

+31
-16
lines changed

apps/svelte.dev/content/docs/kit/20-core-concepts/10-routing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,10 @@ export async function POST({ request }) {
364364
Exporting the `fallback` handler will match any unhandled request methods, including methods like `MOVE` which have no dedicated export from `+server.js`.
365365
366366
```js
367-
// @errors: 7031
368367
/// file: src/routes/api/add/+server.js
369368
import { json, text } from '@sveltejs/kit';
370369

370+
/** @type {import('./$types').RequestHandler} */
371371
export async function POST({ request }) {
372372
const { a, b } = await request.json();
373373
return json(a + b);

apps/svelte.dev/content/docs/kit/20-core-concepts/40-page-options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Routes with `prerender = true` will be excluded from manifests used for dynamic
3232
export const prerender = 'auto';
3333
```
3434

35-
> [!NOTE] If your entire app is suitable for prerendering, you can use [`adapter-static`](https://github.com/sveltejs/kit/tree/main/packages/adapter-static), which will output files suitable for use with any static webserver.
35+
> [!NOTE] If your entire app is suitable for prerendering, you can use [`adapter-static`](adapter-static), which will output files suitable for use with any static webserver.
3636
3737
The prerenderer will start at the root of your app and generate files for any prerenderable pages or `+server.js` routes it finds. Each page is scanned for `<a>` elements that point to other pages that are candidates for prerendering — because of this, you generally don't need to specify which pages should be accessed. If you _do_ need to specify which pages should be accessed by the prerenderer, you can do so with [`config.kit.prerender.entries`](configuration#prerender), or by exporting an [`entries`](#entries) function from your dynamic route.
3838

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,28 @@ The `lang` parameter will be correctly derived from the returned pathname.
300300

301301
Using `reroute` will _not_ change the contents of the browser's address bar, or the value of `event.url`.
302302

303-
Since version 2.18, the `reroute` hook can be asynchronous, allowing it to (for example) fetch data from your backend to decide where to reroute to. Use this carefully and make sure it's fast, as it will delay navigation otherwise.
303+
Since version 2.18, the `reroute` hook can be asynchronous, allowing it to (for example) fetch data from your backend to decide where to reroute to. Use this carefully and make sure it's fast, as it will delay navigation otherwise. If you need to fetch data, use the `fetch` provided as an argument. It has the [same benefits](load#Making-fetch-requests) as the `fetch` provided to `load` functions, with the caveat that `params` and `id` are unavailable to [`handleFetch`](#Server-hooks-handleFetch) because the route is not yet known.
304+
305+
```js
306+
/// file: src/hooks.js
307+
// @errors: 2345`
308+
// @errors: 2304
309+
310+
/** @type {import('@sveltejs/kit').Reroute} */
311+
export function reroute({ url, fetch }) {
312+
// Ask a special endpoint within your app about the destination
313+
if (url.pathname === '/api/reroute') return;
314+
315+
const api = new URL('/api/reroute', url);
316+
api.searchParams.set('pathname', url.pathname);
317+
318+
const result = await fetch(api).then(r => r.json());
319+
return result.pathname;
320+
}
321+
```
322+
323+
324+
> [!NOTE] `reroute` is considered a pure, idempotent function. As such, it must always return the same output for the same input and not have side effects. Under these assumptions, SvelteKit caches the result of `reroute` on the client so it is only called once per unique URL.
304325
305326
### transport
306327

apps/svelte.dev/content/docs/kit/60-appendix/10-faq.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,12 @@ See [the documentation regarding project types](project-types) for more details.
1313

1414
## How do I include details from package.json in my application?
1515

16-
You cannot directly require JSON files, since SvelteKit expects [`svelte.config.js`](./configuration) to be an ES module. If you'd like to include your application's version number or other information from `package.json` in your application, you can load JSON like so:
16+
If you'd like to include your application's version number or other information from `package.json` in your application, you can load JSON like so:
1717

18-
```js
18+
```ts
19+
// @errors: 2732
1920
/// file: svelte.config.js
20-
// @filename: index.js
21-
/// <reference types="@types/node" />
22-
import { URL } from 'node:url';
23-
// ---cut---
24-
import { readFileSync } from 'node:fs';
25-
import { fileURLToPath } from 'node:url';
26-
27-
const path = fileURLToPath(new URL('package.json', import.meta.url));
28-
const pkg = JSON.parse(readFileSync(path, 'utf8'));
21+
import pkg from './package.json' with { type: 'json' };
2922
```
3023

3124
## How do I fix the error I'm getting trying to include a package?

apps/svelte.dev/content/docs/kit/60-appendix/20-integrations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ Since SvelteKit projects are built with Vite, you can use Vite plugins to enhanc
5050

5151
## Integration FAQs
5252

53-
The SvelteKit FAQ has a [how to do X with SvelteKit](./faq#How-do-I-set-up-a-database), which may be helpful if you still have questions.
53+
[The SvelteKit FAQ](./faq) answers many questions about how to do X with SvelteKit, which may be helpful if you still have questions.

apps/svelte.dev/content/docs/kit/60-appendix/40-migrating.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ The bulk of your app, in `src/routes`, can be left where it is, but several proj
4040

4141
Your `webpack.config.js` or `rollup.config.js` should be replaced with a `svelte.config.js`, as documented [here](configuration). Svelte preprocessor options should be moved to `config.preprocess`.
4242

43-
You will need to add an [adapter](adapters). `sapper build` is roughly equivalent to [adapter-node](https://github.com/sveltejs/kit/tree/main/packages/adapter-node) while `sapper export` is roughly equivalent to [adapter-static](https://github.com/sveltejs/kit/tree/main/packages/adapter-static), though you might prefer to use an adapter designed for the platform you're deploying to.
43+
You will need to add an [adapter](adapters). `sapper build` is roughly equivalent to [adapter-node](adapter-node) while `sapper export` is roughly equivalent to [adapter-static](adapter-static), though you might prefer to use an adapter designed for the platform you're deploying to.
4444

4545
If you were using plugins for filetypes that are not automatically handled by [Vite](https://vitejs.dev), you will need to find Vite equivalents and add them to the [Vite config](project-structure#Project-files-vite.config.js).
4646

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,7 @@ The [`reroute`](/docs/kit/hooks#Universal-hooks-reroute) hook allows you to modi
21492149
```dts
21502150
type Reroute = (event: {
21512151
url: URL;
2152+
fetch: typeof fetch;
21522153
}) => MaybePromise<void | string>;
21532154
```
21542155

0 commit comments

Comments
 (0)