Skip to content

feat: support Cloudflare Workers Builds #13733

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

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

eltigerchino
Copy link
Member

@eltigerchino eltigerchino commented Apr 23, 2025

Inspired by unjs/std-env#156

This PR detects the WORKERS_CI environment variable so that adapter-auto and adapter-cloudflare can work with Cloudflare's Workers Git integration to build the app without requiring a wrangler configuration from the user.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

Copy link

changeset-bot bot commented Apr 23, 2025

🦋 Changeset detected

Latest commit: 710510b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@sveltejs/adapter-cloudflare Minor
@sveltejs/adapter-auto Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@eltigerchino
Copy link
Member Author

eltigerchino commented Apr 24, 2025

@benmccann I'm not sure if the adapter-auto case for Workers Builds is very useful with this PR because it doesn't "just work" like the other platforms do (since it fails without a Wrangler config file). Would it be useful to generate a basic Wrangler config during the Workers CI so that it just works? Or is it better that users get a deployment error which forces them to setup a wrangler.jsonc file?

@svelte-docs-bot
Copy link

@eltigerchino eltigerchino marked this pull request as ready for review April 24, 2025 04:23
@benmccann
Copy link
Member

Ah, I guess the PR description is outdated then as it suggests it works without a config

What would a basic config contain? Is there anything we might accidentally omit or get wrong by doing that?

@eltigerchino
Copy link
Member Author

eltigerchino commented Apr 24, 2025

Ah, I guess the PR description is outdated then as it suggests it works without a config

Yeah, I was half-way implementing the generated config but then wasn't sure if it would be a good idea.

What would a basic config contain? Is there anything we might accidentally omit or get wrong by doing that?

The minimum we would need would be:

{
	// `name` is not required since Cloudflare's dashboard requires the user to enter that before the build
	"main": ".svelte-kit/cloudflare/_worker.js",
	// this will be based on the current date
	"compatibility_date": "2025-04-24",
	"assets": {
		"binding": "ASSETS",
		"directory": ".svelte-kit/cloudflare",
	},
	// only required if the user uses `getRequestEvent()` asynchronously.
	// Enabling this doesn't add any polyfills like `nodejs_compat` does so it could be safe to always have this?
	// Otherwise we could only include it if we detect `getRequestEvent()` being used.
	"compatibility_flags": ["nodejs_als"]
}

Another question is: how should we generate the Wrangler config? There are currently two ways to do this:

  1. Simulate a user's Wrangler configuration file.
    This is probably the easiest and means writing a new wrangler.jsonc if there is no user configuration file already.

  2. Create a redirected Wrangler configuration.
    I'm a bit wary of using this method because if the redirected config exists, the Wrangler deploy and dev commands will always ignore the user's configuration file until the redirect is deleted. Fortunately, Wrangler does warn the user if the redirect config is being used. However, it would be better if we could create a config that acts as a fallback/override rather than a complete replacement. I've asked the Cloudflare team about this option and they recommended validating the user config instead.

@benmccann benmccann marked this pull request as draft May 7, 2025 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants