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(remix-react): allow <Link>'s "to" prop to accept absolute urls #5092

Merged
merged 11 commits into from Jan 25, 2023

Conversation

mcansh
Copy link
Collaborator

@mcansh mcansh commented Jan 13, 2023

external "to" detection should be brought over to React Router (pr: remix-run/react-router#9900)

Signed-off-by: Logan McAnsh logan@mcan.sh

Closes: #5016

  • Docs
  • Tests

Testing Strategy:

our current testing for Link should be enough, both here and in RR

@changeset-bot
Copy link

changeset-bot bot commented Jan 13, 2023

🦋 Changeset detected

Latest commit: e69f19d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 18 packages
Name Type
remix Patch
@remix-run/react Patch
@remix-run/testing Patch
create-remix Patch
@remix-run/architect Patch
@remix-run/cloudflare Patch
@remix-run/cloudflare-pages Patch
@remix-run/cloudflare-workers Patch
@remix-run/css-bundle Patch
@remix-run/deno Patch
@remix-run/dev Patch
@remix-run/eslint-config Patch
@remix-run/express Patch
@remix-run/netlify Patch
@remix-run/node Patch
@remix-run/serve Patch
@remix-run/server-runtime Patch
@remix-run/vercel Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

{children}
</a>
{shouldPrefetch ? (
<link key={href} rel="prefetch" as="document" href={to} />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you don't need the as="document" here, I'm using it on my blog without that and it works https://github.com/sergiodxa/sergiodxa.com/blob/ca1fa0d9c7f8d92322b01f009951bbfdd54c6214/app/root.tsx#L213-L215

Also when I tried to prefetch links to external websites the browser didn't re-used the prefetched response and did a new request.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like rel=prerender might be what we're actually after https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/prerender

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any browser supporting prerender? I tried in Safari and Chromium (Edge) and it did nothing 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

back working on this, looks like pre-render was removed 🫠 https://developer.chrome.com/blog/prerender-pages/#a-brief-history-of-prerender

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now it's a script tag apparently and idk how to feel lol

<script type="speculationrules">
{
  "prerender": [
    {"source": "list",
     "urls": ["/home", "/about"]}
  ],
  "prefetch": [
    {"source": "list",
     "urls": ["https://en.wikipedia.org/wiki/Hamster_racing"],
     "requires": ["anonymous-client-ip-when-cross-origin"]}
  ]
}
</script>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

additional update: pre-render does work still in chrome, confirmed using https://stackoverflow.com/a/64330790
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to try to optimize external links to apps we don't control, especially if it's potentially not straightforward?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably not, but would've been nice if it was a straightforward api

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah definitely

@mcansh mcansh force-pushed the logan/accept-external-to branch 2 times, most recently from c5bf19d to c9cb93f Compare January 17, 2023 22:00
Comment on lines 328 to 331
let location = typeof to === "string" ? to : createPath(to);

let isAbsolute =
/^[a-z+]+:\/\//i.test(location) || location.startsWith("//");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcansh mcansh force-pushed the logan/accept-external-to branch 2 times, most recently from 81a92dc to f5602b7 Compare January 25, 2023 17:19
@mcansh mcansh changed the title feat(remix-react): allow Link's "to" prop to accept external urls and prefetch document feat(remix-react): allow Link's "to" prop to accept absolute urls Jan 25, 2023
@mcansh mcansh changed the title feat(remix-react): allow Link's "to" prop to accept absolute urls feat(remix-react): allow <Link>'s "to" prop to accept absolute urls Jan 25, 2023
@mcansh mcansh changed the base branch from dev to release-next January 25, 2023 17:24
@mcansh mcansh marked this pull request as ready for review January 25, 2023 17:24
Copy link
Contributor

@brophdawg11 brophdawg11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor comments but looks good to me!

packages/remix-react/components.tsx Outdated Show resolved Hide resolved
Comment on lines +316 to +312
{shouldPrefetch && !isAbsolute ? (
<PrefetchPageLinks page={href} />
) : null}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this simplified approach 👌

packages/remix-react/components.tsx Outdated Show resolved Hide resolved
mcansh and others added 10 commits January 25, 2023 13:36
… prefetch document

external "to" detection should be brought over to React Router

Signed-off-by: Logan McAnsh <logan@mcan.sh>
Signed-off-by: Logan McAnsh <logan@mcan.sh>
Signed-off-by: Logan McAnsh <logan@mcan.sh>
Signed-off-by: Logan McAnsh <logan@mcan.sh>
Signed-off-by: Logan McAnsh <logan@mcan.sh>
Signed-off-by: Logan McAnsh <logan@mcan.sh>
Signed-off-by: Logan McAnsh <logan@mcan.sh>
Co-authored-by: Matt Brophy <matt@brophy.org>
Signed-off-by: Logan McAnsh <logan@mcan.sh>
Signed-off-by: Logan McAnsh <logan@mcan.sh>
@mcansh mcansh merged commit 8ba240f into release-next Jan 25, 2023
@mcansh mcansh deleted the logan/accept-external-to branch January 25, 2023 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants