Skip to content

feat: use Workers Static Assets #13427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
30d1f46
feat: use the new Workers Static Assets feature from Cloudflare
petebacondarwin Nov 28, 2024
2ae3029
Merge branch 'main' into pr/petebacondarwin/13072
eltigerchino Dec 31, 2024
a294804
Merge branch 'main' into pr/petebacondarwin/13072
eltigerchino Jan 21, 2025
b667ac7
upgrade workers types
eltigerchino Jan 21, 2025
8a0cb79
docs: apply review feedback
astralhpi Feb 6, 2025
65bc106
refactor: fix naming to match convention
astralhpi Feb 6, 2025
a824faf
docs: simplify CHANGELOG for Workers Assets migration
astralhpi Feb 6, 2025
094cc0f
docs: add migration guide for Workers Static Assets
astralhpi Feb 6, 2025
e81980e
Merge branch 'main' into update-cloudflare-worker-assets-handling
astralhpi Feb 6, 2025
7943188
fix: lock file
astralhpi Feb 6, 2025
59495bb
docs: fix mistake
astralhpi Feb 7, 2025
b278361
docs: update Wrangler version requirement to v3.87.0+
astralhpi Feb 7, 2025
60d0c2b
Merge branch 'main' into update-cloudflare-worker-assets-handling
astralhpi Mar 9, 2025
f5f7445
Merge branch 'main' into pr/astralhpi/13427
eltigerchino Mar 19, 2025
6838afa
cleanup and parity with cloudflare pages adapter
eltigerchino Mar 20, 2025
13128a9
move comparisons to cloudflare workers page
eltigerchino Mar 20, 2025
4d952a3
changesets
eltigerchino Mar 20, 2025
cb73dd8
fix lint
eltigerchino Mar 20, 2025
ccb0242
serve immutable assets from worker with cache control
eltigerchino Mar 20, 2025
071f62f
changesets
eltigerchino Mar 20, 2025
5bd2e39
add static headers and redirects
eltigerchino Mar 20, 2025
c13390a
Merge branch 'main' into pr/astralhpi/13427
eltigerchino Mar 20, 2025
578638c
Merge branch 'main' into pr/astralhpi/13427
eltigerchino Mar 20, 2025
94b7b73
add support for _headers and _redirects
eltigerchino Mar 20, 2025
4dde235
Merge branch 'main' into pr/astralhpi/13427
eltigerchino Mar 21, 2025
909c2e7
specify value of assets.binding config
eltigerchino Mar 21, 2025
2b08bd3
add migration example
eltigerchino Mar 21, 2025
906c110
error if site key is in wrangler config
eltigerchino Mar 21, 2025
26fbbbf
revert removing x-robots-tag: noindex from headers
eltigerchino Mar 24, 2025
68a214a
copy to _worker.js file instead of dir
eltigerchino Mar 24, 2025
fefc761
Merge branch 'main' into pr/astralhpi/13427
eltigerchino Mar 24, 2025
e7f3bf8
format
eltigerchino Mar 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/green-students-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare-workers': major
---

feat: remove esbuild step
5 changes: 5 additions & 0 deletions .changeset/new-kings-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare': patch
---

fix: change `esbuild` to a dev dependency
5 changes: 5 additions & 0 deletions .changeset/ninety-fans-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare': patch
---

fix: serve files from `_app/*` from the worker if the `_app/*` route was not excluded
5 changes: 5 additions & 0 deletions .changeset/smart-owls-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare-workers': major
---

feat: use Workers Static Assets instead of Workers Sites
5 changes: 5 additions & 0 deletions .changeset/weak-chairs-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare-workers': minor
---

feat: optimize cache usage with `worktop`
10 changes: 3 additions & 7 deletions documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md
Original file line number Diff line number Diff line change
@@ -6,11 +6,7 @@ To deploy to [Cloudflare Pages](https://pages.cloudflare.com/), use [`adapter-cl

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.

## Comparisons

- `adapter-cloudflare` – supports all SvelteKit features; builds for [Cloudflare Pages](https://blog.cloudflare.com/cloudflare-pages-goes-full-stack/)
- `adapter-cloudflare-workers` – supports all SvelteKit features; builds for Cloudflare Workers
- `adapter-static` – only produces client-side static assets; compatible with Cloudflare Pages
> [!NOTE] Unless you have a specific reason to use `adapter-cloudflare`, it's recommended that you use [`adapter-cloudflare-workers`](adapter-cloudflare-workers) instead since Cloudflare plans to deprecate Cloudflare Pages in favour of Cloudflare Workers. Refer to the [compatibility matrix](https://developers.cloudflare.com/workers/static-assets/compatibility-matrix/) for more information.

## Usage

@@ -62,7 +58,7 @@ Preferences for the emulated `platform.env` local bindings. See the [getPlatform

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

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

- **Framework preset** – SvelteKit
- **Build command** – `npm run build` or `vite build`
@@ -101,7 +97,7 @@ declare global {
export {};
```

### Testing Locally
### Testing locally

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.

Original file line number Diff line number Diff line change
@@ -4,7 +4,11 @@ title: Cloudflare Workers

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

> [!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.
## Comparisons

- `adapter-cloudflare-workers` – supports all SvelteKit features; builds for Cloudflare Workers
- `adapter-cloudflare` – supports all SvelteKit features; builds for Cloudflare Pages
- `adapter-static` – only produces client-side static assets; compatible with Cloudflare Pages and Cloudflare Workers

## Usage

@@ -34,46 +38,26 @@ Path to your [Wrangler configuration file](https://developers.cloudflare.com/wor

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.

## Basic Configuration
## Basic configuration

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:

```jsonc
/// file: wrangler.jsonc
{
"name": "<your-service-name>",
"account_id": "<your-account-id>",
"main": "./.cloudflare/worker.js",
"site": {
"bucket": "./.cloudflare/public"
},
"build": {
"command": "npm run build"
},
"compatibility_date": "2021-11-12"
"main": ".svelte-kit/cloudflare/_worker.js",
"compatibility_date": "2025-01-01",
"assets": {
"binding": "ASSETS",
"directory": ".svelte-kit/cloudflare",
}
}
```

`<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:
## Deployment

```
https://dash.cloudflare.com/<your-account-id>/home
```

> [!NOTE] You should add the `.cloudflare` directory (or whichever directories you specified for `main` and `site.bucket`) and the `.wrangler` directory to your `.gitignore`.

You will need to install [Wrangler](https://developers.cloudflare.com/workers/wrangler/install-and-update/) and log in, if you haven't already:

```sh
npm i -D wrangler
wrangler login
```

Then, you can build your app and deploy it:

```sh
wrangler deploy
```
Please follow the [framework guide](https://developers.cloudflare.com/workers/frameworks/framework-guides/svelte/) for Cloudflare Workers to begin.

## Runtime APIs

@@ -108,12 +92,16 @@ declare global {
export {};
```

### Testing Locally
### Testing locally

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.

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`.

## Notes

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.

## Troubleshooting

### Node.js compatibility
@@ -134,3 +122,31 @@ When deploying your application, the server generated by SvelteKit is bundled in
### Accessing the file system

You can't use `fs` in Cloudflare Workers — you must [prerender](page-options#prerender) the routes in question.

## Migrating from Workers Sites to Workers Static Assets

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, remove all `site` configuration settings from your Wrangler configuration file and add the `assets.directory` and `assets.binding` configuration settings:

### wrangler.toml

```toml
/// file: wrangler.toml
---site.bucket = ".cloudflare/public"---
+++assets.directory = ".cloudflare/public"
assets.binding = "ASSETS"+++
```

### wrangler.jsonc

```jsonc
/// file: wrangler.jsonc
{
--- "site": {
"bucket": ".cloudflare/public"
},---
+++ "assets": {
"directory": ".cloudflare/public",
"binding": "ASSETS"
}+++
}
```
3 changes: 1 addition & 2 deletions packages/adapter-cloudflare-workers/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
.DS_Store
node_modules
/files
4 changes: 2 additions & 2 deletions packages/adapter-cloudflare-workers/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# adapter-cloudflare-workers

SvelteKit adapter that creates a Cloudflare Workers site using a function for dynamic server rendering.
[Adapter](https://svelte.dev/docs/kit/building-your-app) for building SvelteKit applications on [Cloudflare Workers](https://developers.cloudflare.com/workers/) with [static assets](https://developers.cloudflare.com/workers/static-assets/).

**Requires [Wrangler v2](https://developers.cloudflare.com/workers/wrangler/get-started/).** Wrangler v1 is no longer supported.
**Requires [Wrangler v3.91.0 or later](https://developers.cloudflare.com/workers/wrangler/get-started/).**.

## Docs

5 changes: 3 additions & 2 deletions packages/adapter-cloudflare-workers/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { CacheStorage, IncomingRequestCfProperties } from '@cloudflare/workers-types';
import { CacheStorage, CfProperties } from '@cloudflare/workers-types';

declare global {
namespace App {
export interface Platform {
env: unknown;
context: {
waitUntil(promise: Promise<any>): void;
};
caches: CacheStorage;
cf?: IncomingRequestCfProperties;
cf?: CfProperties;
}
}
}
9 changes: 0 additions & 9 deletions packages/adapter-cloudflare-workers/files/_package.json

This file was deleted.

136 changes: 0 additions & 136 deletions packages/adapter-cloudflare-workers/files/entry.js

This file was deleted.

Loading