Compare commits
10 Commits
2f882ae8cd
...
fbccf3d9e1
Author | SHA1 | Date | |
---|---|---|---|
fbccf3d9e1 | |||
c5d7d4b275 | |||
fb64977d06 | |||
d7e9854e70 | |||
db8e8bfd61 | |||
510f1c81dd | |||
6f3b1e5b3f | |||
f424937b17 | |||
85a131cd74 | |||
6d981c6fdf |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
node_modules
|
node_modules
|
||||||
build
|
build
|
||||||
|
public/bundle.js
|
||||||
package-lock.json
|
package-lock.json
|
||||||
|
@ -4,8 +4,10 @@
|
|||||||
"description": "Server side rendering project",
|
"description": "Server side rendering project",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"dev": "npm-run-all --parallel dev:*",
|
||||||
"dev:server": "nodemon --watch build --exec \"node build/bundle.js\"",
|
"dev:server": "nodemon --watch build --exec \"node build/bundle.js\"",
|
||||||
"dev:build:server": "webpack --config webpack.server.js --watch"
|
"dev:build-server": "webpack --config webpack.server.js --watch",
|
||||||
|
"dev:build-client": "webpack --config webpack.client.js --watch"
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
|
11
src/client/Routes.js
Normal file
11
src/client/Routes.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import React from "react";
|
||||||
|
import { Route } from "react-router-dom";
|
||||||
|
import Home from "./components/Home";
|
||||||
|
|
||||||
|
export default () => {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<Route exact path="/" component={Home} />
|
||||||
|
</div>
|
||||||
|
) ;
|
||||||
|
};
|
@ -1,7 +1,12 @@
|
|||||||
import React from "react"; // ES2015 module syntax
|
import React from "react"; // ES2015 module syntax
|
||||||
|
|
||||||
const Home = () => {
|
const Home = () => {
|
||||||
return <div>Home component</div>;
|
return (
|
||||||
|
<div>
|
||||||
|
<div>Home component</div>
|
||||||
|
<button onClick={() => console.log('button clicked')}>Button</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Home;
|
export default Home;
|
11
src/client/index.js
Normal file
11
src/client/index.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import React from "react";
|
||||||
|
import ReactDOM from 'react-dom';
|
||||||
|
import { BrowserRouter } from "react-router-dom";
|
||||||
|
import Routes from './Routes';
|
||||||
|
|
||||||
|
ReactDOM.hydrate(
|
||||||
|
<BrowserRouter>
|
||||||
|
<Routes />
|
||||||
|
</BrowserRouter>,
|
||||||
|
document.querySelector('#root')
|
||||||
|
);
|
23
src/helpers/renderer.js
Normal file
23
src/helpers/renderer.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import React from "react";
|
||||||
|
import { renderToString } from "react-dom/server";
|
||||||
|
import { StaticRouter } from "react-router-dom";
|
||||||
|
import Routes from "../client/Routes";
|
||||||
|
|
||||||
|
export default (req) => {
|
||||||
|
const content = renderToString(
|
||||||
|
<StaticRouter location={req.path} context={{}}>
|
||||||
|
<Routes />
|
||||||
|
</StaticRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
return `
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head></head>
|
||||||
|
<body>
|
||||||
|
<div id="root">${content}</div>
|
||||||
|
<script src="bundle.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`
|
||||||
|
};
|
14
src/index.js
14
src/index.js
@ -1,13 +1,11 @@
|
|||||||
const express = require('express'); // CommonJS module syntax
|
import express from 'express';
|
||||||
const React = require('react');
|
import renderer from './helpers/renderer';
|
||||||
const renderToString = require('react-dom/server').renderToString;
|
|
||||||
const Home = require('./client/components/Home').default;
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
app.get('/', (req, res) => {
|
app.use(express.static('public'));
|
||||||
const content = renderToString(<Home />);
|
app.get('*', (req, res) => {
|
||||||
|
res.send(renderer(req));
|
||||||
res.send(content);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(3000, () => {
|
app.listen(3000, () => {
|
||||||
|
18
webpack.base.js
Normal file
18
webpack.base.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
module.exports = {
|
||||||
|
module: {
|
||||||
|
rules: [
|
||||||
|
{
|
||||||
|
test: /\.js?$/,
|
||||||
|
loader: 'babel-loader',
|
||||||
|
exclude: /node_modules/,
|
||||||
|
options: {
|
||||||
|
presets: [
|
||||||
|
'react',
|
||||||
|
'stage-0',
|
||||||
|
['env', { targets: { browsers: ['last 2 versions'] }}]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
13
webpack.client.js
Normal file
13
webpack.client.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
const path = require('path');
|
||||||
|
const merge = require('webpack-merge');
|
||||||
|
const baseConfig = require('./webpack.base.js');
|
||||||
|
|
||||||
|
const config = {
|
||||||
|
entry: './src/client/index.js',
|
||||||
|
output: {
|
||||||
|
filename: 'bundle.js',
|
||||||
|
path: path.resolve(__dirname, 'public')
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = merge(baseConfig, config);
|
@ -1,33 +1,17 @@
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const merge = require('webpack-merge');
|
||||||
|
const baseConfig = require('./webpack.base.js');
|
||||||
|
const webpackNodeExternals = require('webpack-node-externals');
|
||||||
|
|
||||||
module.exports = {
|
const config = {
|
||||||
// Tell webpack that we're building a bundle for Node.js, rather than for the browser
|
|
||||||
target: 'node',
|
target: 'node',
|
||||||
|
|
||||||
// Tell webpack the root file of our server application
|
|
||||||
entry: './src/index.js',
|
entry: './src/index.js',
|
||||||
|
|
||||||
// Tell webpack where to put the output file that is generated
|
|
||||||
output: {
|
output: {
|
||||||
filename: 'bundle.js',
|
filename: 'bundle.js',
|
||||||
path: path.resolve(__dirname, 'build')
|
path: path.resolve(__dirname, 'build')
|
||||||
},
|
},
|
||||||
|
|
||||||
// Tell webpack to run babel on every file it runs through
|
externals: [webpackNodeExternals()]
|
||||||
module: {
|
};
|
||||||
rules: [
|
|
||||||
{
|
module.exports = merge(baseConfig, config);
|
||||||
test: /\.js?$/,
|
|
||||||
loader: 'babel-loader',
|
|
||||||
exclude: /node_modules/,
|
|
||||||
options: {
|
|
||||||
presets: [
|
|
||||||
'react',
|
|
||||||
'stage-0',
|
|
||||||
['env', { targets: { browsers: ['last 2 versions'] }}]
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
};
|
|
Loading…
Reference in New Issue
Block a user