Skip to content

Commit

Permalink
feat: add http/2 link header for preloading scripts
Browse files Browse the repository at this point in the history
Signed-off-by: Logan McAnsh <[email protected]>
  • Loading branch information
mcansh committed Jan 1, 2023
1 parent 5d489c0 commit 934a7b1
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ export default function handleDocumentRequest(
<RemixServer context={remixContext} url={request.url} />
);

// Add Link header for HTTP/2 Server Push
let modules = Object.entries(remixContext.manifest.routes);
let http2PushLinksHeaders = remixContext.staticHandlerContext.matches
.flatMap((match) => {
let routeMatch = modules.find((m) => m[0] === match.route.id);
if (!routeMatch) return [];
let routeImports = routeMatch[1].imports ?? [];
return [routeMatch[1].module, ...routeImports];
})
.filter(Boolean)
.concat([
remixContext.manifest.url,
remixContext.manifest.entry.module,
...remixContext.manifest.entry.imports,
]);

responseHeaders.set(
"Link",
http2PushLinksHeaders
.map((link) => `<${link}>; rel=preload; as=script; crossorigin=anonymous`)
.concat(responseHeaders.get("Link") ?? "")
.filter(Boolean)
.join()
);

responseHeaders.set("Content-Type", "text/html");

if (process.env.NODE_ENV === "development") {
Expand Down

1 comment on commit 934a7b1

@vercel
Copy link

@vercel vercel bot commented on 934a7b1 Jan 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.