Skip to content

Commit db7c286

Browse files
BDav24timneutkens
authored andcommitted
[POC] Pretty url routing (vercel#1001)
* [example] with pretty url routing * use single quotes even in React components * improve Link import
1 parent a57fa7e commit db7c286

File tree

6 files changed

+130
-0
lines changed

6 files changed

+130
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
# Example app with pretty url routing
3+
4+
## How to use
5+
6+
Download the example [or clone the repo](https://github.com/zeit/next.js):
7+
8+
```bash
9+
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-pretty-url-routing
10+
cd with-pretty-url-routing
11+
```
12+
13+
Install it and run:
14+
15+
```bash
16+
npm install
17+
npm run dev
18+
```
19+
20+
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
21+
22+
```bash
23+
now
24+
```
25+
26+
## The idea behind the example
27+
28+
This example features:
29+
- route customisation and parameterization
30+
- reverse routing
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"scripts": {
3+
"dev": "node server.js",
4+
"build": "next build",
5+
"start": "NODE_ENV=production node server.js"
6+
},
7+
"dependencies": {
8+
"express": "^4.14.1",
9+
"next": "^2.0.0-beta.23",
10+
"next-url-prettifier": "^1.0.2"
11+
}
12+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from 'react'
2+
import {Link} from 'next-url-prettifier'
3+
import {Router} from '../routes'
4+
5+
export default class GreetingPage extends React.Component {
6+
static getInitialProps ({query: {lang, name}}) {
7+
return {lang, name}
8+
}
9+
10+
renderSwitchLangageLink () {
11+
const {lang, name} = this.props
12+
const switchLang = lang === 'fr' ? 'en' : 'fr'
13+
return (
14+
<Link route={Router.linkPage('greeting', {name, lang: switchLang})}>
15+
<a>{switchLang === 'fr' ? 'Français' : 'English'}</a>
16+
</Link>
17+
)
18+
}
19+
20+
render () {
21+
const {lang, name} = this.props
22+
return (
23+
<div>
24+
<h1>{lang === 'fr' ? 'Bonjour' : 'Hello'} {name}</h1>
25+
<div>{this.renderSwitchLangageLink()}</div>
26+
</div>
27+
)
28+
}
29+
}
30+
31+
GreetingPage.propTypes = {
32+
lang: React.PropTypes.string,
33+
name: React.PropTypes.string
34+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react'
2+
3+
export default function IndexPage () {
4+
return (
5+
<div>
6+
<h1>Homepage</h1>
7+
<form method='GET' action='/greeting'>
8+
Name: <input name='name' />
9+
<input type='submit' />
10+
</form>
11+
</div>
12+
)
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const UrlPrettifier = require('next-url-prettifier').default
2+
3+
const routes = [
4+
{
5+
page: 'index',
6+
prettyUrl: '/home'
7+
},
8+
{
9+
page: 'greeting',
10+
prettyUrl: ({lang = '', name = ''}) =>
11+
(lang === 'fr' ? `/bonjour/${name}` : `/hello/${name}`),
12+
prettyUrlPatterns: [
13+
{pattern: '/hello/:name', defaultParams: {lang: 'en'}},
14+
{pattern: '/bonjour/:name', defaultParams: {lang: 'fr'}}
15+
]
16+
}
17+
]
18+
19+
const urlPrettifier = new UrlPrettifier(routes)
20+
exports.default = routes
21+
exports.Router = urlPrettifier
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const express = require('express')
2+
const next = require('next')
3+
const Router = require('./routes').Router
4+
5+
const dev = process.env.NODE_ENV !== 'production'
6+
const port = parseInt(process.env.PORT, 10) || 3000
7+
const app = next({dev})
8+
const handle = app.getRequestHandler()
9+
10+
app.prepare()
11+
.then(() => {
12+
const server = express()
13+
14+
Router.forEachPattern((page, pattern, defaultParams) => server.get(pattern, (req, res) =>
15+
app.render(req, res, `/${page}`, Object.assign({}, defaultParams, req.query, req.params))
16+
))
17+
18+
server.get('*', (req, res) => handle(req, res))
19+
server.listen(port)
20+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy