-
Notifications
You must be signed in to change notification settings - Fork 433
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
Add more prefetch conditions #1178
Conversation
Exclude links with data-turbo-stream or associated with UJS behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
cancelable: true | ||
}) | ||
if (unfetchableLink(link)) return false | ||
if (linkToTheSamePage(link)) return false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we discussed IRL, I'm not sure we should always exclude the current page. Sometimes clicking navigation to the same page is a valid way of getting latest content on a page, and in that case prefetching seems valid.
Perhaps there are edge cases to think about when fetching them, though. In any case, probably fine to leave for another PR, just wanted to note is since we're touching this part.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could probably argue that if the current page is prefetched it might as well morph it to the latest version. Though there are probably edge cases as you say.
if (linkOptsOut(link)) return false | ||
if (nonSafeLink(link)) return false | ||
if (eventPrevented(link)) return false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is much nicer with these checks extracted and given meaningful names, btw! 👏
Should we also open PR to remove https://github.com/hotwired/turbo-site/blob/694d442e6e57208814803cb5b2536e93bb398cd6/_source/handbook/02_drive.md#L369-L374 from the examples for canceling a prefetch? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for addressing this so quickly.
cancelable: true | ||
}) | ||
if (unfetchableLink(link)) return false | ||
if (linkToTheSamePage(link)) return false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could probably argue that if the current page is prefetched it might as well morph it to the latest version. Though there are probably edge cases as you say.
That kind of check is not needed since hotwired/turbo#1178
That kind of check is not needed since hotwired/turbo#1178
@seanpdoyle good point, I've removed the example in hotwired/turbo-site#162 |
This PR adds some more conditions where a link is not considered safe to be prefetched.
The risk/reward ratio favors a more conservative approach. If we don't prefetch a link the interaction can be a bit slower, but if we prefetch a link that is not safe, we may trigger a bug.
The conditions are quite specific, in any case, so they shouldn't trigger often.
The new conditions are:
data-remote
attribute).The PR also cleans up the code a bit, extracting each of the conditions into helper methods.
Fixes #1170
Fixes #1174