Skip to content

Commit

Permalink
feat(server-runtime): pass AppLoadContext to handleRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandojbf committed Mar 17, 2023
1 parent 12e5aa6 commit 4769f1b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 5 deletions.
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
- F3n67u
- federicoestevez
- fergusmeiklejohn
- fernandojbf
- fgiuliani
- fishel-feng
- francisudeji
Expand Down
4 changes: 3 additions & 1 deletion docs/file-conventions/entry.server.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Here's a basic example:
```tsx
import { renderToString } from "react-dom/server";
import type {
AppLoadContext,
EntryContext,
HandleDataRequestFunction,
} from "@remix-run/node"; // or cloudflare/deno
Expand All @@ -25,7 +26,8 @@ export default function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext
remixContext: EntryContext,
loadContext: AppLoadContext
) {
const markup = renderToString(
<RemixServer context={remixContext} url={request.url} />
Expand Down
42 changes: 42 additions & 0 deletions packages/remix-server-runtime/__tests__/server-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1721,5 +1721,47 @@ describe("shared server runtime", () => {
expect(calls.length).toBe(2 * DATA_CALL_MULTIPIER);
expect(spy.console.mock.calls.length).toBe(1 * DATA_CALL_MULTIPIER);
});

test("provides load context to server entrypoint", async () => {
let rootLoader = jest.fn(() => {
return "root";
});
let indexLoader = jest.fn(() => {
return "index";
});
let build = mockServerBuild({
root: {
default: {},
loader: rootLoader,
ErrorBoundary: {},
},
"routes/index": {
parentId: "root",
default: {},
loader: indexLoader,
},
});

build.entry.module.default = jest.fn(
async (
request,
responseStatusCode,
responseHeaders,
entryContext,
loadContext
) =>
new Response(JSON.stringify(loadContext), {
status: responseStatusCode,
headers: responseHeaders,
})
);

let handler = createRequestHandler(build, ServerMode.Development);
let request = new Request(`${baseUrl}/`, { method: "get" });
let loadContext = { "load-context": "load-value" };

let result = await handler(request, loadContext);
expect(await result.text()).toBe(JSON.stringify(loadContext));
});
});
});
8 changes: 7 additions & 1 deletion packages/remix-server-runtime/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ export function mockServerBuild(
entry: {
module: {
default: jest.fn(
async (request, responseStatusCode, responseHeaders, entryContext) =>
async (
request,
responseStatusCode,
responseHeaders,
entryContext,
loadContext
) =>
new Response(null, {
status: responseStatusCode,
headers: responseHeaders,
Expand Down
4 changes: 3 additions & 1 deletion packages/remix-server-runtime/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { DataFunctionArgs } from "./routeModules";
import type { AssetsManifest, EntryContext, FutureConfig } from "./entry";
import type { ServerRouteManifest } from "./routes";
import type { AppLoadContext } from "./data";

/**
* The output of the compiler for the server build.
Expand All @@ -22,7 +23,8 @@ export interface HandleDocumentRequestFunction {
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
context: EntryContext
context: EntryContext,
loadContext: AppLoadContext
): Promise<Response> | Response;
}

Expand Down
6 changes: 4 additions & 2 deletions packages/remix-server-runtime/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ async function handleDocumentRequestRR(
request,
context.statusCode,
headers,
entryContext
entryContext,
loadContext
);
} catch (error: unknown) {
// Get a new StaticHandlerContext that contains the error at the right boundary
Expand Down Expand Up @@ -328,7 +329,8 @@ async function handleDocumentRequestRR(
request,
context.statusCode,
headers,
entryContext
entryContext,
loadContext
);
} catch (error: any) {
logServerErrorIfNotAborted(error, request, serverMode);
Expand Down

0 comments on commit 4769f1b

Please sign in to comment.