From 94afaa3e501f77e919c719937eb1dbfe170e3dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torbj=C3=B8rn=20Holtmon?= <46750288+TorbjornHoltmon@users.noreply.github.com> Date: Tue, 20 Jun 2023 12:24:32 +0200 Subject: [PATCH] fix: cloudflare waitUntil not working as inteded with getRuntime (#7419) Co-authored-by: Bjorn Lu --- .changeset/clever-apes-shop.md | 6 ++++++ packages/integrations/cloudflare/src/server.advanced.ts | 7 +++++-- packages/integrations/cloudflare/src/server.directory.ts | 6 +++++- 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 .changeset/clever-apes-shop.md diff --git a/.changeset/clever-apes-shop.md b/.changeset/clever-apes-shop.md new file mode 100644 index 000000000..0b5a4acd6 --- /dev/null +++ b/.changeset/clever-apes-shop.md @@ -0,0 +1,6 @@ +--- +'@astrojs/cloudflare': patch +--- + +Fixed issue with cloudflare runtime `waitUntil` not working as intended. + diff --git a/packages/integrations/cloudflare/src/server.advanced.ts b/packages/integrations/cloudflare/src/server.advanced.ts index 510e4e7e5..9758b8b19 100644 --- a/packages/integrations/cloudflare/src/server.advanced.ts +++ b/packages/integrations/cloudflare/src/server.advanced.ts @@ -1,4 +1,4 @@ -import type { Request as CFRequest } from '@cloudflare/workers-types'; +import type { Request as CFRequest, ExecutionContext } from '@cloudflare/workers-types'; import type { SSRManifest } from 'astro'; import { App } from 'astro/app'; import { getProcessEnvProxy, isNode } from './util.js'; @@ -15,7 +15,7 @@ type Env = { export function createExports(manifest: SSRManifest) { const app = new App(manifest); - const fetch = async (request: Request & CFRequest, env: Env, context: any) => { + const fetch = async (request: Request & CFRequest, env: Env, context: ExecutionContext) => { process.env = env as any; const { pathname } = new URL(request.url); @@ -38,6 +38,9 @@ export function createExports(manifest: SSRManifest) { caches, cf: request.cf, ...context, + waitUntil: (promise: Promise) => { + context.waitUntil(promise); + }, }); let response = await app.render(request, routeData); diff --git a/packages/integrations/cloudflare/src/server.directory.ts b/packages/integrations/cloudflare/src/server.directory.ts index a0f3299fd..649d0d99b 100644 --- a/packages/integrations/cloudflare/src/server.directory.ts +++ b/packages/integrations/cloudflare/src/server.directory.ts @@ -1,4 +1,4 @@ -import type { Request as CFRequest } from '@cloudflare/workers-types'; +import type { Request as CFRequest, EventContext } from '@cloudflare/workers-types'; import type { SSRManifest } from 'astro'; import { App } from 'astro/app'; import { getProcessEnvProxy, isNode } from './util.js'; @@ -17,6 +17,7 @@ export function createExports(manifest: SSRManifest) { }: { request: Request & CFRequest; next: (request: Request) => void; + waitUntil: EventContext['waitUntil']; } & Record) => { process.env = runtimeEnv.env as any; @@ -35,6 +36,9 @@ export function createExports(manifest: SSRManifest) { ); Reflect.set(request, Symbol.for('runtime'), { ...runtimeEnv, + waitUntil: (promise: Promise) => { + runtimeEnv.waitUntil(promise); + }, name: 'cloudflare', next, caches,