Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 27 additions & 0 deletions examples/named-routes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Named routes example ([next-routes](https://github.com/fridays/next-routes))

## 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/named-routes
cd named-routes
```

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 uses [next-routes](https://github.com/fridays/next-routes) which lets you define named routes with express-style parameters matching.
13 changes: 13 additions & 0 deletions examples/named-routes/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "NODE_ENV=production node server.js"
},
"dependencies": {
"next": "^2.0.0-beta",
"next-routes": "^1.0.17",
"react": "^15.4.2",
"react-dom": "^15.4.2"
}
}
1 change: 1 addition & 0 deletions examples/named-routes/pages/about.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default props => <h1>About foo {props.url.query.foo}</h1>
26 changes: 26 additions & 0 deletions examples/named-routes/pages/blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react'

const posts = [
{ slug: 'hello-world', title: 'Hello world' },
{ slug: 'another-blog-post', title: 'Another blog post' }
]

export default class extends React.Component {
static async getInitialProps ({ query, res }) {
const post = posts.find(post => post.slug === query.slug)

if (!post && res) {
res.statusCode = 404
}

return { post }
}

render () {
const { post } = this.props

if (!post) return <h1>Post not found</h1>

return <h1>{post.title}</h1>
}
}
11 changes: 11 additions & 0 deletions examples/named-routes/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Link, Router } from '../routes'

export default () => (
<ul>
<li><Link route='blog' params={{ slug: 'hello-world' }}><a>Blog: Hello world</a></Link></li>
<li><Link route='blog' params={{ slug: 'another-blog-post' }}><a>Blog: Another blog post</a></Link></li>
<li><Link route='blog' params={{ slug: 'non-existant' }}><a>Blog: Not found</a></Link></li>
<li><button onClick={() => Router.pushRoute('about', { foo: 'bar' })}>About foo bar</button></li>
<li><button onClick={() => Router.pushRoute('about', { foo: 'baz' })}>About foo baz</button></li>
</ul>
)
5 changes: 5 additions & 0 deletions examples/named-routes/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const nextRoutes = require('next-routes')
const routes = module.exports = nextRoutes()

routes.add('blog', '/blog/:slug')
routes.add('about', '/about-us/:foo(bar|baz)')
16 changes: 16 additions & 0 deletions examples/named-routes/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { createServer } = require('http')
const next = require('next')
const routes = require('./routes')

const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handler = routes.getRequestHandler(app)

app.prepare()
.then(() => {
createServer(handler)
.listen(3000, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})