1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Add a nerv example. (#3573)

* Add a nerv example.

* Fix for indentation/style

* Fix for name
This commit is contained in:
Martin Alix 2018-01-15 16:13:51 -05:00 committed by Sergio Xalambrí
parent 77c10b24c5
commit 93990ab3fc
6 changed files with 121 additions and 0 deletions

View file

@ -0,0 +1,44 @@
[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/using-nerv)
# Hello World example
## How to use
### Using `create-next-app`
Download [`create-next-app`](https://github.com/segmentio/create-next-app) to bootstrap the example:
```
npm i -g create-next-app
create-next-app --example using-nerv using-nerv-app
```
### Download manually
Download the example [or clone the repo](https://github.com/zeit/next.js):
```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/using-nerv
cd using-nerv
```
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 [Nerv](https://nerv.aotu.io/) instead of React. It's a "blazing fast React alternative, compatible with IE8 and React 16". Here we've customized Next.js to use Nerv instead of React.
Here's how we did it:
* Use `next.config.js` to customize our webpack config to support [Nerv](https://nerv.aotu.io/)

View file

@ -0,0 +1,16 @@
module.exports = {
webpack: function (config, { dev }) {
// For the development version, we'll use React.
// Because, it supports react hot loading and so on.
if (dev) {
return config
}
config.resolve.alias = {
react: 'nervjs',
'react-dom': 'nervjs'
}
return config
}
}

View file

@ -0,0 +1,21 @@
{
"name": "using-nerv",
"version": "1.0.0",
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "NODE_ENV=production node server.js"
},
"dependencies": {
"module-alias": "^2.0.0",
"next": "latest",
"nervjs": "^1.2.4",
"react": "^16.0.0",
"react-dom": "^16.0.0"
},
"license": "ISC",
"devDependencies": {
"react": "~15.6.1",
"react-dom": "~15.6.1"
}
}

View file

@ -0,0 +1,5 @@
import React from 'react'
export default () => (
<div>About us</div>
)

View file

@ -0,0 +1,6 @@
import React from 'react'
import Link from 'next/link'
export default () => (
<div>Hello World. <Link href='/about'><a>About</a></Link></div>
)

View file

@ -0,0 +1,29 @@
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const moduleAlias = require('module-alias')
// For the development version, we'll use React.
// Because, it support react hot loading and so on.
if (!dev) {
moduleAlias.addAlias('react', 'nervjs')
moduleAlias.addAlias('react-dom', 'nervjs')
}
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')
const app = next({ dev })
const handle = app.getRequestHandler()
app.prepare()
.then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url, true)
handle(req, res, parsedUrl)
})
.listen(port, (err) => {
if (err) throw err
console.log(`> Ready on http://localhost:${port}`)
})
})