Skip to content

Conversation

@ascorbic
Copy link
Contributor

Description (required)

This PR updates the Cloudflare integration guide to use the new Astro 6 APIs. It also adds a migration guide for users moving from Astro 5 to 6. It includes some info about manually configuring Cloudflare Pages (which is no longer automatic), but it doesn't yet have a very good guide to migrate from Pages to Workers. It links out to the guide in the Cloudflare docs, but I'd like to add something more Astro-specific in a follow-up PR.

Related issues & labels (optional)

  • Closes #
  • Suggested label:

@netlify
Copy link

netlify bot commented Dec 23, 2025

Deploy Preview for astro-docs-2 ready!

Name Link
🔨 Latest commit 2f87dd2
🔍 Latest deploy log https://app.netlify.com/projects/astro-docs-2/deploys/694ad3cf4f4da700089a2355
😎 Deploy Preview https://deploy-preview-12981--astro-docs-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@astrobot-houston
Copy link
Contributor

Lunaria Status Overview

🌕 This pull request will trigger status changes.

Learn more

By default, every PR changing files present in the Lunaria configuration's files property will be considered and trigger status changes accordingly.

You can change this by adding one of the keywords present in the ignoreKeywords property in your Lunaria configuration file in the PR's title (ignoring all files) or by including a tracker directive in the merged commit's description.

Tracked Files

File Note
en/guides/integrations-guide/cloudflare.mdx Source changed, localizations will be marked as outdated.
en/guides/upgrade-to/v6.mdx Localization changed, will be marked as complete.
Warnings reference
Icon Description
🔄️ The source for this localization has been updated since the creation of this pull request, make sure all changes in the source have been applied.

@ascorbic ascorbic closed this Dec 23, 2025
@ascorbic ascorbic deleted the v6-astro branch December 23, 2025 17:44
@ascorbic ascorbic restored the v6-astro branch December 23, 2025 17:44
@ascorbic ascorbic reopened this Dec 23, 2025
@ascorbic ascorbic closed this Dec 23, 2025
@ascorbic ascorbic deleted the v6-astro branch December 23, 2025 17:46
@ascorbic ascorbic restored the v6-astro branch December 23, 2025 17:47
@ascorbic ascorbic reopened this Dec 23, 2025
Copy link
Member

@ArmandPhilippot ArmandPhilippot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Matt, this looks great! And well done with the upgrade guide! 🙌🏽

I left a few suggestions, most of them are nits and I expect you to weight on them as Cloudflare expert and native speaker to judge if this makes sense or not. 😅
The most important thing is your formatter which is messing with the API blocks... It would be better to revert these changes.

Otherwise, this looks to be in great shape to me!

Comment on lines +93 to +103
3. Optionally, create a [Wrangler configuration file](https://developers.cloudflare.com/workers/wrangler/configuration/) if you need custom settings, or to declare Cloudflare bindings (KV, D1, Durable Objects, etc.):

```jsonc title="wrangler.jsonc"
{
"main": "dist/_worker.js/index.js",
"name": "my-astro-app",
// Update to today's date
"compatibility_date": "2025-03-25",
"compatibility_flags": [
"nodejs_compat",
"global_fetch_strictly_public"
],
"assets": {
"binding": "ASSETS",
"directory": "./dist"
},
"observability": {
"enabled": true
}
}
```

4. Create a `.assetsignore` file in your `public/` folder, and add the following lines to it:
```jsonc title="wrangler.jsonc"
{
"name": "my-astro-app",
// Add your bindings here, e.g.:
// "kv_namespaces": [{ "binding": "MY_KV", "id": "<namespace_id>" }]
}
```

```txt title="public/.assetsignore"
_worker.js
_routes.json
```
For simple projects without bindings, you can skip this step as Astro will generate a default configuration automatically. The Worker name will be based on the `package.json` name field or the folder name.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I can't make a suggestion directly because GitHub is not happy with deleted lines. This step looks fine to me, but I wonder if it wouldn't be more helpful to talk about the default value first to emphasize that this is optional and help users determine whether they need it or not before reading further.
What I was thinking is something like:

  1. Astro will automatically generate a default configuration by reusing the package.json name field or the folder name as Worker name. You can optionally create a Wrangler configuration file to define custom settings. The following example declares Cloudflare KV bindings:

Then, we don't need the paragraph below the code snippet. And, we could uncomment the code in the code snippet.

<p>
**Type:** `boolean`<br />
**Default:** `true`
**Type:** `boolean`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is auto formatting (here and in other places), but it makes tracking changes more difficult. This will also affect translators when checking what actually changed here. And, more important, this breaks the formatting:

Before After
astro-docs-formatting-before astro-docs-formatting

</p>

The `sessionKVBindingName` option allows you to specify the name of the KV binding used for session storage. By default, this is set to `SESSION`, but you can change it to match your own KV binding name. See [Sessions](#sessions) for more information.
Sets the name of the KV binding used for session storage. The KV namespace is automatically provisioned when you deploy. By default, the binding is named `SESSION`, but you can change it if you define the binding manually in your wrangler config. See [Sessions](#sessions) for more information.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the rewording at the beginning of the sentence!

But, maybe it's just me, while reading I feel that "The KV namespace is automatically provisioned when you deploy." breaks a bit the flow. I'm not familiar enough with Cloudflare (+ probably the language barrier) to suggest something accurate I think. Does the following make sense, or was putting it outside of "By default" intentional?

Suggested change
Sets the name of the KV binding used for session storage. The KV namespace is automatically provisioned when you deploy. By default, the binding is named `SESSION`, but you can change it if you define the binding manually in your wrangler config. See [Sessions](#sessions) for more information.
Sets the name of the KV binding used for session storage. By default, the KV namespace is automatically provisioned when you deploy and the binding is named `SESSION`. You can change this name by setting the binding manually in your wrangler config. See [Sessions](#sessions) for more information.

The path to the entry file. This should be a relative path from the root of your Astro project.

By default, the adapter uses a generic entry file, which only supports the `fetch` handler.
Sets the name of the Images binding used when [`imageService`](#imageservice) is set to `cloudflare-binding`. The binding is automatically provisioned when you deploy. By default, the binding is named `IMAGES`, but you can change it if you define the binding manually in your wrangler config.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If my previous comment makes sense, I have the same feeling here with "The binding is automatically provisioned when you deploy.".

Suggested change
Sets the name of the Images binding used when [`imageService`](#imageservice) is set to `cloudflare-binding`. The binding is automatically provisioned when you deploy. By default, the binding is named `IMAGES`, but you can change it if you define the binding manually in your wrangler config.
Sets the name of the Images binding used when [`imageService`](#imageservice) is set to `cloudflare-binding`. By default, the binding is automatically provisioned when you deploy and the binding is named `IMAGES`. You can change it by setting the binding manually in your wrangler config:

But then, I wonder if reversing the two code snippets wouldn't be more logical.

Comment on lines +214 to 233
Environment variables and bindings defined in your `wrangler.jsonc` configuration file are accessed by importing `env` from `cloudflare:workers`:

```astro title="src/pages/index.astro"
---
const { env } = Astro.locals.runtime;
---
```
You can access the runtime from API endpoints through `context.locals`:
import { env } from 'cloudflare:workers';
```js title="src/pages/api/someFile.js"
export function GET(context) {
const runtime = context.locals.runtime;

return new Response('Some body');
}
const myVariable = env.MY_VARIABLE;
const myKVNamespace = env.MY_KV;
---
```

See the [list of all supported bindings](https://developers.cloudflare.com/workers/wrangler/api/#supported-bindings) in the Cloudflare documentation.


### Environment variables and secrets

The Cloudflare runtime treats environment variables as a type of binding.

For example, you can define an [environment variable](https://developers.cloudflare.com/workers/configuration/environment-variables/#add-environment-variables-via-wrangler) in `wrangler.jsonc` as follows:
Define [environment variables](https://developers.cloudflare.com/workers/configuration/environment-variables/#add-environment-variables-via-wrangler) in `wrangler.jsonc`:

```jsonc title="wrangler.jsonc"
{
"vars" : {
"MY_VARIABLE": "test"
}
"vars": {
"MY_VARIABLE": "test",
},
}
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I can't make a proper suggestion here, but regarding the flow, I wonder if it wouldn't be more logical to show first how to define them, then how to access them, rather than the other way around:

[Environment variables](https://developers.cloudflare.com/workers/configuration/environment-variables/#add-environment-variables-via-wrangler) and bindings are defined in your `wrangler.jsonc` configuration file:
/* code snippet */
You can then access them by importing `env` from `cloudflare:workers`:
/* code snippet */

```
```astro title="src/components/CartButton.astro"
---
export const prerender = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this is not new, but we now tend to add a comment alongside this:

Suggested change
export const prerender = false;
export const prerender = false; // Not needed in 'server' mode

## Node.js compatibility

Out of the box, Cloudflare does not support the Node.js runtime APIs. With some configuration, Cloudflare does support a subset of the Node.js runtime APIs. You can find supported Node.js runtime APIs in Cloudflare's [documentation](https://developers.cloudflare.com/workers/runtime-apis/nodejs).
Cloudflare Workers supports a growing subset of Node.js runtime APIs through the `nodejs_compat` compatibility flag. This includes commonly used modules like `node:buffer`, `node:crypto`, `node:path`, and many others. See the [full list of supported Node.js APIs](https://developers.cloudflare.com/workers/runtime-apis/nodejs) in Cloudflare's documentation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I feel like "growing" might be a bit too marketing speech and might not have its place here? Or, with a link to a roadmap or something, I guess it could make sense:

Suggested change
Cloudflare Workers supports a growing subset of Node.js runtime APIs through the `nodejs_compat` compatibility flag. This includes commonly used modules like `node:buffer`, `node:crypto`, `node:path`, and many others. See the [full list of supported Node.js APIs](https://developers.cloudflare.com/workers/runtime-apis/nodejs) in Cloudflare's documentation.
Cloudflare Workers supports a subset of Node.js runtime APIs through the `nodejs_compat` compatibility flag. This includes commonly used modules like `node:buffer`, `node:crypto`, `node:path`, and many others. See the [full list of supported Node.js APIs](https://developers.cloudflare.com/workers/runtime-apis/nodejs) in Cloudflare's documentation.

Then use the `node:*` import syntax in your server-side code:

```js title="src/pages/api/endpoint.js"
export const prerender = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as before, since we're updating anyway, I think we can add this while we're at it:

Suggested change
export const prerender = false;
export const prerender = false; // Not needed in 'server' mode


### Meaningful error messages

Currently, errors during running your application in Wrangler are not very useful, due to the minification of your code. For better debugging, add `vite.build.minify = false` to your `astro.config.mjs`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is honest, maybe we could only say:

Suggested change
Currently, errors during running your application in Wrangler are not very useful, due to the minification of your code. For better debugging, add `vite.build.minify = false` to your `astro.config.mjs`:
By default, errors occurring while running your application in Wrangler are minified. For better debugging, add `vite.build.minify = false` to your `astro.config.mjs`:

export default defineConfig({
adapter: cloudflare(),
build: {
client: './dist',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
client: './dist',
client: './',

This value is relative to the outDir setting, so it should not include dist

adapter: cloudflare(),
build: {
client: './dist',
server: './dist/_worker.js',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
server: './dist/_worker.js',
server: './_worker.js',

This value is relative to the outDir setting, so it should not include dist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants