You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: apps/svelte.dev/content/docs/kit/25-build-and-deploy/60-adapter-cloudflare.md
+29-15Lines changed: 29 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ NOTE: do not edit this file, it is generated in apps/svelte.dev/scripts/sync-doc
3
3
title: Cloudflare Pages
4
4
---
5
5
6
-
To deploy to [Cloudflare Pages](https://developers.cloudflare.com/pages/), use [`adapter-cloudflare`](https://github.com/sveltejs/kit/tree/main/packages/adapter-cloudflare).
6
+
To deploy to [Cloudflare Pages](https://pages.cloudflare.com/), use [`adapter-cloudflare`](https://github.com/sveltejs/kit/tree/main/packages/adapter-cloudflare).
7
7
8
8
This adapter will be installed by default when you use [`adapter-auto`](adapter-auto). If you plan on staying with Cloudflare Pages, you can switch from [`adapter-auto`](adapter-auto) to using this adapter directly so that values specific to Cloudflare Workers are emulated during local development, type declarations are automatically applied, and the ability to set Cloudflare-specific options is provided.
9
9
@@ -31,10 +31,9 @@ export default {
31
31
exclude: ['<all>']
32
32
},
33
33
platformProxy: {
34
-
configPath:'wrangler.toml',
34
+
configPath:undefined,
35
35
environment:undefined,
36
-
experimentalJsonConfig:false,
37
-
persist:false
36
+
persist:undefined
38
37
}
39
38
})
40
39
}
@@ -45,7 +44,7 @@ export default {
45
44
46
45
### routes
47
46
48
-
Allows you to customise the [`_routes.json`](https://developers.cloudflare.com/pages/platform/functions/routing/#create-a-_routesjson-file) file generated by `adapter-cloudflare`.
47
+
Allows you to customise the [`_routes.json`](https://developers.cloudflare.com/pages/functions/routing/#create-a-_routesjson-file) file generated by `adapter-cloudflare`.
49
48
50
49
-`include` defines routes that will invoke a function, and defaults to `['/*']`
51
50
-`exclude` defines routes that will _not_ invoke a function — this is a faster and cheaper way to serve your app's static assets. This array can include the following special values:
@@ -58,11 +57,11 @@ You can have up to 100 `include` and `exclude` rules combined. Generally you can
58
57
59
58
### platformProxy
60
59
61
-
Preferences for the emulated `platform.env` local bindings. See the [getPlatformProxy](https://developers.cloudflare.com/workers/wrangler/api/#syntax) Wrangler API documentation for a full list of options.
60
+
Preferences for the emulated `platform.env` local bindings. See the [getPlatformProxy](https://developers.cloudflare.com/workers/wrangler/api/#parameters-1) Wrangler API documentation for a full list of options.
62
61
63
62
## Deployment
64
63
65
-
Please follow the [Get Started Guide](https://developers.cloudflare.com/pages/get-started) for Cloudflare Pages to begin.
64
+
Please follow the [Get Started Guide](https://developers.cloudflare.com/pages/get-started/) for Cloudflare Pages to begin.
66
65
67
66
When configuring your project settings, you must use the following settings:
68
67
@@ -72,7 +71,7 @@ When configuring your project settings, you must use the following settings:
72
71
73
72
## Runtime APIs
74
73
75
-
The [`env`](https://developers.cloudflare.com/workers/runtime-apis/fetch-event#parameters) object contains your project's [bindings](https://developers.cloudflare.com/pages/platform/functions/bindings/), which consist of KV/DO namespaces, etc. It is passed to SvelteKit via the `platform` property, along with [`context`](https://developers.cloudflare.com/workers/runtime-apis/handlers/fetch/#contextwaituntil), [`caches`](https://developers.cloudflare.com/workers/runtime-apis/cache/), and [`cf`](https://developers.cloudflare.com/workers/runtime-apis/request/#the-cf-property-requestinitcfproperties), meaning that you can access it in hooks and endpoints:
74
+
The [`env`](https://developers.cloudflare.com/workers/runtime-apis/fetch-event#parameters) object contains your project's [bindings](https://developers.cloudflare.com/pages/functions/bindings/), which consist of KV/DO namespaces, etc. It is passed to SvelteKit via the `platform` property, along with [`context`](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:
> [!NOTE] SvelteKit's built-in `$env` module should be preferred for environment variables.
83
+
> [!NOTE] SvelteKit's built-in [`$env` module]($env-static-private) should be preferred for environment variables.
85
84
86
-
To make these types available to your app, install `@cloudflare/workers-types` and reference them in your `src/app.d.ts`:
85
+
To make these types available to your app, install [`@cloudflare/workers-types`](https://www.npmjs.com/package/@cloudflare/workers-types) and reference them in your `src/app.d.ts`:
87
86
88
87
```ts
89
88
/// file: src/app.d.ts
@@ -105,23 +104,38 @@ export {};
105
104
106
105
### Testing Locally
107
106
108
-
Cloudflare Workers specific values in the `platform` property are emulated during dev and preview modes. Local [bindings](https://developers.cloudflare.com/workers/wrangler/configuration/#bindings) are created based on the configuration in your `wrangler.toml`file and are used to populate `platform.env` during development and preview. Use the adapter config [`platformProxy` option](#Options-platformProxy) to change your preferences for the bindings.
107
+
Cloudflare Workers specific values in the `platform` property are emulated during dev and preview modes. Local [bindings](https://developers.cloudflare.com/pages/functions/bindings/) are created based on your [Wrangler configuration file](https://developers.cloudflare.com/pages/functions/wrangler-configuration/#local-development) and are used to populate `platform.env` during development and preview. Use the adapter config [`platformProxy` option](#Options-platformProxy) to change your preferences for the bindings.
109
108
110
-
For testing the build, you should use [wrangler](https://developers.cloudflare.com/workers/cli-wrangler)**version 3**. Once you have built your site, run `wrangler pages dev .svelte-kit/cloudflare`.
109
+
For testing the build, you should use [Wrangler](https://developers.cloudflare.com/workers/wrangler/)**version 3**. Once you have built your site, run `wrangler pages dev .svelte-kit/cloudflare`.
111
110
112
111
## Notes
113
112
114
-
Functions contained in the `/functions` directory at the project's root will _not_ be included in the deployment, which is compiled to a [single `_worker.js` file](https://developers.cloudflare.com/pages/platform/functions/#advanced-mode). Functions should be implemented as [server endpoints](routing#server) in your SvelteKit app.
113
+
Functions contained in the [`/functions` directory](https://developers.cloudflare.com/pages/functions/routing/) at the project's root will _not_ be included in the deployment. Instead, functions should be implemented as [server endpoints](routing#server) in your SvelteKit app, which is compiled to a [single `_worker.js` file](https://developers.cloudflare.com/pages/functions/advanced-mode/).
115
114
116
-
The `_headers` and `_redirects` files specific to Cloudflare Pages can be used for static asset responses (like images) by putting them into the `/static` folder.
115
+
The [`_headers`](https://developers.cloudflare.com/pages/configuration/headers/) and [`_redirects`](https://developers.cloudflare.com/pages/configuration/redirects/) files specific to Cloudflare Pages can be used for static asset responses (like images) by putting them into the `/static` folder.
117
116
118
117
However, they will have no effect on responses dynamically rendered by SvelteKit, which should return custom headers or redirect responses from [server endpoints](routing#server) or with the [`handle`](hooks#Server-hooks-handle) hook.
119
118
120
119
## Troubleshooting
121
120
122
121
### Further reading
123
122
124
-
You may wish to refer to [Cloudflare's documentation for deploying a SvelteKit site](https://developers.cloudflare.com/pages/framework-guides/deploy-a-svelte-kit-site).
123
+
You may wish to refer to [Cloudflare's documentation for deploying a SvelteKit site](https://developers.cloudflare.com/pages/framework-guides/deploy-a-svelte-kit-site/).
124
+
125
+
### Node.js compatibility
126
+
127
+
If you would like to enable [Node.js compatibility](https://developers.cloudflare.com/workers/runtime-apis/nodejs/), you can add the `nodejs_compat` compatibility flag to your Wrangler configuration file:
128
+
129
+
```jsonc
130
+
/// file: wrangler.jsonc
131
+
{
132
+
"compatibility_flags": ["nodejs_compat"]
133
+
}
134
+
```
135
+
136
+
### Worker size limits
137
+
138
+
When deploying your application, the server generated by SvelteKit is bundled into a single file. Wrangler will fail to publish your worker if it exceeds [the size limits](https://developers.cloudflare.com/workers/platform/limits/#worker-size) after minification. You're unlikely to hit this limit usually, but some large libraries can cause this to happen. In that case, you can try to reduce the size of your worker by only importing such libraries on the client side. See [the FAQ](./faq#How-do-I-use-a-client-side-library-accessing-document-or-window) for more information.
Copy file name to clipboardExpand all lines: apps/svelte.dev/content/docs/kit/25-build-and-deploy/70-adapter-cloudflare-workers.md
+43-47Lines changed: 43 additions & 47 deletions
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ title: Cloudflare Workers
5
5
6
6
To deploy to [Cloudflare Workers](https://workers.cloudflare.com/), use [`adapter-cloudflare-workers`](https://github.com/sveltejs/kit/tree/main/packages/adapter-cloudflare-workers).
7
7
8
-
> [!NOTE] Unless you have a specific reason to use `adapter-cloudflare-workers`, it's recommended that you use `adapter-cloudflare` instead. Both adapters have equivalent functionality, but Cloudflare Pages offers features like GitHub integration with automatic builds and deploys, preview deployments, instant rollback and so on.
8
+
> [!NOTE] Unless you have a specific reason to use `adapter-cloudflare-workers`, it's recommended that you use [`adapter-cloudflare`](adapter-cloudflare) instead. Both adapters have equivalent functionality, but Cloudflare Pages offers features like GitHub integration with automatic builds and deploys, preview deployments, instant rollback and so on.
9
9
10
10
## Usage
11
11
@@ -19,13 +19,7 @@ import adapter from '@sveltejs/adapter-cloudflare-workers';
19
19
exportdefault {
20
20
kit: {
21
21
adapter:adapter({
22
-
config:'wrangler.toml',
23
-
platformProxy: {
24
-
configPath:'wrangler.toml',
25
-
environment:undefined,
26
-
experimentalJsonConfig:false,
27
-
persist:false
28
-
}
22
+
// see below for options that can be set here
29
23
})
30
24
}
31
25
};
@@ -35,42 +29,44 @@ export default {
35
29
36
30
### config
37
31
38
-
Path to your custom `wrangler.toml` or `wrangler.json` config file.
32
+
Path to your [Wrangler configuration file](https://developers.cloudflare.com/workers/wrangler/configuration/). If you would like to use a Wrangler configuration filename other than `wrangler.jsonc`, you can specify it using this option.
39
33
40
34
### platformProxy
41
35
42
-
Preferences for the emulated `platform.env` local bindings. See the [getPlatformProxy](https://developers.cloudflare.com/workers/wrangler/api/#syntax) Wrangler API documentation for a full list of options.
36
+
Preferences for the emulated `platform.env` local bindings. See the [getPlatformProxy](https://developers.cloudflare.com/workers/wrangler/api/#parameters-1) Wrangler API documentation for a full list of options.
43
37
44
38
## Basic Configuration
45
39
46
-
This adapter expects to find a [wrangler.toml/wrangler.json](https://developers.cloudflare.com/workers/platform/sites/configuration) file in the project root. It should look something like this:
47
-
48
-
```toml
49
-
/// file: wrangler.toml
50
-
name = "<your-service-name>"
51
-
account_id = "<your-account-id>"
52
-
53
-
main = "./.cloudflare/worker.js"
54
-
site.bucket = "./.cloudflare/public"
55
-
56
-
build.command = "npm run build"
57
-
58
-
compatibility_date = "2021-11-12"
59
-
workers_dev = true
40
+
This adapter expects to find a [Wrangler configuration file](https://developers.cloudflare.com/workers/configuration/sites/configuration/) in the project root. It should look something like this:
41
+
42
+
```jsonc
43
+
/// file: wrangler.jsonc
44
+
{
45
+
"name":"<your-service-name>",
46
+
"account_id":"<your-account-id>",
47
+
"main":"./.cloudflare/worker.js",
48
+
"site": {
49
+
"bucket":"./.cloudflare/public"
50
+
},
51
+
"build": {
52
+
"command":"npm run build"
53
+
},
54
+
"compatibility_date":"2021-11-12"
55
+
}
60
56
```
61
57
62
-
`<your-service-name>` can be anything. `<your-account-id>` can be found by logging into your [Cloudflare dashboard](https://dash.cloudflare.com) and grabbing it from the end of the URL:
58
+
`<your-service-name>` can be anything. `<your-account-id>` can be found by running `wrangler whoami` using the Wrangler CLI tool or by logging into your [Cloudflare dashboard](https://dash.cloudflare.com) and grabbing it from the end of the URL:
> [!NOTE] You should add the `.cloudflare` directory (or whichever directories you specified for `main` and `site.bucket`) to your `.gitignore`.
64
+
> [!NOTE] You should add the `.cloudflare` directory (or whichever directories you specified for `main` and `site.bucket`) and the `.wrangler` directory to your `.gitignore`.
69
65
70
-
You will need to install [wrangler](https://developers.cloudflare.com/workers/wrangler/get-started/) and log in, if you haven't already:
66
+
You will need to install [Wrangler](https://developers.cloudflare.com/workers/wrangler/install-and-update/) and log in, if you haven't already:
71
67
72
-
```
73
-
npm i -g wrangler
68
+
```sh
69
+
npm i -D wrangler
74
70
wrangler login
75
71
```
76
72
@@ -80,20 +76,9 @@ Then, you can build your app and deploy it:
80
76
wrangler deploy
81
77
```
82
78
83
-
## Custom config
84
-
85
-
If you would like to use a config file other than `wrangler.toml` you can specify so using the [`config` option](#Options-config).
86
-
87
-
If you would like to enable [Node.js compatibility](https://developers.cloudflare.com/workers/runtime-apis/nodejs/#enable-nodejs-from-the-cloudflare-dashboard), you can add "nodejs_compat" flag to `wrangler.toml`:
88
-
89
-
```toml
90
-
/// file: wrangler.toml
91
-
compatibility_flags = [ "nodejs_compat" ]
92
-
```
93
-
94
79
## Runtime APIs
95
80
96
-
The [`env`](https://developers.cloudflare.com/workers/runtime-apis/fetch-event#parameters) object contains your project's [bindings](https://developers.cloudflare.com/pages/platform/functions/bindings/), which consist of KV/DO namespaces, etc. It is passed to SvelteKit via the `platform` property, along with [`context`](https://developers.cloudflare.com/workers/runtime-apis/handlers/fetch/#contextwaituntil), [`caches`](https://developers.cloudflare.com/workers/runtime-apis/cache/), and [`cf`](https://developers.cloudflare.com/workers/runtime-apis/request/#the-cf-property-requestinitcfproperties), meaning that you can access it in hooks and endpoints:
81
+
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 [`context`](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:
> [!NOTE] SvelteKit's built-in `$env` module should be preferred for environment variables.
90
+
> [!NOTE] SvelteKit's built-in [`$env` module]($env-static-private) should be preferred for environment variables.
106
91
107
-
To make these types available to your app, install `@cloudflare/workers-types` and reference them in your `src/app.d.ts`:
92
+
To make these types available to your app, install [`@cloudflare/workers-types`](https://www.npmjs.com/package/@cloudflare/workers-types) and reference them in your `src/app.d.ts`:
108
93
109
94
```ts
110
95
/// file: src/app.d.ts
@@ -126,15 +111,26 @@ export {};
126
111
127
112
### Testing Locally
128
113
129
-
Cloudflare Workers specific values in the `platform` property are emulated during dev and preview modes. Local [bindings](https://developers.cloudflare.com/workers/wrangler/configuration/#bindings) are created based on the configuration in your `wrangler.toml`file and are used to populate `platform.env` during development and preview. Use the adapter config [`platformProxy` option](#Options-platformProxy) to change your preferences for the bindings.
114
+
Cloudflare Workers specific values in the `platform` property are emulated during dev and preview modes. Local [bindings](https://developers.cloudflare.com/workers/wrangler/configuration/#bindings) are created based on your [Wrangler configuration file](https://developers.cloudflare.com/workers/wrangler/) and are used to populate `platform.env` during development and preview. Use the adapter config [`platformProxy` option](#Options-platformProxy) to change your preferences for the bindings.
130
115
131
-
For testing the build, you should use [wrangler](https://developers.cloudflare.com/workers/cli-wrangler)**version 3**. Once you have built your site, run `wrangler dev`.
116
+
For testing the build, you should use [Wrangler](https://developers.cloudflare.com/workers/wrangler/)**version 3**. Once you have built your site, run `wrangler dev`.
132
117
133
118
## Troubleshooting
134
119
120
+
### Node.js compatibility
121
+
122
+
If you would like to enable [Node.js compatibility](https://developers.cloudflare.com/workers/runtime-apis/nodejs/), you can add the `nodejs_compat` compatibility flag to your Wrangler configuration file:
123
+
124
+
```jsonc
125
+
/// file: wrangler.jsonc
126
+
{
127
+
"compatibility_flags": ["nodejs_compat"]
128
+
}
129
+
```
130
+
135
131
### Worker size limits
136
132
137
-
When deploying to workers, the server generated by SvelteKit is bundled into a single file. Wrangler will fail to publish your worker if it exceeds [the size limits](https://developers.cloudflare.com/workers/platform/limits/#worker-size) after minification. You're unlikely to hit this limit usually, but some large libraries can cause this to happen. In that case, you can try to reduce the size of your worker by only importing such libraries on the client side. See [the FAQ](./faq#How-do-I-use-a-client-side-library-accessing-document-or-window) for more information.
133
+
When deploying your application, the server generated by SvelteKit is bundled into a single file. Wrangler will fail to publish your worker if it exceeds [the size limits](https://developers.cloudflare.com/workers/platform/limits/#worker-size) after minification. You're unlikely to hit this limit usually, but some large libraries can cause this to happen. In that case, you can try to reduce the size of your worker by only importing such libraries on the client side. See [the FAQ](./faq#How-do-I-use-a-client-side-library-accessing-document-or-window) for more information.
Copy file name to clipboardExpand all lines: apps/svelte.dev/content/docs/kit/30-advanced/20-hooks.md
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -300,6 +300,8 @@ The `lang` parameter will be correctly derived from the returned pathname.
300
300
301
301
Using `reroute` will _not_ change the contents of the browser's address bar, or the value of `event.url`.
302
302
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.
304
+
303
305
### transport
304
306
305
307
This is a collection of _transporters_, which allow you to pass custom types — returned from `load` and form actions — across the server/client boundary. Each transporter contains an `encode` function, which encodes values on the server (or returns `false` for anything that isn't an instance of the type) and a corresponding `decode` function:
0 commit comments