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

fix: prevent beforeNavigate() for download links #9660

Merged
merged 4 commits into from
Apr 17, 2023

Conversation

eltigerchino
Copy link
Member

@eltigerchino eltigerchino commented Apr 13, 2023

fixes #9611

This is a breaking change that causes the client to ignore download links instead of treating them as an external link.
This fixes the issue of the internal navigating flag being incorrectly set, leading to missing subsequent beforeNavigate() calls.

The navigating flag prevents the beforeunload event from triggering a second round of beforeNavigate() callbacks when an external link is clicked. Because the external link causes the page to unload, Kit doesn't care that the navigating flag isn't reset to false. In our download link case, the page isn't unloaded, causing subsequent navigations to not call beforeNavigate(). Therefore, we should treat download and external links differently by opting out of the same logic as external links.

It's possible for us to still call beforeNavigate for download links but downloading isn't really navigating? I'm open to this.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Sorry, something went wrong.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@changeset-bot
Copy link

changeset-bot bot commented Apr 13, 2023

🦋 Changeset detected

Latest commit: bd68039

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

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Minor

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

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

To me this is not a breaking change but a bug fix - I don't think we ever intended to have this triggered for download, nor do any user want this behavior.
PR looks good to me 👍

@eltigerchino
Copy link
Member Author

Whoops, my mistake. I did wonder if anyone out there actually used it this way. I'm fine with marking it as a patch instead.

Copy link
Member

@Rich-Harris Rich-Harris left a comment

Choose a reason for hiding this comment

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

LGTM though i had one small question


return { url, external, target };
const download = url?.origin === location.origin && a.hasAttribute('download');
Copy link
Member

Choose a reason for hiding this comment

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

is the origin check necessary or could it just be this?

Suggested change
const download = url?.origin === location.origin && a.hasAttribute('download');
const download = a.hasAttribute('download');

Copy link
Member Author

Choose a reason for hiding this comment

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

The origin check ensures it only triggers on valid download links.
If a user (for whatever weird reason) has an invalid link such as:

<a href="https://not-the-same-origin.com/file" download>...</a>

The browser navigates to the URL instead of downloading the file. In this case, it could be useful to have beforeNavigate() run.

@Rich-Harris Rich-Harris merged commit 4315e0b into master Apr 17, 2023
@Rich-Harris Rich-Harris deleted the fix-download-and-before-navigate branch April 17, 2023 15:53
@github-actions github-actions bot mentioned this pull request Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

beforeNavigate does not fire after clicking link with download attribute
3 participants