Skip to content

Conversation

@pavelbelyaev98
Copy link
Contributor

@pavelbelyaev98 pavelbelyaev98 commented Dec 4, 2021

This pr closes issues #2974 and #560

This pr introduces a onBeforeNavigate interceptor and a onNavigate lifecycle function.

onBeforeNavigate

This is a navigation interceptor that can be used to either look up the incoming URL we are going to navigate to or even prevent the navigation from completing by cancelling the navigation request. It works only for internal app navigation.

The navigation interceptor is called onBeforeNavigate and is being called each time we use the following methods:

  • goto()
  • popstate events
  • internal anchor tags clicks

The interceptor can be used in the following way:

<script>
  import { onBeforeNavigate } from '$app/navigation';

  onBeforeNavigate(async (url) => {
	// the URL we are going to navigate to
	console.log(url);

	// cancel or allow navigation in the return statement
	return false;
   });
	
</script>

It can be used with and without async, also It works without a return.

onNavigate

Additionally, the pr adds a onNavigate lifecycle function that runs when the page mounts, and also whenever SvelteKit navigates to a new URL but stays on this component.

<script>
  import { onNavigate } from '$app/navigation';

       onNavigate(() => {
	  console.log("Navigation compelted!");
        });
	
</script>

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

@changeset-bot
Copy link

changeset-bot bot commented Dec 4, 2021

🦋 Changeset detected

Latest commit: eea4279

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

@pavelbelyaev98
Copy link
Contributor Author

@benmccann I've implemented the required changes, this can be reviewed again.

@bluwy
Copy link
Member

bluwy commented Jan 1, 2022

Note: I've put this into next maintainer's meeting list to discuss the path forward of it.

@netlify
Copy link

netlify bot commented Jan 2, 2022

❌ Deploy Preview for kit-demo failed.

🔨 Explore the source changes: eea4279

🔍 Inspect the deploy log: https://app.netlify.com/sites/kit-demo/deploys/61d177d01fee120008835396

@pavelbelyaev98
Copy link
Contributor Author

Closing this in favor of #3293

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants