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(templates): add Link header for Early Hints, HTTP2 Server Push etc. #3200

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
];
}