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

breaking: let beforeunload handle links that will unload the document #11162

Closed
wants to merge 7 commits into from

Conversation

eltigerchino
Copy link
Member

@eltigerchino eltigerchino commented Dec 1, 2023

fixes #8597

The issue of the missing confirmation dialog stems from this code block that prevents the click event behaviour if we call cancel() in beforeNavigate. As a result, the navigation is never attempted and the beforeunload event never occurs. It needs to occur so that we can can call event.preventDefault() which displays the confirmation dialog.

if (external || options.reload) {
if (before_navigate({ url, type: 'link' })) {
// set `navigating` to `true` to prevent `beforeNavigate` callbacks
// being called when the page unloads
navigating = true;
} else {
event.preventDefault();
}
return;
}

The solution is to simply return in our click handler and delegate the responsibility to the beforeunload handler.

This PR also modifies an existing test to check for the beforeunload confirmation dialog.
However, beforeunload still doesn't occur when clicking on links in Safari #9747 (comment) so the macos tests are failing so the test is skipped for webkit

EDIT: upon re-reading #9747 the concerns mentioned in #9747 (comment) might mean this fix is not the one

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:.

Copy link

changeset-bot bot commented Dec 1, 2023

🦋 Changeset detected

Latest commit: f712f5f

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 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

@eltigerchino eltigerchino changed the title fix: let beforeunload handle external links that will unload the document fix: let beforeunload handle links that will unload the document Dec 1, 2023
@eltigerchino eltigerchino marked this pull request as draft December 2, 2023 14:38
@eltigerchino eltigerchino marked this pull request as ready for review December 2, 2023 16:58
@eltigerchino eltigerchino marked this pull request as draft December 2, 2023 16:58
@benmccann benmccann added this to the 2.0 milestone Dec 4, 2023
@eltigerchino eltigerchino changed the title fix: let beforeunload handle links that will unload the document breaking: let beforeunload handle links that will unload the document Dec 4, 2023
@eltigerchino eltigerchino marked this pull request as ready for review December 4, 2023 07:09
@eltigerchino eltigerchino marked this pull request as draft December 4, 2023 07:13
@Rich-Harris
Copy link
Member

going to close this per #8597 (comment) — we want to be able to intercept external link clicks

@dummdidumm dummdidumm deleted the fix-unload-confirmation-dialog branch December 13, 2023 19:24
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.

beforeNavigate doesn't trigger default browser behaviour as-per the docs
3 participants