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(remix-server-runtime): add loadContext to handleDocumentRequest's signature #1606

Closed
wants to merge 1 commit into from

Conversation

jacob-ebey
Copy link
Member

No description provided.

@mjackson
Copy link
Member

Can you describe a little about the use case you have for this?

@jacob-ebey
Copy link
Member Author

General availability of the ({ request, context }) "context" value for access to things that are injected. Potentially a Redis client for loading things from a cache, etc.

Once use case I particularly have is around streaming and gaining access to the adapters response object. You can check it out here: https://stackblitz.com/edit/github-3vromp?file=app%2Fentry.server.tsx

@MichaelDeBoey MichaelDeBoey changed the title feat: add loadContext to handleDocumentRequest feat(remix-server-runtime): add loadContext to handleDocumentRequest's signature May 26, 2022
@MichaelDeBoey MichaelDeBoey force-pushed the jacob/document-request-context branch from f1ae33e to 12f855f Compare May 26, 2022 14:39
@MichaelDeBoey MichaelDeBoey force-pushed the jacob/document-request-context branch from 12f855f to e2f5c3e Compare June 2, 2022 22:48
@jacob-ebey jacob-ebey closed this Aug 3, 2022
@MichaelDeBoey MichaelDeBoey deleted the jacob/document-request-context branch August 4, 2022 11:21
@jacob-ebey
Copy link
Member Author

Use-case: Relay info from loaders back to entry.server.
Example: Calculate a header in the root loader that should be added to every response without having to export a headers function from every route:

// root.tsx
async function loader({ context }) {
  let response = await fetch("some-third-party");
  context.headers.append("X-Custom", response.headers.get("X-Custom"));
}
// entry.server.ts;
import { renderToString } from "react-dom/server";
import { RemixServer } from "@remix-run/react";
import type { EntryContext } from "@remix-run/node";

export default function handleRequest(
  request: Request,
  responseStatusCode: number,
  responseHeaders: Headers,
  remixContext: EntryContext,
  loadContext: { headers: Headers }
) {
  let markup = renderToString(
    <RemixServer context={remixContext} url={request.url} />
  );

  responseHeaders.set("Content-Type", "text/html");
  responseHeaders.set("X-Custom", loadContext.headers.get("X-Custom"));

  return new Response("<!DOCTYPE html>" + markup, {
    status: responseStatusCode,
    headers: responseHeaders,
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants