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/10-getting-started/30-project-structure.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -55,7 +55,7 @@ The `src` directory contains the meat of your project. Everything except `src/ro
55
55
-`hooks.server.js` contains your server [hooks](hooks)
56
56
-`service-worker.js` contains your [service worker](service-workers)
57
57
58
-
(Whether the project contains `.js` or `.ts` files depends on whether you opt to use TypeScript when you create your project. You can switch between JavaScript and TypeScript in the documentation using the toggle at the bottom of this page.)
58
+
(Whether the project contains `.js` or `.ts` files depends on whether you opt to use TypeScript when you create your project.)
59
59
60
60
If you added [Vitest](https://vitest.dev) when you set up your project, your unit tests will live in the `src` directory with a `.test.js` extension.
Copy file name to clipboardExpand all lines: documentation/docs/20-core-concepts/10-routing.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -277,7 +277,7 @@ Like `+layout.js`, `+layout.server.js` can export [page options](page-options)
277
277
278
278
## +server
279
279
280
-
As well as pages, you can define routes with a `+server.js` file (sometimes referred to as an 'API route' or an 'endpoint'), which gives you full control over the response. Your `+server.js` file exports functions corresponding to HTTP verbs like `GET`, `POST`, `PATCH`, `PUT`, `DELETE`, `OPTIONS`, and `HEAD` that take a `RequestEvent` argument and return a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object.
280
+
As well as pages, you can define routes with a `+server.js` file (sometimes referred to as an 'API route' or an 'endpoint'), which gives you full control over the response. Your `+server.js` file exports functions corresponding to HTTP verbs like `GET`, `POST`, `PATCH`, `PUT`, `DELETE`, `OPTIONS`, and `HEAD` that take a [`RequestEvent`](@sveltejs-kit#RequestEvent) argument and return a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object.
281
281
282
282
For example we could create an `/api/random-number` route with a `GET` handler:
Copy file name to clipboardExpand all lines: documentation/docs/25-build-and-deploy/40-adapter-node.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,7 @@ Development dependencies will be bundled into your app using [Rollup](https://ro
34
34
35
35
### Compressing responses
36
36
37
-
You will typically want to compress responses coming from the server. If you are already deploying your server behind a reverse proxy for SSL or load balancing, it typically results in better performance to also handle compression at that layer since Node.js is single-threaded.
37
+
You will typically want to compress responses coming from the server. If you're already deploying your server behind a reverse proxy for SSL or load balancing, it typically results in better performance to also handle compression at that layer since Node.js is single-threaded.
38
38
39
39
However, if you're building a [custom server](#Custom-server) and do want to add a compression middleware there, note that we would recommend using [`@polka/compression`](https://www.npmjs.com/package/@polka/compression) since SvelteKit streams responses and the more popular `compression` package does not support streaming and may cause errors when used.
40
40
@@ -101,7 +101,7 @@ If `adapter-node` can't correctly determine the URL of your deployment, you may
101
101
102
102
### `ADDRESS_HEADER` and `XFF_DEPTH`
103
103
104
-
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:
104
+
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:
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).
6
6
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.
8
8
9
9
## Comparisons
10
10
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
14
14
15
15
## Usage
16
16
17
17
Install with `npm i -D @sveltejs/adapter-cloudflare`, then add the adapter to your `svelte.config.js`:
18
18
19
19
```js
20
-
// @errors: 2307
21
20
/// file: svelte.config.js
22
21
importadapterfrom'@sveltejs/adapter-cloudflare';
23
22
24
23
exportdefault {
25
24
kit: {
26
25
adapter:adapter({
27
26
// See below for an explanation of these options
28
-
routes: {
29
-
include: ['/*'],
30
-
exclude: ['<all>']
31
-
},
27
+
config:undefined,
32
28
platformProxy: {
33
29
configPath:undefined,
34
30
environment:undefined,
35
31
persist:undefined
32
+
},
33
+
fallback:'plaintext',
34
+
routes: {
35
+
include: ['/*'],
36
+
exclude: ['<all>']
36
37
}
37
38
})
38
39
}
@@ -41,9 +42,31 @@ export default {
41
42
42
43
## Options
43
44
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
+
44
67
### routes
45
68
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`.
47
70
48
71
-`include` defines routes that will invoke a function, and defaults to `['/*']`
49
72
-`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,25 +77,55 @@ Allows you to customise the [`_routes.json`](https://developers.cloudflare.com/p
54
77
55
78
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', ...]`.
56
79
57
-
### platformProxy
80
+
##Cloudflare Workers
58
81
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
60
83
61
-
## Deployment
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
104
+
105
+
### Deployment
62
106
63
107
Please follow the [Get Started Guide](https://developers.cloudflare.com/pages/get-started/) for Cloudflare Pages to begin.
64
108
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:
66
110
67
111
-**Framework preset** – SvelteKit
68
112
-**Build command** – `npm run build` or `vite build`
Once configured, go to the **Runtime** section of your project settings, and add the `nodejs_als` compability flag to enable the [Node.js AsyncLocalStorage](https://developers.cloudflare.com/workers/configuration/compatibility-flags/#nodejs-asynclocalstorage).
72
117
118
+
### Further reading
119
+
120
+
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/).
121
+
122
+
### Notes
123
+
124
+
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/).
125
+
73
126
## Runtime APIs
74
127
75
-
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:
128
+
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:
76
129
77
130
```js
78
131
// @errors: 7031
@@ -103,26 +156,20 @@ declare global {
103
156
export {};
104
157
```
105
158
106
-
### Testing Locally
107
-
108
-
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.
159
+
### Testing locally
109
160
110
-
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 --compatibility-flag=nodejs_als`.
161
+
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.
111
162
112
-
## Notes
163
+
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 --compatibility-flag=nodejs_als` if you're testing for Cloudflare Workers or `wrangler pages dev .svelte-kit/cloudflare` for Cloudflare Pages.
113
164
114
-
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/).
165
+
## Headers and redirects
115
166
116
-
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.
167
+
The [`_headers`](https://developers.cloudflare.com/pages/configuration/headers/) and [`_redirects`](https://developers.cloudflare.com/pages/configuration/redirects/) files, specific to Cloudflare, can be used for static asset responses (like images) by putting them into the project root folder.
117
168
118
169
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
170
120
171
## Troubleshooting
121
172
122
-
### Further reading
123
-
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/).
125
-
126
173
### Node.js compatibility
127
174
128
175
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:
@@ -141,3 +188,45 @@ When deploying your application, the server generated by SvelteKit is bundled in
141
188
### Accessing the file system
142
189
143
190
You can't use `fs` in Cloudflare Workers — you must [prerender](page-options#prerender) the routes in question.
191
+
192
+
## Migrating from Workers Sites
193
+
194
+
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:
0 commit comments