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] beforeNavigate and afterNavigate lifecycle functions #3293

Merged
merged 92 commits into from
Jan 13, 2022

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Jan 11, 2022

Todo (from discussion below):

  • rename to beforeNavigate and afterNavigate
  • make beforeNavigate callback synchronous
  • pass full navigation object ({from, to}) to beforeNavigate (and afterNavigate?)
  • add a cancel function to beforeNavigate instead of returning false
  • beforeNavigate should work with external navigation as well as internal navigation
  • beforeNavigate should call onbeforeunload on the user's behalf

For whatever reason, I can't sync my local version of #2982 with that branch, so I've duplicated it for further review. All credit belongs to @zommerberg!

Fixes #2974, fixes #560, and fixes #552.

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 pnpx changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@benmccann benmccann changed the title onNavigate and onBeforeNavigate [feat] beforeNavigate and afterNavigate lifecycle functions Jan 12, 2022
@Rich-Harris
Copy link
Member Author

Example of a 'you have unsaved changes' modal that works with internal navigations and beforeunload:

<script>
  import { beforeNavigate, goto } from '$app/navigation';

  let intercepted = null;
  let unsaved = true;

  beforeNavigate(({ to, cancel }) => {
    if (!unsaved) return; // nothing to do
    if (intercepted) return; // we're here because of the `goto`

    cancel();

    // if `to` has a value, we're navigating internally and
    // should display our own modal. otherwise, we're in
    // beforeunload, which means the user will already
    // see a system dialog and doesn't need ours too
    if (to) {
      intercepted = { url: to };
    }
  });
</script>

{#if intercepted}
  <div class="modal-background" on:click={() => (intercepted = null)}>
    <div class="modal" on:click={(e) => e.stopPropagation()}>
      <p>you have unsaved changes!</p>
      <button on:click={() => (intercepted = null)}>stay on page</button>
      <button on:click={() => goto(intercepted.url)}>leave page</button>
    </div>
  </div>
{/if}

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
@Rich-Harris Rich-Harris merged commit cb44e28 into master Jan 13, 2022
@Rich-Harris Rich-Harris deleted the on-navigate branch January 13, 2022 02:33
Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

A late review, but I have a question below 😬

@@ -122,7 +151,7 @@ export class Router {
addEventListener('sveltekit:trigger_prefetch', trigger_prefetch);

/** @param {MouseEvent} event */
addEventListener('click', (event) => {
addEventListener('click', async (event) => {
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 we can remove this async

@PatrickG
Copy link
Member

Sorry I'm late to the party.

Example of a 'you have unsaved changes' modal that works with internal navigations and beforeunload:

<script>
  import { beforeNavigate, goto } from '$app/navigation';

  let intercepted = null;
  let unsaved = true;

  beforeNavigate(({ to, cancel }) => {
    if (!unsaved) return; // nothing to do
    if (intercepted) return; // we're here because of the `goto`

    cancel();

    // if `to` has a value, we're navigating internally and
    // should display our own modal. otherwise, we're in
    // beforeunload, which means the user will already
    // see a system dialog and doesn't need ours too
    if (to) {
      intercepted = { url: to };
    }
  });
</script>

{#if intercepted}
  <div class="modal-background" on:click={() => (intercepted = null)}>
    <div class="modal" on:click={(e) => e.stopPropagation()}>
      <p>you have unsaved changes!</p>
      <button on:click={() => (intercepted = null)}>stay on page</button>
      <button on:click={() => goto(intercepted.url)}>leave page</button>
    </div>
  </div>
{/if}

The only problem with this is if the user navigated backwards with the browser controls.
The modal will be presented, the user clicks "leave page". If the user then navigates back one more entry, he ends up on the page that presented the modal instead of two pages before that.
That's where async blocking would be helpful.

janosh added a commit to janosh/svelte-toc that referenced this pull request Jan 22, 2022
@benmccann
Copy link
Member

@akshg05
Copy link

akshg05 commented Sep 25, 2022

Hello, I followed the above but am and am able to block the navigation but onbeforeunload is not being called and I don't see the native popup.
Is there something missing which need to be done?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
9 participants