Skip to content

Commit 6c16d9f

Browse files
committed
add types
1 parent 16bc021 commit 6c16d9f

File tree

9 files changed

+40
-31
lines changed

9 files changed

+40
-31
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ 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+
/** @type {import('@sveltejs/kit').Config} */
1617
export default {
1718
kit: {
1819
adapter: adapter()
@@ -64,21 +65,21 @@ node +++--env-file=.env+++ build
6465

6566
By default, the server will accept connections on `0.0.0.0` using port 3000. These can be customised with the `PORT` and `HOST` environment variables:
6667

67-
```
68+
```sh
6869
HOST=127.0.0.1 PORT=4000 node build
6970
```
7071

7172
Alternatively, the server can be configured to accept connections on a specified socket path. When this is done using the `SOCKET_PATH` environment variable, the `HOST` and `PORT` environment variables will be disregarded.
7273

73-
```
74+
```sh
7475
SOCKET_PATH=/tmp/socket node build
7576
```
7677

7778
### `ORIGIN`, `PROTOCOL_HEADER`, `HOST_HEADER`, and `PORT_HEADER`
7879

7980
HTTP doesn't give SvelteKit a reliable way to know the URL that is currently being requested. The simplest way to tell SvelteKit where the app is being served is to set the `ORIGIN` environment variable:
8081

81-
```
82+
```sh
8283
ORIGIN=https://my.site node build
8384

8485
# or e.g. for local previewing and testing
@@ -87,7 +88,7 @@ ORIGIN=http://localhost:3000 node build
8788

8889
With this, a request for the `/stuff` pathname will correctly resolve to `https://my.site/stuff`. Alternatively, you can specify headers that tell SvelteKit about the request protocol and host, from which it can construct the origin URL:
8990

90-
```
91+
```sh
9192
PROTOCOL_HEADER=x-forwarded-proto HOST_HEADER=x-forwarded-host node build
9293
```
9394

@@ -103,7 +104,7 @@ If `adapter-node` can't correctly determine the URL of your deployment, you may
103104

104105
The [`RequestEvent`](@sveltejs-kit#RequestEvent) object passed to hooks and endpoints includes an `event.getClientAddress()` function that returns the client's IP address. By default this is the connecting `remoteAddress`. If your server is behind one or more proxies (such as a load balancer), this value will contain the innermost proxy's IP address rather than the client's, so we need to specify an `ADDRESS_HEADER` to read the address from:
105106

106-
```
107+
```sh
107108
ADDRESS_HEADER=True-Client-IP node build
108109
```
109110

@@ -146,6 +147,7 @@ The adapter can be configured with various options:
146147
/// file: svelte.config.js
147148
import adapter from '@sveltejs/adapter-node';
148149

150+
/** @type {import('@sveltejs/kit').Config} */
149151
export default {
150152
kit: {
151153
adapter: adapter({

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Install with `npm i -D @sveltejs/adapter-static`, then add the adapter to your `
1515
/// file: svelte.config.js
1616
import adapter from '@sveltejs/adapter-static';
1717

18+
/** @type {import('@sveltejs/kit').Config} */
1819
export default {
1920
kit: {
2021
adapter: adapter({

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Install with `npm i -D @sveltejs/adapter-static`, then add the adapter to your `
2222
/// file: svelte.config.js
2323
import adapter from '@sveltejs/adapter-static';
2424

25+
/** @type {import('@sveltejs/kit').Config} */
2526
export default {
2627
kit: {
2728
adapter: adapter({

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ 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+
/** @type {import('@sveltejs/kit').Config} */
2324
export default {
2425
kit: {
2526
adapter: adapter({
@@ -126,6 +127,8 @@ The [`env`](https://developers.cloudflare.com/workers/runtime-apis/fetch-event#p
126127

127128
```js
128129
// @errors: 7031
130+
/// file: +server.js
131+
/** @type {import('./$types').RequestHandler} */
129132
export async function POST({ request, platform }) {
130133
const x = platform.env.YOUR_DURABLE_OBJECT_NAMESPACE.idFromName('x');
131134
}
@@ -197,6 +200,7 @@ Cloudflare no longer recommends using [Workers Sites](https://developers.cloudfl
197200
---import adapter from '@sveltejs/adapter-cloudflare-workers';---
198201
+++import adapter from '@sveltejs/adapter-cloudflare';+++
199202

203+
/** @type {import('@sveltejs/kit').Config} */
200204
export default {
201205
kit: {
202206
adapter: adapter()

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ 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+
/** @type {import('@sveltejs/kit').Config} */
1819
export default {
1920
kit: {
2021
adapter: adapter({
@@ -81,6 +82,8 @@ The [`env`](https://developers.cloudflare.com/workers/runtime-apis/fetch-event#p
8182

8283
```js
8384
// @errors: 7031
85+
/// file: +server.js
86+
/** @type {import('./$types').RequestHandler} */
8487
export async function POST({ request, platform }) {
8588
const x = platform.env.YOUR_DURABLE_OBJECT_NAMESPACE.idFromName('x');
8689
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Install with `npm i -D @sveltejs/adapter-netlify`, then add the adapter to your
1515
/// file: svelte.config.js
1616
import adapter from '@sveltejs/adapter-netlify';
1717

18+
/** @type {import('@sveltejs/kit').Config} */
1819
export default {
1920
kit: {
2021
// default options are shown
@@ -55,6 +56,7 @@ SvelteKit supports [Netlify Edge Functions](https://docs.netlify.com/netlify-lab
5556
/// file: svelte.config.js
5657
import adapter from '@sveltejs/adapter-netlify';
5758

59+
/** @type {import('@sveltejs/kit').Config} */
5860
export default {
5961
kit: {
6062
adapter: adapter({
@@ -94,6 +96,7 @@ With this adapter, SvelteKit endpoints are hosted as [Netlify Functions](https:/
9496
```js
9597
// @errors: 2705 7006
9698
/// file: +page.server.js
99+
/** @type {import('./$types').PageServerLoad} */
97100
export const load = async (event) => {
98101
const context = event.platform.context;
99102
console.log(context); // shows up in your functions log in the Netlify app

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Install with `npm i -D @sveltejs/adapter-vercel`, then add the adapter to your `
1515
/// file: svelte.config.js
1616
import adapter from '@sveltejs/adapter-vercel';
1717

18+
/** @type {import('@sveltejs/kit').Config} */
1819
export default {
1920
kit: {
2021
adapter: adapter({
@@ -72,6 +73,7 @@ You may set the `images` config to control how Vercel builds your images. See th
7273
/// file: svelte.config.js
7374
import adapter from '@sveltejs/adapter-vercel';
7475

76+
/** @type {import('@sveltejs/kit').Config} */
7577
export default {
7678
kit: {
7779
adapter: adapter({
@@ -98,6 +100,7 @@ To add ISR to a route, include the `isr` property in your `config` object:
98100
// @errors: 2664
99101
import { BYPASS_TOKEN } from '$env/static/private';
100102

103+
/** @type {import('@sveltejs/adapter-vercel').Config} */
101104
export const config = {
102105
isr: {
103106
expiration: 60,

documentation/docs/30-advanced/40-service-workers.md

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,23 @@ The following example caches the built app and any files in `static` eagerly, an
2424

2525
```js
2626
// @errors: 2339
27+
/// file: src/service-worker.js
28+
// Disables access to DOM typings like `HTMLElement` which are not available
29+
// inside a service worker and instantiates the correct globals
30+
/// <reference no-default-lib="true"/>
31+
/// <reference lib="esnext" />
32+
/// <reference lib="webworker" />
33+
// Ensures that the `$service-worker` import has proper type definitions
2734
/// <reference types="@sveltejs/kit" />
35+
// Only necessary if you have an import from `$env/static/public`
36+
/// <reference types="../.svelte-kit/ambient.d.ts" />
37+
2838
import { build, files, version } from '$service-worker';
2939

40+
// The reassignment of `self` to `sw` allows you to type cast it in the process
41+
// (this is the easiest way to do it without needing additional files)
42+
const sw = /** @type {ServiceWorkerGlobalScope} */ (/** @type {unknown} */ (self));
43+
3044
// Create a unique cache name for this deployment
3145
const CACHE = `cache-${version}`;
3246

@@ -35,7 +49,7 @@ const ASSETS = [
3549
...files // everything in `static`
3650
];
3751

38-
self.addEventListener('install', (event) => {
52+
sw.addEventListener('install', (event) => {
3953
// Create a new cache and add all files to it
4054
async function addFilesToCache() {
4155
const cache = await caches.open(CACHE);
@@ -45,7 +59,7 @@ self.addEventListener('install', (event) => {
4559
event.waitUntil(addFilesToCache());
4660
});
4761

48-
self.addEventListener('activate', (event) => {
62+
sw.addEventListener('activate', (event) => {
4963
// Remove previous cached data from disk
5064
async function deleteOldCaches() {
5165
for (const key of await caches.keys()) {
@@ -56,7 +70,7 @@ self.addEventListener('activate', (event) => {
5670
event.waitUntil(deleteOldCaches());
5771
});
5872

59-
self.addEventListener('fetch', (event) => {
73+
sw.addEventListener('fetch', (event) => {
6074
// ignore POST requests etc
6175
if (event.request.method !== 'GET') return;
6276

@@ -122,29 +136,6 @@ navigator.serviceWorker.register('/service-worker.js', {
122136

123137
> [!NOTE] `build` and `prerendered` are empty arrays during development
124138
125-
## Type safety
126-
127-
Setting up proper types for service workers requires some manual setup. Inside your `service-worker.js`, add the following to the top of your file:
128-
129-
```original-js
130-
/// <reference types="@sveltejs/kit" />
131-
/// <reference no-default-lib="true"/>
132-
/// <reference lib="esnext" />
133-
/// <reference lib="webworker" />
134-
135-
const sw = /** @type {ServiceWorkerGlobalScope} */ (/** @type {unknown} */ (self));
136-
```
137-
```generated-ts
138-
/// <reference types="@sveltejs/kit" />
139-
/// <reference no-default-lib="true"/>
140-
/// <reference lib="esnext" />
141-
/// <reference lib="webworker" />
142-
143-
const sw = self as unknown as ServiceWorkerGlobalScope;
144-
```
145-
146-
This disables access to DOM typings like `HTMLElement` which are not available inside a service worker and instantiates the correct globals. The reassignment of `self` to `sw` allows you to type cast it in the process (there are a couple of ways to do this, but this is the easiest that requires no additional files). Use `sw` instead of `self` in the rest of the file. The reference to the SvelteKit types ensures that the `$service-worker` import has proper type definitions. If you import `$env/static/public` you either have to `// @ts-ignore` the import or add `/// <reference types="../.svelte-kit/ambient.d.ts" />` to the reference types.
147-
148139
## Other solutions
149140

150141
SvelteKit's service worker implementation is designed to be easy to work with and is probably a good solution for most users. However, outside of SvelteKit, many PWA applications leverage the [Workbox](https://web.dev/learn/pwa/workbox) library. If you're used to using Workbox you may prefer [Vite PWA plugin](https://vite-pwa-org.netlify.app/frameworks/sveltekit.html).

documentation/docs/60-appendix/20-integrations.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Including [`vitePreprocess`](https://github.com/sveltejs/vite-plugin-svelte/blob
1010
// svelte.config.js
1111
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
1212

13+
/** @type {import('@sveltejs/kit').Config} */
1314
export default {
1415
preprocess: [vitePreprocess()]
1516
};

0 commit comments

Comments
 (0)