headers

Each route can define its own HTTP headers. One of the common headers is the Cache-Control header that indicates to browser and CDN caches where and for how long a page is able to be cached.

  1. import type { HeadersFunction } from "@remix-run/node"; // or cloudflare/deno
  2. export const headers: HeadersFunction = ({
  3. actionHeaders,
  4. loaderHeaders,
  5. parentHeaders,
  6. errorHeaders,
  7. }) => ({
  8. "X-Stretchy-Pants": "its for fun",
  9. "Cache-Control": "max-age=300, s-maxage=3600",
  10. });

Usually your data is a better indicator of your cache duration than your route module (data tends to be more dynamic than markup), so the action‘s & loader‘s headers are passed in to headers() too:

  1. import type { HeadersFunction } from "@remix-run/node"; // or cloudflare/deno
  2. export const headers: HeadersFunction = ({
  3. loaderHeaders,
  4. }) => ({
  5. "Cache-Control": loaderHeaders.get("Cache-Control"),
  6. });

Note: actionHeaders & loaderHeaders are an instance of the Web Fetch API Headers class.

If an action or a loader threw a Response and we’re rendering a boundary, any headers from the thrown Response will be available in errorHeaders. This allows you to access headers from a child loader that threw in a parent error boundary.

Nested Routes

Because Remix has nested routes, there’s a battle of the headers to be won when nested routes match. The default behavior is that Remix only leverages the resulting headers from the leaf rendered route. Consider these files in the routes directory:

  1. ├── users.tsx
  2. └── users
  3. ├── $userId.tsx
  4. └── $userId
  5. └── profile.tsx

If we are looking at /users/123/profile then three routes are rendering:

  1. <Users>
  2. <UserId>
  3. <Profile />
  4. </UserId>
  5. </Users>

If all three define headers, the deepest module wins, in this case profile.tsx. However, if your profile.tsx loader threw and bubbled to a boundary in userId.tsx - then userId.tsx‘s headers function would be used as it is the leaf rendered route.

We realize that it can be tedious and error-prone to have to define `headers` on every possible leaf route so we’re changing the current behavior in v2 behind the [`future.v2_headers`][v2_headers] flag.

We don’t want surprise headers in your responses, so it’s your job to merge them if you’d like. Remix passes in the parentHeaders to your headers function. So users.tsx headers get passed to $userId.tsx, and then $userId.tsx headers are passed to profile.tsx headers.

That is all to say that Remix has given you a very large gun with which to shoot your foot. You need to be careful not to send a Cache-Control from a child route module that is more aggressive than a parent route. Here’s some code that picks the least aggressive caching in these cases:

  1. import type { HeadersFunction } from "@remix-run/node"; // or cloudflare/deno
  2. import parseCacheControl from "parse-cache-control";
  3. export const headers: HeadersFunction = ({
  4. loaderHeaders,
  5. parentHeaders,
  6. }) => {
  7. const loaderCache = parseCacheControl(
  8. loaderHeaders.get("Cache-Control")
  9. );
  10. const parentCache = parseCacheControl(
  11. parentHeaders.get("Cache-Control")
  12. );
  13. // take the most conservative between the parent and loader, otherwise
  14. // we'll be too aggressive for one of them.
  15. const maxAge = Math.min(
  16. loaderCache["max-age"],
  17. parentCache["max-age"]
  18. );
  19. return {
  20. "Cache-Control": `max-age=${maxAge}`,
  21. };
  22. };

All that said, you can avoid this entire problem by not defining headers in parent routes and only in leaf routes. Every layout that can be visited directly will likely have an “index route”. If you only define headers on your leaf routes, not your parent routes, you will never have to worry about merging headers.

Note that you can also add headers in your entry.server file for things that should be global, for example:

  1. import type {
  2. AppLoadContext,
  3. EntryContext,
  4. } from "@remix-run/node"; // or cloudflare/deno
  5. import { RemixServer } from "@remix-run/react";
  6. import { renderToString } from "react-dom/server";
  7. export default function handleRequest(
  8. request: Request,
  9. responseStatusCode: number,
  10. responseHeaders: Headers,
  11. remixContext: EntryContext,
  12. loadContext: AppLoadContext
  13. ) {
  14. const markup = renderToString(
  15. <RemixServer context={remixContext} url={request.url} />
  16. );
  17. responseHeaders.set("Content-Type", "text/html");
  18. responseHeaders.set("X-Powered-By", "Hugs");
  19. return new Response("<!DOCTYPE html>" + markup, {
  20. status: responseStatusCode,
  21. headers: responseHeaders,
  22. });
  23. }

Just keep in mind that doing this will apply to all document requests, but does not apply to data requests (for client-side transitions for example). For those, use handleDataRequest.

v2 Behavior

Since it can be tedious and error-prone to define a header function in every single possible leaf route, we’re changing the behavior slightly in v2 and you can opt-into the new behavior via the future.v2_headers Future Flag in remix.config.js.

When enabling this flag, Remix will now use the deepest headers function it finds in the renderable matches (up to and including the boundary route if an error is present). You’ll still need to handle merging together headers as shown above for any headers functions above this route.

This means that, re-using the example above:

  1. ├── users.tsx
  2. └── users
  3. ├── $userId.tsx
  4. └── $userId
  5. └── profile.tsx

If a user is looking at /users/123/profile and profile.tsx does not export a headers function, then Remix will use the return value of $userId.tsx‘s headers function. If that file doesn’t export one, then it will use the result of the one in users.tsx, and so on.