npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sapper-navigation-enhancer

v0.1.6

Published

Sapper navigation enhancer

Downloads

2

Readme

sapper-navigation-enhancer

Important

  • You need to use the goto function exported by sapper-navigation-enhancer instead of the goto function exported by Sapper.
  • You need to use the redirect function exported by sapper-navigation-enhancer instead of Sappers this.redirect in your preload functions.

Install

npm install sapper-navigation-enhancer

Initialization

src/client.js

import { start } from '@sapper/app';
import beforeStart from 'sapper-navigation-enhancer';

const afterStart = beforeStart(['/'], true);
start({ target: document.querySelector('#app') });
afterStart();

src/routes/_layout.svelte

<script>
  import { goto, stores } from '@sapper/app';
  import { init } from 'sapper-navigation-enhancer';

  const { page } = stores();

  init(page, goto);
</script>

API

back

function back(fallback: string | () => string): void;

If canGoBack() returns true, navigates to the previous history entry (like history.back()).
If canGoBack() returns false, navigates to the fallback url. It will do so by prepending a history entry with the fallback url before the current history entry and then navigates back.

beforeNavigate

type Callback = (href: string) => false | Promise<false | any> | any;
type Unsubscribe = () => void;
function beforeNavigate(callback: Callback, useBeforeUnload: boolean = false): Unsubscribe;

Subscribe to navigation attempts. Navigation will be prevented, when you return false or Promise<false>.

If useBeforeUnload (or alwaysUseBeforeUnload in beforeStart) is true, a onbeforeunload listener will be created.
Returns an Unsubscribe function, which must be called when the component is destroyed.

<!-- some-route-or-component.svelte -->
<script>
  import { beforeNavigate } from 'sapper-navigation-enhancer';
  import { onMount } from 'svelte';

  onMount(() => beforeNavigate(href => confirm(`Do you want to navigate to ${href}?`)));
</script>

beforeStart

type AfterStart = () => void;
function beforeStart(startPaths?: string[], alwaysUseBeforeUnload: boolean = false): AfterStart;

You need to call this function before you call Sappers start function and you need to call the returned AfterStart function after you call Sappers start function. Typically in the client.js.

If you provide the startPaths parameter and the current location.pathname does not match any of them, it will prepend a history entry - with the first item of startPaths array as the url - before the current history entry.
If alwaysUseBeforeUnload is true, a onbeforeunload listener will be created when calling beforeNavigate or preventNavigation.

This is the default exported function also, see Initialization.

canGoBack

function canGoBack(): boolean;

Returns true if the previous history entry is from your app.

At the same time, canGoBack is a readable store.

<!-- some-component.svelte -->
<script>
  import { back, canGoBack } from 'sapper-navigation-enhancer';
</script>

{#if $canGoBack}
  <button on:click={() => back('/')}>Go back</button>
{/if}

goto

function goto(
  href: string,
  opts: {
    force?: boolean;
    noscroll?: boolean;
    replaceStart?: boolean;
    state?: Record<string, any>;
  } = {}
): Promise<void>;

You need to use this function instead of Sappers goto function.

If opts.force is true, no beforeNavigate callback will be called.
If you called preventNavigation, opts.force has no effect.

With opts.state you can set history.state properties for the next history entry.

<!-- some-component.svelte -->
<script>
-  import { goto } from '@sapper/app';
+  import { goto } from 'sapper-navigation-enhancer';
</script>

<button on:click={() => goto('/')}>Home</button>

init

import type { PageContext } from '@sapper/common';
import type { Readable } from 'svelte/store';
type Goto = typeof import('@sapper/app').goto;
function init(page: Readable<PageContext>, goto: Goto): void;

You need to call this in your root layout component, see Initialization.

preventNavigation

type RemovePrevention = () => void;
function preventNavigation(useBeforeUnload: boolean = false): RemovePrevention;

Prevents navigation. Returns a function that stops the prevention when called.

If useBeforeUnload (or alwaysUseBeforeUnload in beforeStart) is true, a onbeforeunload listener will be created.

<!-- some-component.svelte -->
<script>
  import { preventNavigation } from 'sapper-navigation-enhancer';

  let preventing;
  function togglePrevention() {
    if (preventing) {
      preventing();
      preventing = null;
    } else {
      preventing = preventNavigation();
    }
  }
</script>

<button on:click={togglePrevention}>Toggle navigation prevention</button>

redirect

import type { PreloadContext } from '@sapper/common';
function redirect(preloadContext: PreloadContext, statusCode: number, location: string): void;

You need to use this function instead of Sappers this.redirect() inside your preload functions.

<!-- some-route-or-layout.svelte -->
<script context="module">
+  import { redirect } from 'sapper-navigation-enhancer';

  export function preload() {
-    this.redirect(302, '/');
+    redirect(this, 302, '/');
  }
</script>

state

function state(state: Record<string, any>): void;

Update the history.state of the current history entry.