Skip to content

Commit 3bbf4cb

Browse files
docs: add missing type information (#13928)
* add types * align config export and replace sh with bash * type errors to go with new type fixes in svelte.dev * separate syntax highlighting into a different pr * actually i guess this section was moved above? removing * add error annotation pending rewrite in #14203 --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 233ea22 commit 3bbf4cb

File tree

11 files changed

+125
-60
lines changed

11 files changed

+125
-60
lines changed

documentation/docs/20-core-concepts/30-form-actions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ Note that you need to `deserialize` the response before processing it further us
469469
If you have a `+server.js` alongside your `+page.server.js`, `fetch` requests will be routed there by default. To `POST` to an action in `+page.server.js` instead, use the custom `x-sveltekit-action` header:
470470
471471
```js
472+
// @errors: 2532 2304
472473
const response = await fetch(this.action, {
473474
method: 'POST',
474475
body: data,

documentation/docs/25-build-and-deploy/40-adapter-node.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ Install with `npm i -D @sveltejs/adapter-node`, then add the adapter to your `sv
1313
/// file: svelte.config.js
1414
import adapter from '@sveltejs/adapter-node';
1515

16-
export default {
16+
/** @type {import('@sveltejs/kit').Config} */
17+
const config = {
1718
kit: {
1819
adapter: adapter()
1920
}
2021
};
22+
23+
export default config;
2124
```
2225

2326
## Deploying
@@ -146,7 +149,8 @@ The adapter can be configured with various options:
146149
/// file: svelte.config.js
147150
import adapter from '@sveltejs/adapter-node';
148151

149-
export default {
152+
/** @type {import('@sveltejs/kit').Config} */
153+
const config = {
150154
kit: {
151155
adapter: adapter({
152156
// default options are shown
@@ -156,6 +160,8 @@ export default {
156160
})
157161
}
158162
};
163+
164+
export default config;
159165
```
160166

161167
### out

documentation/docs/25-build-and-deploy/50-adapter-static.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ This will prerender your entire site as a collection of static files. If you'd l
1111
Install with `npm i -D @sveltejs/adapter-static`, then add the adapter to your `svelte.config.js`:
1212

1313
```js
14-
// @errors: 2307
1514
/// file: svelte.config.js
1615
import adapter from '@sveltejs/adapter-static';
1716

18-
export default {
17+
/** @type {import('@sveltejs/kit').Config} */
18+
const config = {
1919
kit: {
2020
adapter: adapter({
2121
// default options are shown. On some platforms
@@ -28,6 +28,8 @@ export default {
2828
})
2929
}
3030
};
31+
32+
export default config;
3133
```
3234

3335
...and add the [`prerender`](page-options#prerender) option to your root layout:
@@ -49,13 +51,17 @@ Some platforms have zero-config support (more to come in future):
4951
On these platforms, you should omit the adapter options so that `adapter-static` can provide the optimal configuration:
5052

5153
```js
52-
// @errors: 2304
5354
/// file: svelte.config.js
54-
export default {
55+
import adapter from '@sveltejs/adapter-static';
56+
57+
/** @type {import('@sveltejs/kit').Config} */
58+
const config = {
5559
kit: {
5660
adapter: adapter(---{...}---)
5761
}
5862
};
63+
64+
export default config;
5965
```
6066

6167
## Options
@@ -89,7 +95,7 @@ You'll also want to generate a fallback `404.html` page to replace the default 4
8995
A config for GitHub Pages might look like the following:
9096

9197
```js
92-
// @errors: 2307 2322
98+
// @errors: 2322
9399
/// file: svelte.config.js
94100
import adapter from '@sveltejs/adapter-static';
95101

documentation/docs/25-build-and-deploy/55-single-page-apps.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@ If you don't have any server-side logic (i.e. `+page.server.js`, `+layout.server
1818
Install with `npm i -D @sveltejs/adapter-static`, then add the adapter to your `svelte.config.js` with the following options:
1919

2020
```js
21-
// @errors: 2307
2221
/// file: svelte.config.js
2322
import adapter from '@sveltejs/adapter-static';
2423

25-
export default {
24+
/** @type {import('@sveltejs/kit').Config} */
25+
const config = {
2626
kit: {
2727
adapter: adapter({
2828
fallback: '200.html' // may differ from host to host
2929
})
3030
}
3131
};
32+
33+
export default config;
3234
```
3335

3436
The `fallback` page is an HTML page created by SvelteKit from your page template (e.g. `app.html`) that loads your app and navigates to the correct route. For example [Surge](https://surge.sh/help/adding-a-200-page-for-client-side-routing), a static web host, lets you add a `200.html` file that will handle any requests that don't correspond to static assets or prerendered pages.

documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ Install with `npm i -D @sveltejs/adapter-cloudflare`, then add the adapter to yo
2020
/// file: svelte.config.js
2121
import adapter from '@sveltejs/adapter-cloudflare';
2222

23-
export default {
23+
/** @type {import('@sveltejs/kit').Config} */
24+
const config = {
2425
kit: {
2526
adapter: adapter({
2627
// See below for an explanation of these options
@@ -38,6 +39,8 @@ export default {
3839
})
3940
}
4041
};
42+
43+
export default config;
4144
```
4245

4346
## Options
@@ -125,9 +128,25 @@ Functions contained in the [`/functions` directory](https://developers.cloudflar
125128
The [`env`](https://developers.cloudflare.com/workers/runtime-apis/fetch-event#parameters) object contains your project's [bindings](https://developers.cloudflare.com/workers/runtime-apis/bindings/), which consist of KV/DO namespaces, etc. It is passed to SvelteKit via the `platform` property, along with [`ctx`](https://developers.cloudflare.com/workers/runtime-apis/context/), [`caches`](https://developers.cloudflare.com/workers/runtime-apis/cache/), and [`cf`](https://developers.cloudflare.com/workers/runtime-apis/request/#incomingrequestcfproperties), meaning that you can access it in hooks and endpoints:
126129

127130
```js
128-
// @errors: 7031
131+
// @filename: ambient.d.ts
132+
import { DurableObjectNamespace } from '@cloudflare/workers-types';
133+
134+
declare global {
135+
namespace App {
136+
interface Platform {
137+
env: {
138+
YOUR_DURABLE_OBJECT_NAMESPACE: DurableObjectNamespace;
139+
};
140+
}
141+
}
142+
}
143+
// @filename: +server.js
144+
// ---cut---
145+
// @errors: 2355 2322
146+
/// file: +server.js
147+
/** @type {import('./$types').RequestHandler} */
129148
export async function POST({ request, platform }) {
130-
const x = platform.env.YOUR_DURABLE_OBJECT_NAMESPACE.idFromName('x');
149+
const x = platform?.env.YOUR_DURABLE_OBJECT_NAMESPACE.idFromName('x');
131150
}
132151
```
133152
@@ -142,7 +161,7 @@ To make these types available to your app, install [`@cloudflare/workers-types`]
142161
declare global {
143162
namespace App {
144163
interface Platform {
145-
+++ env?: {
164+
+++ env: {
146165
YOUR_KV_NAMESPACE: KVNamespace;
147166
YOUR_DURABLE_OBJECT_NAMESPACE: DurableObjectNamespace;
148167
};+++
@@ -197,15 +216,19 @@ Cloudflare no longer recommends using [Workers Sites](https://developers.cloudfl
197216
### svelte.config.js
198217
199218
```js
219+
// @errors: 2307
200220
/// file: svelte.config.js
201221
---import adapter from '@sveltejs/adapter-cloudflare-workers';---
202222
+++import adapter from '@sveltejs/adapter-cloudflare';+++
203223

204-
export default {
224+
/** @type {import('@sveltejs/kit').Config} */
225+
const config = {
205226
kit: {
206227
adapter: adapter()
207228
}
208229
};
230+
231+
export default config;
209232
```
210233
211234
### wrangler.toml

documentation/docs/25-build-and-deploy/70-adapter-cloudflare-workers.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ Install with `npm i -D @sveltejs/adapter-cloudflare-workers`, then add the adapt
1515
/// file: svelte.config.js
1616
import adapter from '@sveltejs/adapter-cloudflare-workers';
1717

18-
export default {
18+
/** @type {import('@sveltejs/kit').Config} */
19+
const config = {
1920
kit: {
2021
adapter: adapter({
2122
// see below for options that can be set here
2223
})
2324
}
2425
};
26+
27+
export default config;
2528
```
2629

2730
## Options
@@ -80,9 +83,25 @@ wrangler deploy
8083
The [`env`](https://developers.cloudflare.com/workers/runtime-apis/fetch-event#parameters) object contains your project's [bindings](https://developers.cloudflare.com/workers/runtime-apis/bindings/), which consist of KV/DO namespaces, etc. It is passed to SvelteKit via the `platform` property, along with [`ctx`](https://developers.cloudflare.com/workers/runtime-apis/context/), [`caches`](https://developers.cloudflare.com/workers/runtime-apis/cache/), and [`cf`](https://developers.cloudflare.com/workers/runtime-apis/request/#incomingrequestcfproperties), meaning that you can access it in hooks and endpoints:
8184

8285
```js
83-
// @errors: 7031
86+
// @filename: ambient.d.ts
87+
import { DurableObjectNamespace } from '@cloudflare/workers-types';
88+
89+
declare global {
90+
namespace App {
91+
interface Platform {
92+
env: {
93+
YOUR_DURABLE_OBJECT_NAMESPACE: DurableObjectNamespace;
94+
};
95+
}
96+
}
97+
}
98+
// @filename: +server.js
99+
// ---cut---
100+
// @errors: 2355 2322
101+
/// file: +server.js
102+
/** @type {import('./$types').RequestHandler} */
84103
export async function POST({ request, platform }) {
85-
const x = platform.env.YOUR_DURABLE_OBJECT_NAMESPACE.idFromName('x');
104+
const x = platform?.env.YOUR_DURABLE_OBJECT_NAMESPACE.idFromName('x');
86105
}
87106
```
88107

documentation/docs/25-build-and-deploy/80-adapter-netlify.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ This adapter will be installed by default when you use [`adapter-auto`](adapter-
1111
Install with `npm i -D @sveltejs/adapter-netlify`, then add the adapter to your `svelte.config.js`:
1212

1313
```js
14-
// @errors: 2307
1514
/// file: svelte.config.js
1615
import adapter from '@sveltejs/adapter-netlify';
1716

18-
export default {
17+
/** @type {import('@sveltejs/kit').Config} */
18+
const config = {
1919
kit: {
2020
// default options are shown
2121
adapter: adapter({
@@ -30,6 +30,8 @@ export default {
3030
})
3131
}
3232
};
33+
34+
export default config;
3335
```
3436

3537
Then, make sure you have a [netlify.toml](https://docs.netlify.com/configure-builds/file-based-configuration) file in the project root. This will determine where to write static assets based on the `build.publish` settings, as per this sample configuration:
@@ -51,11 +53,11 @@ New projects will use the current Node LTS version by default. However, if you'r
5153
SvelteKit supports [Netlify Edge Functions](https://docs.netlify.com/netlify-labs/experimental-features/edge-functions/). If you pass the option `edge: true` to the `adapter` function, server-side rendering will happen in a Deno-based edge function that's deployed close to the site visitor. If set to `false` (the default), the site will deploy to Node-based Netlify Functions.
5254

5355
```js
54-
// @errors: 2307
5556
/// file: svelte.config.js
5657
import adapter from '@sveltejs/adapter-netlify';
5758

58-
export default {
59+
/** @type {import('@sveltejs/kit').Config} */
60+
const config = {
5961
kit: {
6062
adapter: adapter({
6163
// will create a Netlify Edge Function using Deno-based
@@ -64,6 +66,8 @@ export default {
6466
})
6567
}
6668
};
69+
70+
export default config;
6771
```
6872

6973
## Netlify alternatives to SvelteKit functionality
@@ -92,10 +96,15 @@ During compilation, redirect rules are automatically appended to your `_redirect
9296
With this adapter, SvelteKit endpoints are hosted as [Netlify Functions](https://docs.netlify.com/functions/overview/). Netlify function handlers have additional context, including [Netlify Identity](https://docs.netlify.com/visitor-access/identity/) information. You can access this context via the `event.platform.context` field inside your hooks and `+page.server` or `+layout.server` endpoints. These are [serverless functions](https://docs.netlify.com/functions/overview/) when the `edge` property is `false` in the adapter config or [edge functions](https://docs.netlify.com/edge-functions/overview/#app) when it is `true`.
9397

9498
```js
95-
// @errors: 2705 7006
99+
// @errors: 2339
100+
// @filename: ambient.d.ts
101+
/// <reference types="@sveltejs/adapter-netlify" />
102+
// @filename: +page.server.js
103+
// ---cut---
96104
/// file: +page.server.js
105+
/** @type {import('./$types').PageServerLoad} */
97106
export const load = async (event) => {
98-
const context = event.platform.context;
107+
const context = event.platform?.context;
99108
console.log(context); // shows up in your functions log in the Netlify app
100109
};
101110
```

documentation/docs/25-build-and-deploy/90-adapter-vercel.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@ This adapter will be installed by default when you use [`adapter-auto`](adapter-
1111
Install with `npm i -D @sveltejs/adapter-vercel`, then add the adapter to your `svelte.config.js`:
1212

1313
```js
14-
// @errors: 2307 2345
1514
/// file: svelte.config.js
1615
import adapter from '@sveltejs/adapter-vercel';
1716

18-
export default {
17+
/** @type {import('@sveltejs/kit').Config} */
18+
const config = {
1919
kit: {
2020
adapter: adapter({
2121
// see below for options that can be set here
2222
})
2323
}
2424
};
25+
26+
export default config;
2527
```
2628

2729
## Deployment configuration
@@ -72,7 +74,8 @@ You may set the `images` config to control how Vercel builds your images. See th
7274
/// file: svelte.config.js
7375
import adapter from '@sveltejs/adapter-vercel';
7476

75-
export default {
77+
/** @type {import('@sveltejs/kit').Config} */
78+
const config = {
7679
kit: {
7780
adapter: adapter({
7881
images: {
@@ -84,6 +87,8 @@ export default {
8487
})
8588
}
8689
};
90+
91+
export default config;
8792
```
8893

8994
## Incremental Static Regeneration
@@ -95,9 +100,9 @@ Vercel supports [Incremental Static Regeneration](https://vercel.com/docs/increm
95100
To add ISR to a route, include the `isr` property in your `config` object:
96101

97102
```js
98-
// @errors: 2664
99103
import { BYPASS_TOKEN } from '$env/static/private';
100104

105+
/** @type {import('@sveltejs/adapter-vercel').Config} */
101106
export const config = {
102107
isr: {
103108
expiration: 60,
@@ -146,7 +151,6 @@ A list of valid query parameters that contribute to the cache key. Other paramet
146151
Vercel makes a set of [deployment-specific environment variables](https://vercel.com/docs/concepts/projects/environment-variables#system-environment-variables) available. Like other environment variables, these are accessible from `$env/static/private` and `$env/dynamic/private` (sometimes — more on that later), and inaccessible from their public counterparts. To access one of these variables from the client:
147152

148153
```js
149-
// @errors: 2305
150154
/// file: +layout.server.js
151155
import { VERCEL_COMMIT_REF } from '$env/static/private';
152156

0 commit comments

Comments
 (0)