Skip to content

Commit e5558f2

Browse files
authored
Merge pull request #10 from imbhargav5/fix-2
Add server routing
2 parents 29e3309 + 53a9619 commit e5558f2

File tree

10 files changed

+258
-55
lines changed

10 files changed

+258
-55
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"babel-preset-stage-0": "^6.24.1",
44
"express": "^4.15.4",
55
"prop-types": "^15.5.10",
6+
"pug": "^2.0.0-rc.3",
67
"react": "next",
78
"react-dom": "next",
89
"react-redux": "^5.0.6",
@@ -22,6 +23,7 @@
2223
"copy-webpack-plugin": "^4.0.1",
2324
"cross-env": "^5.0.5",
2425
"html-webpack-plugin": "^2.30.1",
26+
"html-webpack-pug-plugin": "^0.2.1",
2527
"nodemon": "^1.11.0",
2628
"npm-run-all": "^4.0.2",
2729
"react-hot-loader": "next",

src/client/app.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import Header from "./components/Header";
66
import Core from "./components/Core";
77
import Footer from "./components/Footer";
88
import store from "./store";
9+
import Home from "./containers/Home";
10+
import About from "./containers/About";
11+
import NotFoundPage from "./containers/NotFound";
912
import styled, { injectGlobal } from "styled-components";
1013

1114
// Global styles
@@ -24,10 +27,10 @@ const Root = styled.div``;
2427
const RootMain = styled.div`min-height: 600px;`;
2528

2629
// Fetch bundles
27-
const Home = bundle(() => import("./containers/Home"));
28-
const About = bundle(() => import("./containers/About"));
30+
// Dynamic imports
31+
// These components don't have server rendering at the moment
32+
// TO-DO : Add server rendering if possible
2933
const Counter = bundle(() => import("./containers/Counter"));
30-
const NotFoundPage = bundle(() => import("./containers/NotFound"));
3134

3235
export default class App extends Component {
3336
render() {

src/client/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from "react";
22
import { BrowserRouter } from "react-router-dom";
3-
import { render } from "react-dom";
3+
// hydrate is responsible for server rendering going forward
4+
import { hydrate as render } from "react-dom";
45
import App from "./app";
56

67
render(

src/server/app.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,19 @@ const port = process.env.PORT || 8888;
2020
const staticPath = path.join(__dirname, "../../", "static");
2121
app.use(express.static(staticPath));
2222

23+
// set view engine
24+
app.set("view engine", "pug");
25+
// set views directory
26+
if (isDevelopment) {
27+
app.set("views", path.join(__dirname, "templates"));
28+
} else {
29+
app.set("views", path.join(__dirname, "../../", "static/templates"));
30+
}
31+
2332
//Root html template
24-
let indexFile = path.join(__dirname, "templates/index.dev.html");
33+
let indexTemplate = "index.dev.pug";
2534
if (!isDevelopment) {
26-
indexFile = path.join(__dirname, "../../index.html");
35+
indexTemplate = "index";
2736
}
2837

2938
// add routes
@@ -37,9 +46,12 @@ app.get("*", (req, res) => {
3746
</Router>
3847
)
3948
);
40-
// it will contain classnames
41-
// console.log(html);
42-
res.sendFile(indexFile);
49+
const styleTags = sheet.getStyleTags();
50+
// This renders html as well as a concatenated string list of style tags
51+
res.render(indexTemplate, {
52+
content: html,
53+
styles: styleTags
54+
});
4355
});
4456

4557
// start app

src/server/templates/index.dev.html

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/server/templates/index.dev.pug

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
doctype html
2+
html
3+
head
4+
meta(charset="utf-8")
5+
title Summer
6+
link(rel="shortcut icon", type="image/png", href="/logo.png")
7+
link(rel="shortcut icon", type="image/png", href="http://localhost:8888/favicon.png")
8+
!= styles
9+
body
10+
#app!=content
11+
script(src="http://localhost:8080/manifest.bundle.js", charset="utf-8")
12+
script(src="http://localhost:8080/vendor.bundle.js", charset="utf-8")
13+
script(src="http://localhost:8080/main.bundle.js", charset="utf-8")

src/server/templates/index.prod.html

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/server/templates/index.prod.pug

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
doctype html
2+
html
3+
head
4+
meta(charset="utf-8")
5+
title Summer
6+
link(rel="shortcut icon", type="image/png", href="/logo.png")
7+
link(rel="shortcut icon", type="image/png", href="http://localhost:8888/favicon.png")
8+
!= styles
9+
body
10+
#app!=content

webpack.config.babel.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import webpack from "webpack";
22
import path from "path";
3+
import HtmlWebpackPugPlugin from "html-webpack-pug-plugin";
34
import HtmlWebpackPlugin from "html-webpack-plugin";
45
import CopyWebpackPlugin from "copy-webpack-plugin";
56

@@ -51,11 +52,12 @@ export default env => {
5152
ifDev(new webpack.HotModuleReplacementPlugin()),
5253
ifProd(
5354
new HtmlWebpackPlugin({
54-
title: "pubg",
55-
filename: "index.html",
56-
template: path.join(__dirname, "src/server/templates/index.prod.html")
55+
title: "Summer",
56+
filename: "templates/index.pug",
57+
template: path.join(__dirname, "src/server/templates/index.prod.pug")
5758
})
5859
),
60+
ifProd(new HtmlWebpackPugPlugin()),
5961
new webpack.optimize.CommonsChunkPlugin({
6062
name: "vendor",
6163
minChunks: function(module) {

0 commit comments

Comments
 (0)