Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support async getLoadContext in all adapters #6170

Merged
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
12 changes: 12 additions & 0 deletions .changeset/async-load-context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"remix": patch
"@remix-run/architect": patch
"@remix-run/cloudflare": patch
"@remix-run/cloudflare-pages": patch
"@remix-run/cloudflare-workers": patch
"@remix-run/express": patch
"@remix-run/netlify": patch
"@remix-run/vercel": patch
---

feat: support async `getLoadContext` in all adapters
4 changes: 2 additions & 2 deletions packages/remix-architect/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { isBinaryType } from "./binaryTypes";
*/
export type GetLoadContextFunction = (
event: APIGatewayProxyEventV2
) => AppLoadContext;
) => Promise<AppLoadContext> | AppLoadContext;

export type RequestHandler = APIGatewayProxyHandlerV2;

Expand All @@ -50,7 +50,7 @@ export function createRequestHandler({

return async (event) => {
let request = createRemixRequest(event);
let loadContext = getLoadContext?.(event);
let loadContext = await getLoadContext?.(event);

let response = (await handleRequest(request, loadContext)) as NodeResponse;

Expand Down
6 changes: 3 additions & 3 deletions packages/remix-cloudflare-pages/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createRequestHandler as createRemixRequestHandler } from "@remix-run/cl
*/
export type GetLoadContextFunction<Env = unknown> = (
context: Parameters<PagesFunction<Env>>[0]
) => AppLoadContext;
) => Promise<AppLoadContext> | AppLoadContext;

export type RequestHandler<Env = any> = PagesFunction<Env>;

Expand All @@ -27,8 +27,8 @@ export function createRequestHandler<Env = any>({
}: createPagesFunctionHandlerParams<Env>): RequestHandler<Env> {
let handleRequest = createRemixRequestHandler(build, mode);

return (context) => {
let loadContext = getLoadContext?.(context);
return async (context) => {
let loadContext = await getLoadContext?.(context);

return handleRequest(context.request, loadContext);
};
Expand Down
8 changes: 5 additions & 3 deletions packages/remix-cloudflare-workers/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import { createRequestHandler as createRemixRequestHandler } from "@remix-run/cl
* You can think of this as an escape hatch that allows you to pass
* environment/platform-specific values through to your loader/action.
*/
export type GetLoadContextFunction = (event: FetchEvent) => AppLoadContext;
export type GetLoadContextFunction = (
event: FetchEvent
) => Promise<AppLoadContext> | AppLoadContext;

export type RequestHandler = (event: FetchEvent) => Promise<Response>;

Expand All @@ -36,8 +38,8 @@ export function createRequestHandler({
}): RequestHandler {
let handleRequest = createRemixRequestHandler(build, mode);

return (event: FetchEvent) => {
let loadContext = getLoadContext?.(event);
return async (event: FetchEvent) => {
let loadContext = await getLoadContext?.(event);

return handleRequest(event.request, loadContext);
};
Expand Down
4 changes: 2 additions & 2 deletions packages/remix-express/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
export type GetLoadContextFunction = (
req: express.Request,
res: express.Response
) => AppLoadContext;
) => Promise<AppLoadContext> | AppLoadContext;

export type RequestHandler = (
req: express.Request,
Expand Down Expand Up @@ -53,7 +53,7 @@ export function createRequestHandler({
) => {
try {
let request = createRemixRequest(req, res);
let loadContext = getLoadContext?.(req, res);
let loadContext = await getLoadContext?.(req, res);

let response = (await handleRequest(
request,
Expand Down
4 changes: 2 additions & 2 deletions packages/remix-netlify/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { isBinaryType } from "./binaryTypes";
export type GetLoadContextFunction = (
event: HandlerEvent,
context: HandlerContext
) => AppLoadContext;
) => Promise<AppLoadContext> | AppLoadContext;

export type RequestHandler = Handler;

Expand All @@ -47,7 +47,7 @@ export function createRequestHandler({

return async (event, context) => {
let request = createRemixRequest(event);
let loadContext = getLoadContext?.(event, context);
let loadContext = await getLoadContext?.(event, context);

let response = (await handleRequest(request, loadContext)) as NodeResponse;

Expand Down
4 changes: 2 additions & 2 deletions packages/remix-vercel/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
export type GetLoadContextFunction = (
req: VercelRequest,
res: VercelResponse
) => AppLoadContext;
) => Promise<AppLoadContext> | AppLoadContext;

export type RequestHandler = (
req: VercelRequest,
Expand All @@ -47,7 +47,7 @@ export function createRequestHandler({

return async (req, res) => {
let request = createRemixRequest(req, res);
let loadContext = getLoadContext?.(req, res);
let loadContext = await getLoadContext?.(req, res);

let response = (await handleRequest(request, loadContext)) as NodeResponse;

Expand Down