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

next-url-prettifier

v1.4.0

Published

Url prettifier for Next Framework

Downloads

703

Readme

Url prettifier for Next Framework

npm version

Easy to use url prettifier for next.js.

Why you should use it

  • Good integration with Next.js (only adds "href" and "as" props to existing Link)
  • On server-side, use the parameter matching you want (Express.js or else)
  • Handles reverse routing
  • It's flow typed and well tested
  • It's extendable (you can use the query stringfier you want)
  • No dependencies

How to use

Install:

npm i --save next-url-prettifier

Create routes.js inside your project root:

// routes.js
const UrlPrettifier = require('next-url-prettifier').default;

const routes = [
  {
    page: 'index',
    prettyUrl: '/home'
  },
  {
    page: 'greeting',
    // `prettyUrl` is used on client side to construct the URL of your link
    prettyUrl: ({lang = '', name = ''}) =>
      (lang === 'fr' ? `/bonjour/${name}` : `/hello/${name}`),
    // `prettyPatterns` is used on server side to find which component/page to display
    prettyPatterns: [
      {pattern: '/hello/:name', defaultParams: {lang: 'en'}},
      {pattern: '/bonjour/:name', defaultParams: {lang: 'fr'}}
    ]
  }
];

const urlPrettifier = new UrlPrettifier(routes);
exports.default = routes;
exports.Router = urlPrettifier;

In your components:

// pages/greeting.js
import React from 'react';
import PropTypes from 'prop-types';
import {Link} from 'next-url-prettifier';
import {Router} from '../routes';

export default class GreetingPage extends React.Component {
  static getInitialProps({query: {lang, name}}) {
    return {lang, name};
  }

  renderSwitchLangageLink() {
    const {lang, name} = this.props;
    const switchLang = lang === 'fr' ? 'en' : 'fr';
    return (
      <Link route={Router.getPrettyUrl('greeting', {name, lang: switchLang})}>
        <a>{switchLang === 'fr' ? 'Français' : 'English'}</a>
      </Link>
    );
    /*
    Note: you can also use Next native Link and spread notation:
    import Link from 'next/link';
    return (
      <Link {...Router.getPrettyUrl('greeting', {name, lang: switchLang})}>
        <a>{switchLang === 'fr' ? 'Français' : 'English'}</a>
      </Link>
    );
    */
  }

  render() {
    const {lang, name} = this.props;
    return (
      <div>
        <h1>{lang === 'fr' ? 'Bonjour' : 'Hello'} {name}</h1>
        <div>{this.renderSwitchLangageLink()}</div>
      </div>
    );
  }
}

GreetingPage.propTypes = {
  lang: PropTypes.string,
  name: PropTypes.string
};

In your server.js file (example with Express.js):

// server.js
const express = require('express');
const next = require('next');
const Router = require('./routes').Router;

const dev = process.env.NODE_ENV !== 'production';
const port = parseInt(process.env.PORT, 10) || 3000;
const app = next({dev});
const handle = app.getRequestHandler();

app.prepare()
  .then(() => {
    const server = express();

    Router.forEachPrettyPattern((page, pattern, defaultParams) => server.get(pattern, (req, res) =>
      app.render(req, res, `/${page}`, Object.assign({}, defaultParams, req.query, req.params))
    ));

    server.get('*', (req, res) => handle(req, res));
    server.listen(port);
  })
;

Examples: