Adapters should send 103 Early Hints #5378
Replies: 5 comments 2 replies
-
Explored this approach a bit today as an opportunity to teach myself about Remix's internal architecture, my initial impression is that it wouldn't be too difficult to build out an initial version that would play well with Node and be available to other adapters. When we're about to invoke I've written up a hacky prototype over at ryanwilsonperkin#1 to play around with and would be happy to be involved in any discussions about supporting this feature. |
Beta Was this translation helpful? Give feedback.
-
It'd be great to see this in Remix, we do this already in express. For us we're using early hints for:
|
Beta Was this translation helpful? Give feedback.
-
Related: #3200 |
Beta Was this translation helpful? Give feedback.
-
I am not sure if this is right or wrong, but this is how I did it in my Fastify + Remix setup: import { matchServerRoutes } from '@remix-run/server-runtime/dist/routeMatching';
import { createRoutes } from '@remix-run/server-runtime/dist/routes';
// ...
const serverBuild = (await import('../../build/server')) as ServerBuild;
const routes = createRoutes(serverBuild.routes);
const getLinks = (path: string, maxLinks: number = 10) => {
const matches = matchServerRoutes(routes, path);
return (matches || []).flatMap((match) => {
return [
...(serverBuild.assets.routes[match.route.id].css?.map((css) => {
return `<${css}>; rel=stylesheet; as=style; crossorigin`;
}) ?? []),
...(serverBuild.assets.routes[match.route.id].imports?.map((script) => {
return `<${script}>; rel=modulepreload; as=script; crossorigin`;
}) ?? []),
].slice(maxLinks);
});
};
await app.register(async (instance) => {
instance.removeAllContentTypeParsers();
// https://github.com/mcansh/remix-fastify/issues/244
instance.addContentTypeParser('*', (_request, payload, done) => {
done(null, payload);
});
instance.all('*', async (request, reply) => {
const links = getLinks(request.url);
reply.header(
'Link',
[
'</fonts/gabarito/Gabarito-VariableFont.ttf>; rel=preload; as=font; type=font/ttf; crossorigin',
...links,
].join(', '),
); |
Beta Was this translation helpful? Give feedback.
-
An intermediate step could be to send the Even without a reverse proxy to do the conversion, some browsers can now handle the Cloudflare doesn't support |
Beta Was this translation helpful? Give feedback.
-
For the adapters that support it, we should automatically send 103 Early Hints
links
exportsWe can send all of this before we even start calling middleware or loaders, we can send it immediately.
For browsers that support it, that means the browser is downloading client assets in parallel with the server's data fetching in loaders.
Beta Was this translation helpful? Give feedback.
All reactions