Skip to content

Commit fb88ff7

Browse files
fix: deprecate cloudflare platform.context in favor for platform.ctx (#13856)
* fix: deprecate cloudflare platform.context in favor for platform.ctx (#13851) * add test * format * don't format cloudflare test apps --------- Co-authored-by: Chew Tee Ming <[email protected]>
1 parent 8b93550 commit fb88ff7

File tree

28 files changed

+69
-35
lines changed

28 files changed

+69
-35
lines changed

.changeset/moody-baboons-drum.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-cloudflare': patch
3+
---
4+
5+
fix: deprecate `platform.context` in favor of `platform.ctx` to align with Cloudflare's naming convention

.prettierrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
"**/.custom-out-dir/**",
2727
"**/build/**",
2828
"**/test-results/**",
29+
"**/.wrangler/**",
2930
"documentation/**/*.md",
3031
"packages/package/test/fixtures/**/expected/**/*",
3132
"packages/package/test/watch/expected/**/*",
3233
"packages/package/test/watch/package/**/*",
33-
"packages/kit/src/core/postbuild/fixtures/**/*"
34+
"packages/kit/src/core/postbuild/fixtures/**/*",
35+
"packages/adapter-cloudflare/test/apps/workers/dist/**/*"
3436
],
3537
"options": {
3638
"rangeEnd": 0

documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Functions contained in the [`/functions` directory](https://developers.cloudflar
122122

123123
## Runtime APIs
124124

125-
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:
125+
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:
126126

127127
```js
128128
// @errors: 7031

documentation/docs/25-build-and-deploy/70-adapter-cloudflare-workers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ wrangler deploy
7777

7878
## Runtime APIs
7979

80-
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:
80+
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:
8181

8282
```js
8383
// @errors: 7031

packages/adapter-cloudflare/ambient.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ declare global {
88
namespace App {
99
export interface Platform {
1010
env: unknown;
11+
ctx: ExecutionContext;
12+
/** @deprecated Use `ctx` instead */
1113
context: ExecutionContext;
1214
caches: CacheStorage;
1315
cf?: IncomingRequestCfProperties;

packages/adapter-cloudflare/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ export default function (options = {}) {
135135
const proxy = await getPlatformProxy(options.platformProxy);
136136
const platform = /** @type {App.Platform} */ ({
137137
env: proxy.env,
138-
context: proxy.ctx,
138+
ctx: proxy.ctx,
139+
context: proxy.ctx, // deprecated in favor of ctx
139140
caches: proxy.caches,
140141
cf: proxy.cf
141142
});

packages/adapter-cloudflare/src/worker.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ export default {
1313
/**
1414
* @param {Request} req
1515
* @param {{ ASSETS: { fetch: typeof fetch } }} env
16-
* @param {ExecutionContext} context
16+
* @param {ExecutionContext} ctx
1717
* @returns {Promise<Response>}
1818
*/
19-
async fetch(req, env, context) {
19+
async fetch(req, env, ctx) {
2020
await server.init({
2121
// @ts-expect-error env contains environment variables and bindings
2222
env
@@ -70,7 +70,8 @@ export default {
7070
res = await server.respond(req, {
7171
platform: {
7272
env,
73-
context,
73+
ctx,
74+
context: ctx, // deprecated in favor of ctx
7475
// @ts-expect-error webworker types from worktop are not compatible with Cloudflare Workers types
7576
caches,
7677
// @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
@@ -85,6 +86,6 @@ export default {
8586
// write to `Cache` only if response is not an error,
8687
// let `Cache.save` handle the Cache-Control and Vary headers
8788
pragma = res.headers.get('cache-control') || '';
88-
return pragma && res.status < 400 ? Cache.save(req, res, context) : res;
89+
return pragma && res.status < 400 ? Cache.save(req, res, ctx) : res;
8990
}
9091
};

packages/adapter-cloudflare/test/apps/pages/src/routes/+page.server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// this tests that Wrangler can correctly resolve and bundle server-side dependencies
12
import { sum } from 'server-side-dep';
23

34
export function load() {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect, test } from '@playwright/test';
22

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

packages/adapter-cloudflare/test/apps/workers/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
"build": "vite build",
88
"preview": "wrangler dev dist/index.js",
99
"prepare": "svelte-kit sync || echo ''",
10-
"test": "playwright test"
10+
"test:dev": "cross-env DEV=true playwright test",
11+
"test:build": "playwright test",
12+
"test": "pnpm test:dev && pnpm test:build"
1113
},
1214
"devDependencies": {
1315
"@sveltejs/kit": "workspace:^",
1416
"@sveltejs/vite-plugin-svelte": "catalog:",
17+
"cross-env": "catalog:",
1518
"server-side-dep": "file:server-side-dep",
1619
"svelte": "^5.23.1",
1720
"vite": "catalog:",

0 commit comments

Comments
 (0)