Skip to content

Commit

Permalink
feat: prefetch link preloads
Browse files Browse the repository at this point in the history
If a route exports `<link rel="preload">`, those are added as prefetches for `<Link prefetch>`. If the route says they are important enough to make high priority, then we can speed it up even more by prefetching before the user gets there.

This enables routes to tell links to prefetch any kind of asset without the linking component needing to know.
  • Loading branch information
ryanflorence committed Dec 3, 2021
1 parent 7c59285 commit fe4d395
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
Binary file modified fixtures/gists-app/app/components/guitar.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion fixtures/gists-app/app/routes/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export default function Index() {
<nav>
<ul>
<li>
<Link to="/links">Link preloads and stuff</Link>
<Link prefetch="intent" to="/links">
Link preloads and stuff
</Link>
</li>
<li>
<Link to="/gists">View Some gists</Link>
Expand Down
22 changes: 7 additions & 15 deletions fixtures/gists-app/app/routes/links.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useLoaderData, Link } from "remix";

import redTextHref from "~/styles/redText.css";
import blueTextHref from "~/styles/blueText.css";
import guitar from "~/components/guitar.jpg";

interface User {
name: string;
Expand Down Expand Up @@ -30,7 +31,9 @@ export let links: LinksFunction = () => {
// preload another page
let pageLink = { page: `/gists/mjackson` };

return [styleLink, nonMatching, fails, pageLink];
let preloadGuitar = { rel: "preload", as: "image", href: guitar };

return [styleLink, nonMatching, fails, pageLink, preloadGuitar];
};

export default function LinksPage() {
Expand All @@ -45,23 +48,12 @@ export default function LinksPage() {
</Link>
</li>
))}
{/*

<hr />
<p>
<img alt="another guitar..." src={guitar.src} data-test-id="blocked" />
<br />
Preloaded and blocked guitar, no layout shift.
</p>
<p>
<img
alt="another guitar..."
src={notPreloadedGuitar.src}
data-test-id="not-blocked"
/>
<br />
Not preloaded, not blocked, layout shift!
<img alt="a guitar" src={guitar} data-test-id="blocked" /> Prefetched
because it's a preload.
</p>
*/}
</div>
);
}
9 changes: 7 additions & 2 deletions packages/remix-react/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,13 @@ export async function getStylesheetPrefetchLinks(
return links
.flat(1)
.filter(isHtmlLinkDescriptor)
.filter(link => link.rel === "stylesheet")
.map(({ rel, ...attrs }) => ({ rel: "prefetch", as: "style", ...attrs }));
.filter(link => link.rel === "stylesheet" || link.rel === "preload")
.map(({ rel, ...attrs }) => {
if (rel === "preload") {
return { rel: "prefetch", ...attrs };
}
return { rel: "prefetch", as: "style", ...attrs };
});
}

// This is ridiculously identical to transition.ts `filterMatchesToLoad`
Expand Down

0 comments on commit fe4d395

Please sign in to comment.