Skip to content

Commit

Permalink
Add HTTP/2 Server Push Link header
Browse files Browse the repository at this point in the history
  • Loading branch information
Plopix committed Mar 9, 2023
1 parent d6b0499 commit a02154a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 20 deletions.
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@
- phishy
- plastic041
- plondon
- plopix
- pmbanugo
- princerajroy
- prvnbist
Expand Down
42 changes: 32 additions & 10 deletions packages/remix-dev/config/defaults/entry.server.node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ export default function handleRequest(
) {
return isbot(request.headers.get("user-agent"))
? handleBotRequest(
request,
responseStatusCode,
responseHeaders,
remixContext
)
request,
responseStatusCode,
responseHeaders,
remixContext
)
: handleBrowserRequest(
request,
responseStatusCode,
responseHeaders,
remixContext
);
request,
responseStatusCode,
responseHeaders,
remixContext
);
}

function handleBotRequest(
Expand All @@ -46,6 +46,13 @@ function handleBotRequest(
const body = new PassThrough();

responseHeaders.set("Content-Type", "text/html");
responseHeaders.set(
"Link",
httpPushLinks(remixContext).map((link: string) => `<${link}>; rel=preload; as=script; crossorigin=anonymous`)
.concat(responseHeaders.get("Link") as string)
.filter(Boolean)
.join(",")
);

resolve(
new Response(body, {
Expand Down Expand Up @@ -88,6 +95,13 @@ function handleBrowserRequest(
const body = new PassThrough();

responseHeaders.set("Content-Type", "text/html");
responseHeaders.set(
"Link",
httpPushLinks(remixContext).map((link: string) => `<${link}>; rel=preload; as=script; crossorigin=anonymous`)
.concat(responseHeaders.get("Link") as string)
.filter(Boolean)
.join(",")
);

resolve(
new Response(body, {
Expand All @@ -111,3 +125,11 @@ function handleBrowserRequest(
setTimeout(abort, ABORT_DELAY);
});
}

function httpPushLinks(remixContext: EntryContext) {
return [
remixContext.manifest.url,
remixContext.manifest.entry.module,
...remixContext.manifest.entry.imports,
];
}
44 changes: 34 additions & 10 deletions templates/remix/app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ export default function handleRequest(
) {
return isbot(request.headers.get("user-agent"))
? handleBotRequest(
request,
responseStatusCode,
responseHeaders,
remixContext
)
request,
responseStatusCode,
responseHeaders,
remixContext
)
: handleBrowserRequest(
request,
responseStatusCode,
responseHeaders,
remixContext
);
request,
responseStatusCode,
responseHeaders,
remixContext
);
}

function handleBotRequest(
Expand All @@ -46,6 +46,14 @@ function handleBotRequest(
const body = new PassThrough();

responseHeaders.set("Content-Type", "text/html");
responseHeaders.set(
"Link",
httpPushLinks(remixContext)
.map((link: string) => `<${link}>; rel=preload; as=script; crossorigin=anonymous`)
.concat(responseHeaders.get("Link") as string)
.filter(Boolean)
.join(",")
);

resolve(
new Response(body, {
Expand Down Expand Up @@ -88,6 +96,13 @@ function handleBrowserRequest(
const body = new PassThrough();

responseHeaders.set("Content-Type", "text/html");
responseHeaders.set(
"Link",
httpPushLinks(remixContext).map((link: string) => `<${link}>; rel=preload; as=script; crossorigin=anonymous`)
.concat(responseHeaders.get("Link") as string)
.filter(Boolean)
.join(",")
);

resolve(
new Response(body, {
Expand All @@ -111,3 +126,12 @@ function handleBrowserRequest(
setTimeout(abort, ABORT_DELAY);
});
}


function httpPushLinks(remixContext: EntryContext) {
return [
remixContext.manifest.url,
remixContext.manifest.entry.module,
...remixContext.manifest.entry.imports,
];
}

0 comments on commit a02154a

Please sign in to comment.