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-router-server-location

v2.0.0

Published

A server-friendly Location for React Router that can handle redirects & transitions.

Downloads

9

Readme

React Router ServerLocation https://img.shields.io/npm/v/react-router-server-location.svg

A React Router Location for universal apps.


What does ServerLocation Do?

  • Normalizes & exposes server-side request data so that React Router (and your components) can respond to all HTTP methods (e.g. GET, POST).

  • Redirects server-side requests when the router transitions to another URL.

  • Correctly supports complex, deep query strings (e.g. ?foo[bar][baz][bing]=...)

  • Allows the use of <Redirect> routes on the server as well as the client.

  • Works with both Express & Hapi.

Why ServiceLocation?

By default, React Router uses StaticLocation on the server which does not support transitions. Also, in my experience, the onAbort handler has not been a reliable means of handling this behavior.

Plus, ServerLocation allows your app components to take advantage of:

  • Redirect server-side requests via router.transitionTo.
  • The HTTP method via query._method (e.g. GET, POST).
  • POST params are available on the query like normal GET query params.
  • Access to HTTP headers via query._headers (which is useful for pivoting off of X-Requested-With)

Installation

$ npm install --save react-router-server-location

Usage

First, include ServerLocation as a dependency:

import ServerLocation from "react-router-server-location";

Next, create an instance using your server's request & response objects:

// Express
const location = new ServerLocation({ req, res });

// or Hapi
const location = new ServerLocation({ request, reply });

Finally, use React Router as normal:

Router.create({ location, routes }).run((Root) => {
  React.renderToString(<Root />);
});

Now, calls to router.transitionTo will redirect as expected on the server, and POST requests to your server-side React application can be supported!

If you'd like to handle redirects manually, you can instead pass a callback:

const location = new ServerLocation({ req }, function(path) {
  // Maybe save data to the session...
  req.session.redirected = true;
  res.redirect(path);
});

The res or reply objects aren't necessary since we're providing our own callback.

Authors

License

Collaboration

If you have questions or issues, please open an issue!