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

react-instantsearch-router-nextjs

v7.13.8

Published

React InstantSearch Router for Next.js

Downloads

167,852

Readme

react-instantsearch-router-nextjs

This package is a router for React InstantSearch that is compatible with Next.js routing.

:warning: This function cannot be used in conjunction with getStaticProps(). Use getServerSideProps() or client-side rendering instead.

Installation

yarn add react-instantsearch-router-nextjs
# or with npm
npm install react-instantsearch-router-nextjs

Usage

You need to pass the Next.js router singleton that you can import from next/router.

If you are doing SSR with the getServerState and InstantSearchSSRProvider from react-instantsearch/server, you need to pass the url prop to createInstantSearchRouterNext's serverUrl option :

import singletonRouter from 'next/router';
import { createInstantSearchRouterNext } from 'react-instantsearch-router-nextjs';

export default function Page({ serverState, url }) {
  return (
    <InstantSearchSSRProvider {...serverState}>
      <InstantSearch
        searchClient={searchClient}
        indexName="instant_search"
        routing={{
          router: createInstantSearchRouterNext({
            singletonRouter,
            serverUrl: url,
          }),
        }}
      >
        {/* ... */}
      </InstantSearch>
    </InstantSearchSSRProvider>
  );
}

If you are not doing SSR but only CSR, you can omit the serverUrl option:

import singletonRouter from 'next/router';
import { createInstantSearchRouterNext } from 'react-instantsearch-router-nextjs';

export default function Page() {
  return (
    <InstantSearch
      searchClient={searchClient}
      indexName="instant_search"
      routing={{ router: createInstantSearchRouterNext({ singletonRouter }) }}
    >
      {/* ... */}
    </InstantSearch>
  );
}

Lastly, if you had custom routing logic in your app, you can pass it to the createInstantSearchRouterNext's routerOptions option:

import singletonRouter from 'next/router';
import { createInstantSearchRouterNext } from 'react-instantsearch-router-nextjs';

export default function Page({ serverState, url }) {
  return (
    {/* ... */}
      <InstantSearch
        searchClient={searchClient}
        indexName="instant_search"
        routing={{
          router: createInstantSearchRouterNext({
            singletonRouter,
            serverUrl: url,
            routerOptions: {
              createURL: /* ... */,
              parseURL: /* ... */,
            },
          }),
           // if you are using a custom `stateMapping` you can still pass it :
          stateMapping: /* ... */,
        }}
      >
        {/* ... */}
      </InstantSearch>
    {/* ... */}
  );
}

API

The options are :

  • singletonRouter: SingletonRouter: the required Next.js router singleton.
  • serverUrl?: string: the URL of the page on the server. Required if you are doing SSR with getServerState and InstantSearchSSRProvider.
  • routerOptions?: RouterOptions: the options passed to the history router. See the documentation for more details.

For troubleshooting purposes, some other options are available :

  • beforeStart?: (onUpdate: () => void) => void: a function called before the router starts. You can use it to inform InstantSearch to update on router events by calling onUpdate.
  • beforeDispose?: () => void: a function called before the router disposes. You can use it to clean up handlers you added in beforeStart.
  • beforePopState?: (options: { state: NextHistoryState, ownBeforePopState: BeforePopStateCallback, libraryBeforePopState: BeforePopStateCallback }) => boolean: a function used by the Next.js router to know whether it should trigger SSR when using back/forward buttons. You can use it to override the default one by writing your own logic. The ownBeforePopState is the pre-existing handler that you may have set yourself, and the libraryBeforePopState is the default one from the library.

Troubleshooting

If you're experiencing issues, please refer to the Need help? section of the docs, or open a new issue.

Contributing

We welcome all contributors, from casual to regular 💙

To start contributing to code, you need to:

  1. Fork the project
  2. Clone the repository
  3. Install the dependencies: yarn

Please read our contribution process to learn more.

License

React InstantSearch is MIT licensed.