From 38bf2a5eff2d4a803cdc83eed86a1dc375d82d57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morel=20Se=CC=81bastien?= Date: Sun, 15 May 2022 12:18:32 -0700 Subject: [PATCH] Add HTTP/2 Server Push Link header --- contributors.yml | 1 + templates/remix/app/entry.server.tsx | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/contributors.yml b/contributors.yml index 59d68d42a14..b970de8dce0 100644 --- a/contributors.yml +++ b/contributors.yml @@ -259,6 +259,7 @@ - penspinner - phishy - plastic041 +- plopix - princerajroy - prvnbist - ptitFicus diff --git a/templates/remix/app/entry.server.tsx b/templates/remix/app/entry.server.tsx index aa0aa0978b1..a123baefb6f 100644 --- a/templates/remix/app/entry.server.tsx +++ b/templates/remix/app/entry.server.tsx @@ -14,6 +14,17 @@ export default function handleRequest( responseHeaders.set("Content-Type", "text/html"); + // Add Link header for HTTP/2 Server Push + let http2PushLinksHeaders = remixContext.matches.reduce((acc: string[], { route: { module, imports } }) => { + acc.push(module); + if (imports) { + acc.push(...imports); + } + return acc; + }, []); + http2PushLinksHeaders.push(...[remixContext.manifest.url, remixContext.manifest.entry.module, ...remixContext.manifest.entry.imports]) + responseHeaders.set("Link", (responseHeaders.has("Link") ? responseHeaders.get("Link") + "," : '') + http2PushLinksHeaders.map((link: string) => `<${link}>; rel=preload; as=script`).join(",")); + return new Response("" + markup, { status: responseStatusCode, headers: responseHeaders,