Skip to content

feat(vite): auto-detect SvelteKit Cloudflare adapter#827

Merged
theoephraim merged 1 commit into
mainfrom
sveltekit-cf-autodetect
Jun 23, 2026
Merged

feat(vite): auto-detect SvelteKit Cloudflare adapter#827
theoephraim merged 1 commit into
mainfrom
sveltekit-cf-autodetect

Conversation

@theoephraim

@theoephraim theoephraim commented Jun 23, 2026

Copy link
Copy Markdown
Member

What

SvelteKit on Cloudflare previously required a different import (varlockSvelteKitCloudflarePlugin from @varlock/cloudflare-integration/sveltekit) than the Node/other-adapter path (varlockVitePlugin). This makes the same varlockVitePlugin() work for every deploy target.

varlockVitePlugin() now detects @sveltejs/adapter-cloudflare and automatically injects the Cloudflare Workers runtime env-loader into the SSR entry. Detection reads the SvelteKit setup plugin's resolved api.options.kit.adapter in configResolved, so it works whether the adapter is configured in svelte.config.js or inline in vite.config (SvelteKit ≥ 2.62). adapter-auto is also handled via a CF_PAGES/WORKERS_CI fallback.

The Cloudflare-specific injected code stays in @varlock/cloudflare-integration and is pulled in lazily via a runtime dynamic import. CF deployers already have that package (it ships varlock-wrangler). It is intentionally not declared as a peer dependency of @varlock/vite-integration — cloudflare-integration depends on vite-integration, so the back-edge would create a Turbo build/typecheck cycle; the variable-specifier dynamic import means typecheck never needs it resolvable.

Changes

  • @varlock/vite-integration: adapter detection + lazy loader injection (ssrEdgeRuntime + ssrEntryCode), and externalize cloudflare:workers when the CF Vite plugin isn't present. Logs a one-time notice when the Cloudflare adapter is detected so the auto-injection isn't silent.
  • varlockSvelteKitCloudflarePlugin → deprecated thin alias for back-compat.
  • Docs: single setup path on the SvelteKit page; Cloudflare page now clearly redirects SvelteKit users to the SvelteKit guide.
  • Framework tests: new frameworks/sveltekit/ fixture covering build (adapter-node), dev, Cloudflare auto-detection (incl. asserting the detection notice), and a regression guard that a fully-prerendered ("totally static") Cloudflare build still works (the injected loader stays inert in Node). All passing locally (bun run test:sveltekit).

Notes for reviewers

  • This avoids the ESM-only trap from the earlier abandoned attempt (folding into varlockCloudflareVitePlugin, which is coupled to the ESM-only @cloudflare/vite-plugin). Detection lives in the vite plugin and only imports the dependency-free ./ssr-entry-code.
  • Static setups are safe: adapter-static never triggers detection (different adapter name); adapter-cloudflare + full prerender injects the loader but it's inert at build/prerender time (guarded by navigator.userAgent === 'Cloudflare-Workers', and cloudflare:workers is externalized + dynamically imported).
  • Merge overlap: codex/static-dynamic-integration also introduces a framework-tests/frameworks/sveltekit/ fixture (adapter-static, static/dynamic feature). When both land, the two fixtures should be reconciled into one with all scenarios.
  • The varlock-examples SvelteKit example's vite.config.ts has a matching simplification queued separately (different repo).

@github-actions

github-actions Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

bumpy-frog

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

minor Minor releases

  • @varlock/astro-integration 1.0.4 → 1.1.0 (cascade)
  • @varlock/cloudflare-integration 1.1.7 → 1.2.0 (cascade)
  • @varlock/vite-integration 1.1.3 → 1.2.0

Bump files in this PR

Click here if you want to add another bump file to this PR


This comment is maintained by bumpy.

@pkg-pr-new

pkg-pr-new Bot commented Jun 23, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/varlock@827
npm i https://pkg.pr.new/@varlock/cloudflare-integration@827
npm i https://pkg.pr.new/@varlock/vite-integration@827

commit: f9dcd3f

@theoephraim theoephraim force-pushed the sveltekit-cf-autodetect branch from f557e66 to b6b090f Compare June 23, 2026 22:35
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 23, 2026

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
varlock-website f9dcd3f Commit Preview URL

Branch Preview URL
Jun 23 2026, 11:01 PM

@theoephraim theoephraim force-pushed the sveltekit-cf-autodetect branch 2 times, most recently from c4a8f04 to 8a65471 Compare June 23, 2026 22:56
The standard varlockVitePlugin() now detects @sveltejs/adapter-cloudflare
(configured in svelte.config.js or inline in vite.config) and injects the
Cloudflare Workers runtime env-loader automatically, so the same import works
across every deploy target. The Cloudflare-specific loader stays in
@varlock/cloudflare-integration (an optional peer dep) and is pulled in lazily.

- deprecate varlockSvelteKitCloudflarePlugin to a thin alias
- single setup path in the SvelteKit/Cloudflare docs
- add SvelteKit framework tests (build, dev, cloudflare auto-detection)
@theoephraim theoephraim force-pushed the sveltekit-cf-autodetect branch from 8a65471 to f9dcd3f Compare June 23, 2026 22:59
@theoephraim theoephraim merged commit 57677a5 into main Jun 23, 2026
31 checks passed
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.

1 participant