Skip to content

Commit 1277936

Browse files
committed
Load DB on each request to test service worker
1 parent d2f1473 commit 1277936

File tree

6 files changed

+21
-11
lines changed

6 files changed

+21
-11
lines changed
File renamed without changes.

server/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"babel-loader": "^7.1.2",
2121
"babel-preset-react-app": "^3.1.0",
2222
"concurrently": "^3.5.1",
23-
"json-loader": "^0.5.7",
2423
"nodemon": "^1.14.0",
2524
"webpack": "^3.10.0",
2625
"webpack-node-externals": "^1.6.0"

server/src/loadDb.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import fs from 'fs'
2+
3+
export default function loadDb () {
4+
return new Promise((r, e) =>
5+
fs.readFile('./db.json', (err, data) => {
6+
if (err) { return e(err) }
7+
const db = JSON.parse(data.toString())
8+
r(db)
9+
})
10+
)
11+
}

server/src/middlewares.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import db from './db.json'
2-
import fs from 'fs'
31
import finalhandler from 'finalhandler'
2+
import loadDb from './loadDb'
43

5-
export function api ({ method, url }, res, next) {
4+
export async function api ({ method, url }, res, next) {
65
if (method !== 'GET' || url !== '/api/posts') { return next() }
6+
const db = await loadDb()
77
res.writeHead(200, { 'Content-Type': 'application/json' })
88
res.end(JSON.stringify(db))
99
}

server/src/prerenderClient.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import serveStatic from 'serve-static'
22
import fs from 'fs'
3-
import db from './db.json'
3+
import loadDb from './loadDb'
44
import { renderToStrings } from '../../client/src/renderToStrings'
55

66
export function prerenderClient () {
77
const layout = fs.readFileSync('./public/layout.html').toString()
88

9-
return (req, res, next) => {
9+
return async (req, res, next) => {
1010
if (req.method !== 'GET') { return next() }
1111

12-
const app = renderToStrings(db)
12+
const db = await loadDb()
13+
const { html, state } = renderToStrings(db)
1314
const content = layout
1415
.replace(
1516
'<div id="root"></div>',
16-
`<div id="root">${app.html}</div>`
17+
`<div id="root">${html}</div>`
1718
)
1819
.replace(
1920
'</head>',
20-
`<script>window.__INIT_STATE__=${app.state}</script></head>`
21+
`<script>window.__INIT_STATE__=${state}</script></head>`
2122
)
2223
res.writeHead(200, {
2324
'Content-Type': 'text/html',

server/webpack.config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ const config = {
2020
babelrc: false,
2121
presets: [require.resolve('babel-preset-react-app')]
2222
}
23-
},
24-
{ test: /\.json$/, loader: 'json-loader' }
23+
}
2524
]
2625
},
2726
plugins: [

0 commit comments

Comments
 (0)