Url prettifier for Next Framework
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
routes.js
inside your project root:
Create // routes.jsconst UrlPrettifier = default; const routes = page: 'index' prettyUrl: '/home' page: 'greeting' // `prettyUrl` is used on client side to construct the URL of your link lang === 'fr' ? `/bonjour/` : `/hello/` // `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 = routes;exportsdefault = routes;exportsRouter = urlPrettifier;
In your components:
// pages/greeting.js;;;; Component static { return lang name; } { const lang name = thisprops; const switchLang = lang === 'fr' ? 'en' : 'fr'; return <Link route=Router> <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> ); */ } { const lang name = thisprops; return <div> <h1>lang === 'fr' ? 'Bonjour' : 'Hello' name</h1> <div>this</div> </div> ; } GreetingPagepropTypes = lang: PropTypesstring name: PropTypesstring;
server.js
file (example with Express.js):
In your // server.jsconst express = ;const next = ;const Router = Router; const dev = processenvNODE_ENV !== 'production';const port = || 3000;const app = ;const handle = app; app ;