Skip to content

Commit 5947716

Browse files
timneutkensrauchg
authored andcommitted
Implement preact/inferno SSR (vercel#1346)
* Use module-alias to alias preact server side * Use module-alias to alias inferno server side * Remove unneeded routes example
1 parent bcd582a commit 5947716

File tree

6 files changed

+89
-6
lines changed

6 files changed

+89
-6
lines changed

examples/using-inferno/next.config.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
module.exports = {
2-
webpack: function (config) {
2+
webpack: function (config, { dev }) {
3+
// For the development version, we'll use React.
4+
// Because, it support react hot loading and so on.
5+
if (dev) {
6+
return config
7+
}
8+
39
config.resolve.alias = {
410
'react': 'inferno-compat',
511
'react-dom': 'inferno-compat'

examples/using-inferno/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
"name": "using-inferno",
33
"version": "1.0.0",
44
"scripts": {
5-
"dev": "next",
5+
"dev": "node server.js",
66
"build": "next build",
7-
"start": "next start"
7+
"start": "NODE_ENV=production node server.js"
88
},
99
"dependencies": {
1010
"inferno": "^1.0.7",
1111
"inferno-compat": "^1.0.7",
12+
"inferno-server": "^1.3.0-rc.9",
13+
"module-alias": "^2.0.0",
1214
"next": "^2.0.0-beta",
1315
"react": "^15.4.2",
1416
"react-dom": "^15.4.2"

examples/using-inferno/server.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const dev = process.env.NODE_ENV !== 'production'
2+
const moduleAlias = require('module-alias')
3+
4+
// For the development version, we'll use React.
5+
// Because, it support react hot loading and so on.
6+
if (!dev) {
7+
moduleAlias.addAlias('react', 'inferno-compat')
8+
moduleAlias.addAlias('react-dom/server', 'inferno-server')
9+
moduleAlias.addAlias('react-dom', 'inferno-compat')
10+
}
11+
12+
const { createServer } = require('http')
13+
const { parse } = require('url')
14+
const next = require('next')
15+
16+
const app = next({ dev })
17+
const handle = app.getRequestHandler()
18+
19+
app.prepare()
20+
.then(() => {
21+
createServer((req, res) => {
22+
const parsedUrl = parse(req.url, true)
23+
handle(req, res, parsedUrl)
24+
})
25+
.listen(3000, (err) => {
26+
if (err) throw err
27+
console.log('> Ready on http://localhost:3000')
28+
})
29+
})

examples/using-preact/next.config.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
module.exports = {
2-
webpack: function (config) {
2+
webpack: function (config, { dev }) {
3+
// For the development version, we'll use React.
4+
// Because, it support react hot loading and so on.
5+
if (dev) {
6+
return config
7+
}
8+
39
config.resolve.alias = {
410
'react': 'preact-compat/dist/preact-compat',
511
'react-dom': 'preact-compat/dist/preact-compat'
612
}
13+
14+
// Disable uglify. This has been fixed in https://github.com/developit/preact-compat/issues/155.
15+
// Can be removed once there is a new preact-compat release.
16+
config.plugins = config.plugins.filter((plugin) => {
17+
if (plugin.constructor.name === 'UglifyJsPlugin') {
18+
return false
19+
} else {
20+
return true
21+
}
22+
})
23+
724
return config
825
}
926
}

examples/using-preact/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
"name": "hello-world",
33
"version": "1.0.0",
44
"scripts": {
5-
"dev": "next",
5+
"dev": "node server.js",
66
"build": "next build",
7-
"start": "next start"
7+
"start": "NODE_ENV=production node server.js"
88
},
99
"dependencies": {
10+
"module-alias": "^2.0.0",
1011
"next": "^2.0.0-beta",
1112
"preact": "^7.1.0",
1213
"preact-compat": "^3.9.4",

examples/using-preact/server.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const dev = process.env.NODE_ENV !== 'production'
2+
const moduleAlias = require('module-alias')
3+
4+
// For the development version, we'll use React.
5+
// Because, it support react hot loading and so on.
6+
if (dev) {
7+
moduleAlias.addAlias('react', 'preact-compat')
8+
moduleAlias.addAlias('react-dom', 'preact-compat')
9+
}
10+
11+
const { createServer } = require('http')
12+
const { parse } = require('url')
13+
const next = require('next')
14+
15+
const app = next({ dev })
16+
const handle = app.getRequestHandler()
17+
18+
app.prepare()
19+
.then(() => {
20+
createServer((req, res) => {
21+
const parsedUrl = parse(req.url, true)
22+
handle(req, res, parsedUrl)
23+
})
24+
.listen(3000, (err) => {
25+
if (err) throw err
26+
console.log('> Ready on http://localhost:3000')
27+
})
28+
})

0 commit comments

Comments
 (0)