Skip to content

Commit 7c8cd4b

Browse files
fridaysrauchg
authored andcommitted
Example with next-routes (vercel#1290)
* Example with next-routes * optimize description * rename to with-next-routes
1 parent fd8559c commit 7c8cd4b

File tree

7 files changed

+101
-0
lines changed

7 files changed

+101
-0
lines changed

examples/with-next-routes/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Named routes example ([next-routes](https://github.com/fridays/next-routes))
2+
3+
## How to use
4+
5+
Download the example [or clone the repo](https://github.com/zeit/next.js):
6+
7+
```bash
8+
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-next-routes
9+
cd with-next-routes
10+
```
11+
12+
Install it and run:
13+
14+
```bash
15+
npm install
16+
npm run dev
17+
```
18+
19+
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
20+
21+
```bash
22+
now
23+
```
24+
25+
## The idea behind the example
26+
27+
This example uses [next-routes](https://github.com/fridays/next-routes) for dynamic routing, which lets you define parameterized, named routes with express-style parameters matching.
28+
29+
It works similar to the [parameterized-routing](https://github.com/zeit/next.js/tree/master/examples/parameterized-routing) example and makes use of next.js [custom server and routing](https://github.com/zeit/next.js#custom-server-and-routing) possibilities.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"scripts": {
3+
"dev": "node server.js",
4+
"build": "next build",
5+
"start": "NODE_ENV=production node server.js"
6+
},
7+
"dependencies": {
8+
"next": "^2.0.0-beta",
9+
"next-routes": "^1.0.17",
10+
"react": "^15.4.2",
11+
"react-dom": "^15.4.2"
12+
}
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default props => <h1>About foo {props.url.query.foo}</h1>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react'
2+
3+
const posts = [
4+
{ slug: 'hello-world', title: 'Hello world' },
5+
{ slug: 'another-blog-post', title: 'Another blog post' }
6+
]
7+
8+
export default class extends React.Component {
9+
static async getInitialProps ({ query, res }) {
10+
const post = posts.find(post => post.slug === query.slug)
11+
12+
if (!post && res) {
13+
res.statusCode = 404
14+
}
15+
16+
return { post }
17+
}
18+
19+
render () {
20+
const { post } = this.props
21+
22+
if (!post) return <h1>Post not found</h1>
23+
24+
return <h1>{post.title}</h1>
25+
}
26+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Link, Router } from '../routes'
2+
3+
export default () => (
4+
<ul>
5+
<li><Link route='blog' params={{ slug: 'hello-world' }}><a>Blog: Hello world</a></Link></li>
6+
<li><Link route='blog' params={{ slug: 'another-blog-post' }}><a>Blog: Another blog post</a></Link></li>
7+
<li><Link route='blog' params={{ slug: 'non-existant' }}><a>Blog: Not found</a></Link></li>
8+
<li><button onClick={() => Router.pushRoute('about', { foo: 'bar' })}>About foo bar</button></li>
9+
<li><button onClick={() => Router.pushRoute('about', { foo: 'baz' })}>About foo baz</button></li>
10+
</ul>
11+
)

examples/with-next-routes/routes.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const nextRoutes = require('next-routes')
2+
const routes = module.exports = nextRoutes()
3+
4+
routes.add('blog', '/blog/:slug')
5+
routes.add('about', '/about-us/:foo(bar|baz)')

examples/with-next-routes/server.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { createServer } = require('http')
2+
const next = require('next')
3+
const routes = require('./routes')
4+
5+
const dev = process.env.NODE_ENV !== 'production'
6+
const app = next({ dev })
7+
const handler = routes.getRequestHandler(app)
8+
9+
app.prepare()
10+
.then(() => {
11+
createServer(handler)
12+
.listen(3000, (err) => {
13+
if (err) throw err
14+
console.log('> Ready on http://localhost:3000')
15+
})
16+
})

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