fix: cloudflare waitUntil not working as inteded with getRuntime (#7419)

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
This commit is contained in:
Torbjørn Holtmon 2023-06-20 12:24:32 +02:00 committed by GitHub
parent f87680f6df
commit 94afaa3e50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 3 deletions

View file

@ -0,0 +1,6 @@
---
'@astrojs/cloudflare': patch
---
Fixed issue with cloudflare runtime `waitUntil` not working as intended.

View file

@ -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 type { SSRManifest } from 'astro';
import { App } from 'astro/app'; import { App } from 'astro/app';
import { getProcessEnvProxy, isNode } from './util.js'; import { getProcessEnvProxy, isNode } from './util.js';
@ -15,7 +15,7 @@ type Env = {
export function createExports(manifest: SSRManifest) { export function createExports(manifest: SSRManifest) {
const app = new App(manifest); 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; process.env = env as any;
const { pathname } = new URL(request.url); const { pathname } = new URL(request.url);
@ -38,6 +38,9 @@ export function createExports(manifest: SSRManifest) {
caches, caches,
cf: request.cf, cf: request.cf,
...context, ...context,
waitUntil: (promise: Promise<any>) => {
context.waitUntil(promise);
},
}); });
let response = await app.render(request, routeData); let response = await app.render(request, routeData);

View file

@ -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 type { SSRManifest } from 'astro';
import { App } from 'astro/app'; import { App } from 'astro/app';
import { getProcessEnvProxy, isNode } from './util.js'; import { getProcessEnvProxy, isNode } from './util.js';
@ -17,6 +17,7 @@ export function createExports(manifest: SSRManifest) {
}: { }: {
request: Request & CFRequest; request: Request & CFRequest;
next: (request: Request) => void; next: (request: Request) => void;
waitUntil: EventContext<unknown, any, unknown>['waitUntil'];
} & Record<string, unknown>) => { } & Record<string, unknown>) => {
process.env = runtimeEnv.env as any; process.env = runtimeEnv.env as any;
@ -35,6 +36,9 @@ export function createExports(manifest: SSRManifest) {
); );
Reflect.set(request, Symbol.for('runtime'), { Reflect.set(request, Symbol.for('runtime'), {
...runtimeEnv, ...runtimeEnv,
waitUntil: (promise: Promise<any>) => {
runtimeEnv.waitUntil(promise);
},
name: 'cloudflare', name: 'cloudflare',
next, next,
caches, caches,