Skip to content
Draft
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/cloudflare-current-platform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare': patch
---

fix: read the current Cloudflare platform proxy after Wrangler config reloads
22 changes: 10 additions & 12 deletions packages/adapter-cloudflare/src/virtual-cloudflare-workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import { AsyncLocalStorage } from 'node:async_hooks';

const als = new AsyncLocalStorage();

const proxy = globalThis.__sveltekit_cloudflare_platform;

function get_current_env() {
return als.getStore() ?? proxy?.env;
return als.getStore() ?? globalThis.__sveltekit_cloudflare_platform?.env;
}

/** @typedef {typeof CloudflareWorkersModule} Module */
Expand All @@ -15,7 +13,7 @@ export const env = new Proxy(
{},
{
get(_, prop) {
if (!proxy) {
if (!globalThis.__sveltekit_cloudflare_platform) {
throw new Error(`Cannot access cloudflare:workers in a prerenderable route`);
}
const inner = get_current_env();
Expand All @@ -26,7 +24,7 @@ export const env = new Proxy(
},

set(_, prop, newValue) {
if (!proxy) {
if (!globalThis.__sveltekit_cloudflare_platform) {
throw new Error(`Cannot access cloudflare:workers in a prerenderable route`);
}
const inner = get_current_env();
Expand All @@ -37,7 +35,7 @@ export const env = new Proxy(
},

has(_, prop) {
if (!proxy) {
if (!globalThis.__sveltekit_cloudflare_platform) {
throw new Error(`Cannot access cloudflare:workers in a prerenderable route`);
}
const inner = get_current_env();
Expand All @@ -48,7 +46,7 @@ export const env = new Proxy(
},

ownKeys(_) {
if (!proxy) {
if (!globalThis.__sveltekit_cloudflare_platform) {
throw new Error(`Cannot access cloudflare:workers in a prerenderable route`);
}
const inner = get_current_env();
Expand All @@ -59,7 +57,7 @@ export const env = new Proxy(
},

deleteProperty(_, prop) {
if (!proxy) {
if (!globalThis.__sveltekit_cloudflare_platform) {
throw new Error(`Cannot access cloudflare:workers in a prerenderable route`);
}
const inner = get_current_env();
Expand All @@ -70,7 +68,7 @@ export const env = new Proxy(
},

defineProperty(_, prop, attr) {
if (!proxy) {
if (!globalThis.__sveltekit_cloudflare_platform) {
throw new Error(`Cannot access cloudflare:workers in a prerenderable route`);
}
const inner = get_current_env();
Expand All @@ -81,7 +79,7 @@ export const env = new Proxy(
},

getOwnPropertyDescriptor(_, prop) {
if (!proxy) {
if (!globalThis.__sveltekit_cloudflare_platform) {
throw new Error(`Cannot access cloudflare:workers in a prerenderable route`);
}
const inner = get_current_env();
Expand All @@ -95,14 +93,14 @@ export const env = new Proxy(

/** @type {Module['withEnv']} */
export function withEnv(newEnv, fn) {
if (!proxy) {
if (!globalThis.__sveltekit_cloudflare_platform) {
throw new Error(`Cannot access cloudflare:workers in a prerenderable route`);
}
return als.run(newEnv, fn);
}
/** @type {Module['withEnvAndExports']} */
export function withEnvAndExports(newEnv, _, fn) {
if (!proxy) {
if (!globalThis.__sveltekit_cloudflare_platform) {
throw new Error(`Cannot access cloudflare:workers in a prerenderable route`);
}
return als.run(newEnv, fn);
Expand Down
27 changes: 27 additions & 0 deletions packages/adapter-cloudflare/test/apps/workers/test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'node:fs';
import path from 'node:path';
import { expect, test } from '@playwright/test';
import { location } from '../../../../../../test-utils/deploy.js';

test('worker', async ({ page }) => {
await page.goto('/');
Expand All @@ -12,6 +13,32 @@ test('cloudflare:workers', async ({ request }) => {
expect(await res.text()).toBe('from wrangler.jsonc');
});

test('cloudflare:workers uses the current bindings after a wrangler config reload', async ({
request
}) => {
test.skip(!process.env.DEV);
const config_path = path.join(location(process.cwd()), 'config/wrangler.jsonc');
const original = fs.readFileSync(config_path, 'utf-8');

/** @param {string} contents */
function write_config(contents) {
// Replace atomically so Wrangler cannot read a partially written config.
fs.writeFileSync(`${config_path}.tmp`, contents);
fs.renameSync(`${config_path}.tmp`, config_path);
}

// Evaluate cloudflare:workers before the platform proxy is replaced.
expect(await (await request.get('/env')).text()).toBe('from wrangler.jsonc');

try {
write_config(original.replace('from wrangler.jsonc', 'updated binding'));
await expect.poll(async () => (await request.get('/env')).text()).toBe('updated binding');
} finally {
write_config(original);
await expect.poll(async () => (await request.get('/env')).text()).toBe('from wrangler.jsonc');
}
});

test('Request.cf', async ({ request }) => {
const res = await request.get('cf');
const cf = await res.json();
Expand Down
Loading