Skip to content
Open
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
1 change: 1 addition & 0 deletions src/presets/netlify/runtime/netlify-edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default async function netlifyEdge(netlifyReq: Request, context: Context)
req.ip = context.ip;
req.runtime ??= { name: "netlify-edge" };
req.runtime.netlify ??= { context } as any;
req.waitUntil ??= context.waitUntil?.bind(context);

const url = new URL(req.url);

Expand Down
4 changes: 3 additions & 1 deletion src/presets/netlify/runtime/netlify.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import "#nitro/virtual/polyfills";
import { useNitroApp } from "nitro/app";
import type { Context } from "@netlify/functions";
import type { ServerRequest } from "srvx";

const nitroApp = useNitroApp();

const ONE_YEAR_IN_SECONDS = 365 * 24 * 60 * 60;

const handler = async (req: ServerRequest): Promise<Response> => {
const handler = async (req: ServerRequest, context?: Context): Promise<Response> => {
req.runtime ??= { name: "netlify" };
req.waitUntil ??= context?.waitUntil?.bind(context);
req.ip ??= req.headers.get("x-nf-client-connection-ip") || undefined;

const response = await nitroApp.fetch(req);
Expand Down
29 changes: 28 additions & 1 deletion test/presets/netlify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { tmpdir } from "node:os";
import type { Context as FunctionContext } from "@netlify/functions";
import { execa, execaSync } from "execa";
import { join, resolve } from "pathe";
import { describe, expect, it } from "vitest";
import { describe, expect, it, vi } from "vitest";
import type { Nitro } from "nitro/types";
import {
generateNetlifyEdgeFunction,
Expand Down Expand Up @@ -177,6 +177,18 @@ describe("nitro:preset:netlify", async () => {
);
});

it("forwards event.waitUntil to the Netlify context", async () => {
const { default: handler } = (await import(resolve(ctx.outDir, "server/main.mjs"))) as {
default: (req: Request, ctx: FunctionContext) => Promise<Response>;
};
const waitUntil = vi.fn();
const res = await handler(new Request("https://example.com/wait-until"), {
waitUntil,
} as unknown as FunctionContext);
expect(await res.text()).toBe("done");
expect(waitUntil).toHaveBeenCalledWith(expect.any(Promise));
});

// https://github.com/nitrojs/nitro/issues/5035
// Missing public assets now reach the function, which opts them out of
// the immutable `cache-control` route rule of the asset dir.
Expand Down Expand Up @@ -299,4 +311,19 @@ describe("nitro:preset:netlify-edge", async () => {
);
expect(stdout).toBe("ok");
});

it.runIf(hasDeno)("forwards event.waitUntil to the Netlify context", async () => {
const { stdout } = await execa(
"deno",
[
"eval",
`const { default: handler } = await import("./server.mjs");
let calls = 0;
const res = await handler(new Request("http://localhost/wait-until"), { waitUntil: () => calls++ });
console.log(await res.text(), calls);`,
],
{ cwd: ctx.nitro!.options.output.serverDir }
);
expect(stdout).toBe("done 1");
});
});
Loading