Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POC] Pretty url routing #1001

Merged
merged 3 commits into from
Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions examples/with-pretty-url-routing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

# Example app with pretty url routing

## How to use

Download the example [or clone the repo](https://github.com/zeit/next.js):

```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-pretty-url-routing
cd with-pretty-url-routing
```

Install it and run:

```bash
npm install
npm run dev
```

Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))

```bash
now
```

## The idea behind the example

This example features:
- route customisation and parameterization
- reverse routing
12 changes: 12 additions & 0 deletions examples/with-pretty-url-routing/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "NODE_ENV=production node server.js"
},
"dependencies": {
"express": "^4.14.1",
"next": "^2.0.0-beta.23",
"next-url-prettifier": "^1.0.2"
}
}
34 changes: 34 additions & 0 deletions examples/with-pretty-url-routing/pages/greeting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react'
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.linkPage('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: React.PropTypes.string,
name: React.PropTypes.string
}
13 changes: 13 additions & 0 deletions examples/with-pretty-url-routing/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react'

export default function IndexPage () {
return (
<div>
<h1>Homepage</h1>
<form method='GET' action='/greeting'>
Name: <input name='name' />
<input type='submit' />
</form>
</div>
)
}
21 changes: 21 additions & 0 deletions examples/with-pretty-url-routing/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const UrlPrettifier = require('next-url-prettifier').default

const routes = [
{
page: 'index',
prettyUrl: '/home'
},
{
page: 'greeting',
prettyUrl: ({lang = '', name = ''}) =>
(lang === 'fr' ? `/bonjour/${name}` : `/hello/${name}`),
prettyUrlPatterns: [
{pattern: '/hello/:name', defaultParams: {lang: 'en'}},
{pattern: '/bonjour/:name', defaultParams: {lang: 'fr'}}
]
}
]

const urlPrettifier = new UrlPrettifier(routes)
exports.default = routes
exports.Router = urlPrettifier
20 changes: 20 additions & 0 deletions examples/with-pretty-url-routing/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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.forEachPattern((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)
})