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: documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md
+29-15Lines changed: 29 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
title: Cloudflare Pages
3
3
---
4
4
5
-
To deploy to [Cloudflare Pages](https://developers.cloudflare.com/pages/), use [`adapter-cloudflare`](https://github.com/sveltejs/kit/tree/main/packages/adapter-cloudflare).
5
+
To deploy to [Cloudflare Pages](https://pages.cloudflare.com/), use [`adapter-cloudflare`](https://github.com/sveltejs/kit/tree/main/packages/adapter-cloudflare).
6
6
7
7
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.
8
8
@@ -30,10 +30,9 @@ export default {
30
30
exclude: ['<all>']
31
31
},
32
32
platformProxy: {
33
-
configPath:'wrangler.toml',
33
+
configPath:undefined,
34
34
environment:undefined,
35
-
experimentalJsonConfig:false,
36
-
persist:false
35
+
persist:undefined
37
36
}
38
37
})
39
38
}
@@ -44,7 +43,7 @@ export default {
44
43
45
44
### routes
46
45
47
-
Allows you to customise the [`_routes.json`](https://developers.cloudflare.com/pages/platform/functions/routing/#create-a-_routesjson-file) file generated by `adapter-cloudflare`.
46
+
Allows you to customise the [`_routes.json`](https://developers.cloudflare.com/pages/functions/routing/#create-a-_routesjson-file) file generated by `adapter-cloudflare`.
48
47
49
48
-`include` defines routes that will invoke a function, and defaults to `['/*']`
50
49
-`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:
@@ -57,11 +56,11 @@ You can have up to 100 `include` and `exclude` rules combined. Generally you can
57
56
58
57
### platformProxy
59
58
60
-
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.
59
+
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.
61
60
62
61
## Deployment
63
62
64
-
Please follow the [Get Started Guide](https://developers.cloudflare.com/pages/get-started) for Cloudflare Pages to begin.
63
+
Please follow the [Get Started Guide](https://developers.cloudflare.com/pages/get-started/) for Cloudflare Pages to begin.
65
64
66
65
When configuring your project settings, you must use the following settings:
67
66
@@ -71,7 +70,7 @@ When configuring your project settings, you must use the following settings:
71
70
72
71
## Runtime APIs
73
72
74
-
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:
73
+
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.
82
+
> [!NOTE] SvelteKit's built-in [`$env` module]($env-static-private) should be preferred for environment variables.
84
83
85
-
To make these types available to your app, install `@cloudflare/workers-types` and reference them in your `src/app.d.ts`:
84
+
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`:
86
85
87
86
```ts
88
87
/// file: src/app.d.ts
@@ -104,23 +103,38 @@ export {};
104
103
105
104
### Testing Locally
106
105
107
-
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.
106
+
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.
108
107
109
-
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`.
108
+
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`.
110
109
111
110
## Notes
112
111
113
-
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.
112
+
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/).
114
113
115
-
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.
114
+
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.
116
115
117
116
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.
118
117
119
118
## Troubleshooting
120
119
121
120
### Further reading
122
121
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).
122
+
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
+
124
+
### Node.js compatibility
125
+
126
+
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:
127
+
128
+
```jsonc
129
+
/// file: wrangler.jsonc
130
+
{
131
+
"compatibility_flags": ["nodejs_compat"]
132
+
}
133
+
```
134
+
135
+
### Worker size limits
136
+
137
+
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: documentation/docs/25-build-and-deploy/70-adapter-cloudflare-workers.md
+43-47Lines changed: 43 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ title: Cloudflare Workers
4
4
5
5
To deploy to [Cloudflare Workers](https://workers.cloudflare.com/), use [`adapter-cloudflare-workers`](https://github.com/sveltejs/kit/tree/main/packages/adapter-cloudflare-workers).
6
6
7
-
> [!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.
7
+
> [!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.
8
8
9
9
## Usage
10
10
@@ -18,13 +18,7 @@ import adapter from '@sveltejs/adapter-cloudflare-workers';
18
18
exportdefault {
19
19
kit: {
20
20
adapter:adapter({
21
-
config:'wrangler.toml',
22
-
platformProxy: {
23
-
configPath:'wrangler.toml',
24
-
environment:undefined,
25
-
experimentalJsonConfig:false,
26
-
persist:false
27
-
}
21
+
// see below for options that can be set here
28
22
})
29
23
}
30
24
};
@@ -34,42 +28,44 @@ export default {
34
28
35
29
### config
36
30
37
-
Path to your custom `wrangler.toml` or `wrangler.json` config file.
31
+
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.
38
32
39
33
### platformProxy
40
34
41
-
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.
35
+
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.
42
36
43
37
## Basic Configuration
44
38
45
-
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:
46
-
47
-
```toml
48
-
/// file: wrangler.toml
49
-
name = "<your-service-name>"
50
-
account_id = "<your-account-id>"
51
-
52
-
main = "./.cloudflare/worker.js"
53
-
site.bucket = "./.cloudflare/public"
54
-
55
-
build.command = "npm run build"
56
-
57
-
compatibility_date = "2021-11-12"
58
-
workers_dev = true
39
+
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:
40
+
41
+
```jsonc
42
+
/// file: wrangler.jsonc
43
+
{
44
+
"name":"<your-service-name>",
45
+
"account_id":"<your-account-id>",
46
+
"main":"./.cloudflare/worker.js",
47
+
"site": {
48
+
"bucket":"./.cloudflare/public"
49
+
},
50
+
"build": {
51
+
"command":"npm run build"
52
+
},
53
+
"compatibility_date":"2021-11-12"
54
+
}
59
55
```
60
56
61
-
`<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:
57
+
`<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`.
63
+
> [!NOTE] You should add the `.cloudflare` directory (or whichever directories you specified for `main` and `site.bucket`) and the `.wrangler` directory to your `.gitignore`.
68
64
69
-
You will need to install [wrangler](https://developers.cloudflare.com/workers/wrangler/get-started/) and log in, if you haven't already:
65
+
You will need to install [Wrangler](https://developers.cloudflare.com/workers/wrangler/install-and-update/) and log in, if you haven't already:
70
66
71
-
```
72
-
npm i -g wrangler
67
+
```sh
68
+
npm i -D wrangler
73
69
wrangler login
74
70
```
75
71
@@ -79,20 +75,9 @@ Then, you can build your app and deploy it:
79
75
wrangler deploy
80
76
```
81
77
82
-
## Custom config
83
-
84
-
If you would like to use a config file other than `wrangler.toml` you can specify so using the [`config` option](#Options-config).
85
-
86
-
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`:
87
-
88
-
```toml
89
-
/// file: wrangler.toml
90
-
compatibility_flags = [ "nodejs_compat" ]
91
-
```
92
-
93
78
## Runtime APIs
94
79
95
-
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:
80
+
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.
89
+
> [!NOTE] SvelteKit's built-in [`$env` module]($env-static-private) should be preferred for environment variables.
105
90
106
-
To make these types available to your app, install `@cloudflare/workers-types` and reference them in your `src/app.d.ts`:
91
+
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`:
107
92
108
93
```ts
109
94
/// file: src/app.d.ts
@@ -125,15 +110,26 @@ export {};
125
110
126
111
### Testing Locally
127
112
128
-
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.
113
+
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.
129
114
130
-
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`.
115
+
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`.
131
116
132
117
## Troubleshooting
133
118
119
+
### Node.js compatibility
120
+
121
+
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:
122
+
123
+
```jsonc
124
+
/// file: wrangler.jsonc
125
+
{
126
+
"compatibility_flags": ["nodejs_compat"]
127
+
}
128
+
```
129
+
134
130
### Worker size limits
135
131
136
-
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.
132
+
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.
0 commit comments