Skip to content

fix: deprecate cloudflare platform.context in favor for platform.ctx #13856

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

Merged
merged 5 commits into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/moody-baboons-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare': patch
---

fix: deprecate `platform.context` in favor of `platform.ctx` to align with Cloudflare's naming convention
4 changes: 3 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
"**/.custom-out-dir/**",
"**/build/**",
"**/test-results/**",
"**/.wrangler/**",
"documentation/**/*.md",
"packages/package/test/fixtures/**/expected/**/*",
"packages/package/test/watch/expected/**/*",
"packages/package/test/watch/package/**/*",
"packages/kit/src/core/postbuild/fixtures/**/*"
"packages/kit/src/core/postbuild/fixtures/**/*",
"packages/adapter-cloudflare/test/apps/workers/dist/**/*"
],
"options": {
"rangeEnd": 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Functions contained in the [`/functions` directory](https://developers.cloudflar

## Runtime APIs

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:
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 [`ctx`](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:

```js
// @errors: 7031
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ wrangler deploy

## Runtime APIs

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:
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 [`ctx`](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:

```js
// @errors: 7031
Expand Down
2 changes: 2 additions & 0 deletions packages/adapter-cloudflare/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ declare global {
namespace App {
export interface Platform {
env: unknown;
ctx: ExecutionContext;
/** @deprecated Use `ctx` instead */
context: ExecutionContext;
caches: CacheStorage;
cf?: IncomingRequestCfProperties;
Expand Down
3 changes: 2 additions & 1 deletion packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ export default function (options = {}) {
const proxy = await getPlatformProxy(options.platformProxy);
const platform = /** @type {App.Platform} */ ({
env: proxy.env,
context: proxy.ctx,
ctx: proxy.ctx,
context: proxy.ctx, // deprecated in favor of ctx
caches: proxy.caches,
cf: proxy.cf
});
Expand Down
9 changes: 5 additions & 4 deletions packages/adapter-cloudflare/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export default {
/**
* @param {Request} req
* @param {{ ASSETS: { fetch: typeof fetch } }} env
* @param {ExecutionContext} context
* @param {ExecutionContext} ctx
* @returns {Promise<Response>}
*/
async fetch(req, env, context) {
async fetch(req, env, ctx) {
await server.init({
// @ts-expect-error env contains environment variables and bindings
env
Expand Down Expand Up @@ -70,7 +70,8 @@ export default {
res = await server.respond(req, {
platform: {
env,
context,
ctx,
context: ctx, // deprecated in favor of ctx
// @ts-expect-error webworker types from worktop are not compatible with Cloudflare Workers types
caches,
// @ts-expect-error the type is correct but ts is confused because platform.cf uses the type from index.ts while req.cf uses the type from index.d.ts
Expand All @@ -85,6 +86,6 @@ export default {
// write to `Cache` only if response is not an error,
// let `Cache.save` handle the Cache-Control and Vary headers
pragma = res.headers.get('cache-control') || '';
return pragma && res.status < 400 ? Cache.save(req, res, context) : res;
return pragma && res.status < 400 ? Cache.save(req, res, ctx) : res;
}
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// this tests that Wrangler can correctly resolve and bundle server-side dependencies
import { sum } from 'server-side-dep';

export function load() {
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-cloudflare/test/apps/pages/test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from '@playwright/test';

test('worker works', async ({ page }) => {
test('worker', async ({ page }) => {
await page.goto('/');
await expect(page.locator('h1')).toContainText('Sum: 3');
});
5 changes: 4 additions & 1 deletion packages/adapter-cloudflare/test/apps/workers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
"build": "vite build",
"preview": "wrangler dev dist/index.js",
"prepare": "svelte-kit sync || echo ''",
"test": "playwright test"
"test:dev": "cross-env DEV=true playwright test",
"test:build": "playwright test",
"test": "pnpm test:dev && pnpm test:build"
},
"devDependencies": {
"@sveltejs/kit": "workspace:^",
"@sveltejs/vite-plugin-svelte": "catalog:",
"cross-env": "catalog:",
"server-side-dep": "file:server-side-dep",
"svelte": "^5.23.1",
"vite": "catalog:",
Expand Down
3 changes: 3 additions & 0 deletions packages/adapter-cloudflare/test/apps/workers/src/app.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// TODO: remove this in 3.0 once svelte.config.js is included by the generated tsconfig.json
// this ensures we get the ambient types from the adapter
import '../../../../index.js';
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// this tests that Wrangler can correctly resolve and bundle server-side dependencies
import { sum } from 'server-side-dep';

export function load() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function GET({ platform }) {
return new Response(platform?.ctx.waitUntil ? 'ctx works' : 'ctx does not work');
}
7 changes: 6 additions & 1 deletion packages/adapter-cloudflare/test/apps/workers/test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { expect, test } from '@playwright/test';

test('worker works', async ({ page }) => {
test('worker', async ({ page }) => {
await page.goto('/');
await expect(page.locator('h1')).toContainText('Sum: 3');
});

test('ctx', async ({ request }) => {
const res = await request.get('/ctx');
expect(await res.text()).toBe('ctx works');
});
4 changes: 2 additions & 2 deletions packages/adapter-cloudflare/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export const config = {
// generous timeouts on CI
timeout: process.env.CI ? 45000 : 15000,
webServer: {
command: 'pnpm build && pnpm preview',
port: 8787
command: process.env.DEV ? 'pnpm dev' : 'pnpm build && pnpm preview',
port: process.env.DEV ? 5173 : 8787
},
retries: process.env.CI ? 2 : 0,
projects: [
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/amp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@sveltejs/amp": "workspace:^",
"@sveltejs/kit": "workspace:^",
"@sveltejs/vite-plugin-svelte": "catalog:",
"cross-env": "^7.0.3",
"cross-env": "catalog:",
"dropcss": "^1.0.16",
"svelte": "^5.23.1",
"svelte-check": "^4.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/basics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"devDependencies": {
"@sveltejs/kit": "workspace:^",
"@sveltejs/vite-plugin-svelte": "catalog:",
"cross-env": "^7.0.3",
"cross-env": "catalog:",
"svelte": "^5.23.1",
"svelte-check": "^4.1.1",
"redirect-pkg": "file:./_test_dependencies/redirect-pkg",
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/dev-only/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"devDependencies": {
"@sveltejs/kit": "workspace:^",
"@sveltejs/vite-plugin-svelte": "catalog:",
"cross-env": "^7.0.3",
"cross-env": "catalog:",
"e2e-test-dep-error": "file:./_test_dependencies/cjs-only",
"e2e-test-dep-hooks": "file:./_test_dependencies/cjs-only",
"e2e-test-dep-hooks-client": "file:./_test_dependencies/cjs-only",
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/embed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"devDependencies": {
"@sveltejs/kit": "workspace:^",
"@sveltejs/vite-plugin-svelte": "catalog:",
"cross-env": "^7.0.3",
"cross-env": "catalog:",
"svelte": "^5.23.1",
"svelte-check": "^4.1.1",
"typescript": "^5.5.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/hash-based-routing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"devDependencies": {
"@sveltejs/kit": "workspace:^",
"@sveltejs/vite-plugin-svelte": "catalog:",
"cross-env": "^7.0.3",
"cross-env": "catalog:",
"svelte": "^5.23.1",
"svelte-check": "^4.1.1",
"typescript": "^5.5.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/no-ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"devDependencies": {
"@sveltejs/kit": "workspace:^",
"@sveltejs/vite-plugin-svelte": "catalog:",
"cross-env": "^7.0.3",
"cross-env": "catalog:",
"svelte": "^5.23.1",
"svelte-check": "^4.1.1",
"typescript": "^5.5.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/options-2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@sveltejs/adapter-node": "workspace:^",
"@sveltejs/kit": "workspace:^",
"@sveltejs/vite-plugin-svelte": "catalog:",
"cross-env": "^7.0.3",
"cross-env": "catalog:",
"svelte": "^5.23.1",
"svelte-check": "^4.1.1",
"typescript": "^5.5.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/options/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@fontsource/libre-barcode-128-text": "^5.1.0",
"@sveltejs/kit": "workspace:^",
"@sveltejs/vite-plugin-svelte": "catalog:",
"cross-env": "^7.0.3",
"cross-env": "catalog:",
"svelte": "^5.23.1",
"svelte-check": "^4.1.1",
"typescript": "^5.5.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"devDependencies": {
"@sveltejs/kit": "workspace:^",
"@sveltejs/vite-plugin-svelte": "catalog:",
"cross-env": "^7.0.3",
"cross-env": "catalog:",
"svelte": "^5.23.1",
"svelte-check": "^4.1.1",
"typescript": "^5.5.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/writes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"devDependencies": {
"@sveltejs/kit": "workspace:^",
"@sveltejs/vite-plugin-svelte": "catalog:",
"cross-env": "^7.0.3",
"cross-env": "catalog:",
"svelte": "^5.23.1",
"svelte-check": "^4.1.1",
"typescript": "^5.5.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"devDependencies": {
"@sveltejs/kit": "workspace:^",
"@sveltejs/vite-plugin-svelte": "catalog:",
"cross-env": "^7.0.3",
"cross-env": "catalog:",
"svelte": "^5.23.1",
"svelte-check": "^4.1.1",
"typescript": "^5.5.4",
Expand Down
28 changes: 17 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ packages:
catalog:
'@playwright/test': '^1.51.1'
'@sveltejs/vite-plugin-svelte': '^6.0.0-next.0'
'cross-env': '^7.0.3'
'vitest': '^3.2.3'
'vite': '^6.3.5'
Loading