Skip to content

Commit ebec1e9

Browse files
feat: deprecate adapter-cloudflare-workers in favour of adapter-cloudflare (#13634)
* merge adapters * changeset * it's valid to not specify a main entry if the user is only deploying static assets * format * adjust docs * also use cf pages env var * fix migration guide link * try to fix doc build error * Update index.d.ts Co-authored-by: Ben McCann <[email protected]> * Update kind-carrots-deliver.md Co-authored-by: Ben McCann <[email protected]> * validate assets key if main key is set * delete adapter-cloudflare-workers * adjust docs and workspace for removing cloudflare workers * fix docs title * clarify _routes.json belongs to cloudflare pages * Update packages/adapter-cloudflare/src/worker.js Co-authored-by: Ben McCann <[email protected]> * changes according to feedback * revert type change * fix lock file * fixes * tests * fix lockfile * format --------- Co-authored-by: Ben McCann <[email protected]>
1 parent f4ce185 commit ebec1e9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+599
-1367
lines changed

.changeset/kind-carrots-deliver.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-cloudflare': major
3+
---
4+
5+
feat: support specifically building for Cloudflare Workers Static Assets

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ Web development, streamlined. Read the [documentation](https://svelte.dev/docs/k
1111
| [@sveltejs/kit](packages/kit) | [Changelog](packages/kit/CHANGELOG.md) |
1212
| [@sveltejs/adapter-auto](packages/adapter-auto) | [Changelog](packages/adapter-auto/CHANGELOG.md) |
1313
| [@sveltejs/adapter-cloudflare](packages/adapter-cloudflare) | [Changelog](packages/adapter-cloudflare/CHANGELOG.md) |
14-
| [@sveltejs/adapter-cloudflare-workers](packages/adapter-cloudflare-workers) | [Changelog](packages/adapter-cloudflare-workers/CHANGELOG.md) |
1514
| [@sveltejs/adapter-netlify](packages/adapter-netlify) | [Changelog](packages/adapter-netlify/CHANGELOG.md) |
1615
| [@sveltejs/adapter-node](packages/adapter-node) | [Changelog](packages/adapter-node/CHANGELOG.md) |
1716
| [@sveltejs/adapter-static](packages/adapter-static) | [Changelog](packages/adapter-static/CHANGELOG.md) |

documentation/docs/25-build-and-deploy/20-adapters.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ Before you can deploy your SvelteKit app, you need to _adapt_ it for your deploy
66

77
Official adapters exist for a variety of platforms — these are documented on the following pages:
88

9-
- [`@sveltejs/adapter-cloudflare`](adapter-cloudflare) for Cloudflare Pages
10-
- [`@sveltejs/adapter-cloudflare-workers`](adapter-cloudflare-workers) for Cloudflare Workers
9+
- [`@sveltejs/adapter-cloudflare`](adapter-cloudflare) for Cloudflare Workers and Cloudflare Pages
1110
- [`@sveltejs/adapter-netlify`](adapter-netlify) for Netlify
1211
- [`@sveltejs/adapter-node`](adapter-node) for Node servers
1312
- [`@sveltejs/adapter-static`](adapter-static) for static site generation (SSG)

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

Lines changed: 115 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,38 @@
22
title: Cloudflare Pages
33
---
44

5-
To deploy to [Cloudflare Pages](https://pages.cloudflare.com/), use [`adapter-cloudflare`](https://github.com/sveltejs/kit/tree/main/packages/adapter-cloudflare).
5+
To deploy to [Cloudflare Workers](https://workers.cloudflare.com/) or [Cloudflare Pages](https://pages.cloudflare.com/), use [`adapter-cloudflare`](https://github.com/sveltejs/kit/tree/main/packages/adapter-cloudflare).
66

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 `event.platform` is emulated during local development, type declarations are automatically applied, and the ability to set Cloudflare-specific options is provided.
7+
This adapter will be installed by default when you use [`adapter-auto`](adapter-auto). If you plan on staying with Cloudflare, you can switch from [`adapter-auto`](adapter-auto) to using this adapter directly so that `event.platform` is emulated during local development, type declarations are automatically applied, and the ability to set Cloudflare-specific options is provided.
88

99
## Comparisons
1010

11-
- `adapter-cloudflare` – supports all SvelteKit features; builds for [Cloudflare Pages](https://blog.cloudflare.com/cloudflare-pages-goes-full-stack/)
12-
- `adapter-cloudflare-workers`supports all SvelteKit features; builds for Cloudflare Workers
13-
- `adapter-static` – only produces client-side static assets; compatible with Cloudflare Pages
11+
- `adapter-cloudflare` – supports all SvelteKit features; builds for Cloudflare Workers Static Assets and Cloudflare Pages
12+
- `adapter-cloudflare-workers`deprecated. Supports all SvelteKit features; builds for Cloudflare Workers Sites
13+
- `adapter-static` – only produces client-side static assets; compatible with Cloudflare Workers Static Assets and Cloudflare Pages
1414

1515
## Usage
1616

1717
Install with `npm i -D @sveltejs/adapter-cloudflare`, then add the adapter to your `svelte.config.js`:
1818

1919
```js
20-
// @errors: 2307
2120
/// file: svelte.config.js
2221
import adapter from '@sveltejs/adapter-cloudflare';
2322

2423
export default {
2524
kit: {
2625
adapter: adapter({
2726
// See below for an explanation of these options
28-
routes: {
29-
include: ['/*'],
30-
exclude: ['<all>']
31-
},
27+
config: undefined,
3228
platformProxy: {
3329
configPath: undefined,
3430
environment: undefined,
3531
persist: undefined
32+
},
33+
fallback: 'plaintext',
34+
routes: {
35+
include: ['/*'],
36+
exclude: ['<all>']
3637
}
3738
})
3839
}
@@ -41,9 +42,31 @@ export default {
4142

4243
## Options
4344

45+
### config
46+
47+
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`, `wrangler.json`, or `wrangler.toml` you can specify it using this option.
48+
49+
### platformProxy
50+
51+
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.
52+
53+
### fallback
54+
55+
Whether to render a plaintext 404.html page or a rendered SPA fallback page for non-matching asset requests.
56+
57+
For Cloudflare Workers, the default behaviour is to return a null-body 404-status response for non-matching assets requests. However, if the [`assets.not_found_handling`](https://developers.cloudflare.com/workers/static-assets/routing/#2-not_found_handling) Wrangler configuration setting is set to `"404-page"`, this page will be served if a request fails to match an asset. If `assets.not_found_handling` is set to `"single-page-application"`, the adapter will render a SPA fallback index.html page regardless of the `fallback` option specified.
58+
59+
For Cloudflare Pages, this page will only be served when a request that matches an entry in `routes.exclude` fails to match an asset.
60+
61+
Most of the time `plaintext` is sufficient, but if you are using `routes.exclude` to manually
62+
exclude a set of prerendered pages without exceeding the 100 route limit, you may wish to
63+
use `spa` instead to avoid showing an unstyled 404 page to users.
64+
65+
See Cloudflare Pages' [Not Found behaviour](https://developers.cloudflare.com/pages/configuration/serving-pages/#not-found-behavior) for more info.
66+
4467
### routes
4568

46-
Allows you to customise the [`_routes.json`](https://developers.cloudflare.com/pages/functions/routing/#create-a-_routesjson-file) file generated by `adapter-cloudflare`.
69+
Only for Cloudflare Pages. Allows you to customise the [`_routes.json`](https://developers.cloudflare.com/pages/functions/routing/#create-a-_routesjson-file) file generated by `adapter-cloudflare`.
4770

4871
- `include` defines routes that will invoke a function, and defaults to `['/*']`
4972
- `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:
@@ -54,23 +77,52 @@ Allows you to customise the [`_routes.json`](https://developers.cloudflare.com/p
5477

5578
You can have up to 100 `include` and `exclude` rules combined. Generally you can omit the `routes` options, but if (for example) your `<prerendered>` paths exceed that limit, you may find it helpful to manually create an `exclude` list that includes `'/articles/*'` instead of the auto-generated `['/articles/foo', '/articles/bar', '/articles/baz', ...]`.
5679

57-
### platformProxy
80+
## Cloudflare Workers
5881

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.
82+
### Basic configuration
83+
84+
When building for Cloudflare Workers, 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:
85+
86+
```jsonc
87+
/// file: wrangler.jsonc
88+
{
89+
"name": "<any-name-you-want>",
90+
"main": ".svelte-kit/cloudflare/_worker.js",
91+
"compatibility_date": "2025-01-01",
92+
"assets": {
93+
"binding": "ASSETS",
94+
"directory": ".svelte-kit/cloudflare",
95+
}
96+
}
97+
```
98+
99+
### Deployment
100+
101+
Please follow the [framework guide](https://developers.cloudflare.com/workers/frameworks/framework-guides/svelte/) for Cloudflare Workers to begin.
102+
103+
## Cloudflare Pages
60104

61-
## Deployment
105+
### Deployment
62106

63107
Please follow the [Get Started Guide](https://developers.cloudflare.com/pages/get-started/) for Cloudflare Pages to begin.
64108

65-
When configuring your project settings, you must use the following settings:
109+
If you're using the [Git integration](https://developers.cloudflare.com/pages/get-started/git-integration/), your build settings should look like this:
66110

67111
- **Framework preset** – SvelteKit
68112
- **Build command**`npm run build` or `vite build`
69113
- **Build output directory**`.svelte-kit/cloudflare`
70114

115+
### Further reading
116+
117+
You may wish to refer to [Cloudflare's documentation for deploying a SvelteKit site on Cloudflare Pages](https://developers.cloudflare.com/pages/framework-guides/deploy-a-svelte-kit-site/).
118+
119+
### Notes
120+
121+
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/).
122+
71123
## Runtime APIs
72124

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:
125+
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:
74126

75127
```js
76128
// @errors: 7031
@@ -101,26 +153,20 @@ declare global {
101153
export {};
102154
```
103155

104-
### Testing Locally
156+
### Testing locally
105157

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.
158+
Cloudflare 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.
107159

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`.
160+
For testing the build, you should use [Wrangler](https://developers.cloudflare.com/workers/wrangler/) **version 4**. Once you have built your site, run `wrangler dev .svelte-kit/cloudflare` if you're testing for Cloudflare Workers or `wrangler pages dev .svelte-kit/cloudflare` for Cloudflare Pages.
109161

110-
## Notes
111-
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/).
162+
## `_headers` and `_redirects`
113163

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 project root folder.
164+
The [`_headers`](https://developers.cloudflare.com/pages/configuration/headers/) and [`_redirects`](https://developers.cloudflare.com/pages/configuration/redirects/) files specific can be used for static asset responses (like images) by putting them into the project root folder.
115165

116166
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.
117167

118168
## Troubleshooting
119169

120-
### Further reading
121-
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-
124170
### Node.js compatibility
125171

126172
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:
@@ -139,3 +185,45 @@ When deploying your application, the server generated by SvelteKit is bundled in
139185
### Accessing the file system
140186

141187
You can't use `fs` in Cloudflare Workers — you must [prerender](page-options#prerender) the routes in question.
188+
189+
## Migrating from Workers Sites to Workers Static Assets
190+
191+
Cloudflare no longer recommends using [Workers Sites](https://developers.cloudflare.com/workers/configuration/sites/configuration/) and instead recommends using [Workers Static Assets](https://developers.cloudflare.com/workers/static-assets/). To migrate, replace `@sveltejs/adapter-cloudflare-workers` with `@sveltejs/adapter-cloudflare` and remove all `site` configuration settings from your Wrangler configuration file, then add the `assets.directory` and `assets.binding` configuration settings:
192+
193+
### svelte.config.js
194+
195+
```js
196+
/// file: svelte.config.js
197+
---import adapter from '@sveltejs/adapter-cloudflare-workers';---
198+
+++import adapter from '@sveltejs/adapter-cloudflare';+++
199+
200+
export default {
201+
kit: {
202+
adapter: adapter()
203+
}
204+
};
205+
```
206+
207+
### wrangler.toml
208+
209+
```toml
210+
/// file: wrangler.toml
211+
---site.bucket = ".cloudflare/public"---
212+
+++assets.directory = ".cloudflare/public"
213+
assets.binding = "ASSETS"+++
214+
```
215+
216+
### wrangler.jsonc
217+
218+
```jsonc
219+
/// file: wrangler.jsonc
220+
{
221+
--- "site": {
222+
"bucket": ".cloudflare/public"
223+
},---
224+
+++ "assets": {
225+
"directory": ".cloudflare/public",
226+
"binding": "ASSETS"
227+
}+++
228+
}
229+
```

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
title: Cloudflare Workers
33
---
44

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).
5+
> [!NOTE] `adapter-cloudflare-workers` has been deprecated in favour of [`adapter-cloudflare`](adapter-cloudflare). We recommend using `adapter-cloudflare` to deploy to Cloudflare Workers with [Static Assets](https://developers.cloudflare.com/workers/static-assets/) since Cloudflare Workers Sites will be deprecated in favour of it.
66
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.
7+
To deploy to [Cloudflare Workers](https://workers.cloudflare.com/) with [Workers Sites](https://developers.cloudflare.com/workers/configuration/sites/), use `adapter-cloudflare-workers`.
88

99
## Usage
1010

@@ -28,7 +28,7 @@ export default {
2828

2929
### config
3030

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.
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`, `wrangler.json`, or `wrangler.toml` you can specify it using this option.
3232

3333
### platformProxy
3434

@@ -112,7 +112,7 @@ export {};
112112

113113
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.
114114

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`.
115+
For testing the build, you should use [Wrangler](https://developers.cloudflare.com/workers/wrangler/) **version 4**. Once you have built your site, run `wrangler dev`.
116116

117117
## Troubleshooting
118118

eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default [
3030
'@typescript-eslint/no-floating-promises': 'error'
3131
},
3232
ignores: [
33+
'packages/adapter-cloudflare/test/apps/**/*',
3334
'packages/adapter-node/rollup.config.js',
3435
'packages/adapter-node/tests/smoke.spec_disabled.js',
3536
'packages/adapter-static/test/apps/**/*',

0 commit comments

Comments
 (0)