Skip to content

Commit 95bec03

Browse files
committed
Add index.html
1 parent a46c8f6 commit 95bec03

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>vue-ssr-starter-kit</title>
6+
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
7+
{{ STYLE }}
8+
</head>
9+
<body>
10+
{{ APP }}
11+
<script src="/dist/client-vendor-bundle.js"></script>
12+
<script src="/dist/client-bundle.js"></script>
13+
</body>
14+
</html>

server.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ const createBundleRenderer = require('vue-server-renderer').createBundleRenderer
1212

1313
const app = express()
1414

15+
// parse index.html template
16+
const html = (() => {
17+
const template = fs.readFileSync(resolve('./index.html'), 'utf-8')
18+
const i = template.indexOf('{{ APP }}')
19+
// styles are injected dynamically via vue-style-loader in development
20+
const style = isProd ? '<link rel="stylesheet" href="/dist/styles.css">' : ''
21+
return {
22+
head: template.slice(0, i).replace('{{ STYLE }}', style),
23+
tail: template.slice(i + '{{ APP }}'.length)
24+
}
25+
})()
26+
1527
let renderer
1628
if (isProd) {
1729
// create server renderer from real fs
@@ -36,42 +48,34 @@ app.use('/dist', express.static(resolve('./dist')))
3648
app.use(favicon(path.resolve(__dirname, 'src/assets/logo.png')))
3749

3850
app.get('*', (req, res) => {
51+
if (!renderer) {
52+
return res.end('waiting for compilation... refresh in a moment.')
53+
}
54+
3955
var s = Date.now()
4056
const context = { url: req.url }
4157
const renderStream = renderer.renderToStream(context)
4258
let firstChunk = true
4359

44-
res.write(`<!DOCTYPE html>
45-
<html lang="en">
46-
<head>
47-
<meta charset="utf-8">
48-
<title>vue-ssr-starter-kit</title>
49-
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
50-
${process.env.NODE_ENV === 'production'
51-
? `<link rel="stylesheet" href="/dist/styles.css">`
52-
: ``}
53-
</head>
54-
<body>`)
60+
res.write(html.head)
5561

5662
renderStream.on('data', chunk => {
5763
if (firstChunk) {
58-
// send down initial store state
64+
// embed initial store state
5965
if (context.initialState) {
60-
res.write(`<script>window.__INITIAL_STATE__=${
61-
serialize(context.initialState, { isJSON: true })
62-
}</script>`)
66+
res.write(
67+
`<script>window.__INITIAL_STATE__=${
68+
serialize(context.initialState, { isJSON: true })
69+
}</script>`
70+
)
6371
}
6472
firstChunk = false
6573
}
6674
res.write(chunk)
6775
})
6876

6977
renderStream.on('end', () => {
70-
res.end(`
71-
<script src="/dist/client-vendor-bundle.js"></script>
72-
<script src="/dist/client-bundle.js"></script>
73-
</body></html>`
74-
)
78+
res.end(html.tail)
7579
console.log(`whole request: ${Date.now() - s}ms`)
7680
})
7781

0 commit comments

Comments
 (0)