Read head.json instead of importing
This commit is contained in:
parent
cc9ba52c6b
commit
4ce788e750
|
@ -2,6 +2,8 @@ import express from 'express'
|
||||||
import helmet from 'helmet'
|
import helmet from 'helmet'
|
||||||
import expressStaticGzip from 'express-static-gzip'
|
import expressStaticGzip from 'express-static-gzip'
|
||||||
import config from '../config/config.json'
|
import config from '../config/config.json'
|
||||||
|
import path from 'path'
|
||||||
|
import jsonfile from 'jsonfile'
|
||||||
import { ServerRenderer } from './utils/serverRender'
|
import { ServerRenderer } from './utils/serverRender'
|
||||||
import { Scanner } from './utils/scanner'
|
import { Scanner } from './utils/scanner'
|
||||||
|
|
||||||
|
@ -29,8 +31,15 @@ app.get('/favicon.ico', (req, res) => {
|
||||||
res.status(404).send('Not Found !!!')
|
res.status(404).send('Not Found !!!')
|
||||||
})
|
})
|
||||||
|
|
||||||
const serverRenderer = new ServerRenderer()
|
let head = jsonfile.readFileSync(path.join(process.cwd(), 'config/head.json'))
|
||||||
app.get('*', serverRenderer.render)
|
if (head == null) {
|
||||||
|
head = {
|
||||||
|
"scripts": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const serverRenderer = new ServerRenderer(head)
|
||||||
|
app.get('*', serverRenderer.render.bind(serverRenderer))
|
||||||
|
|
||||||
app.listen(port, function (error) {
|
app.listen(port, function (error) {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
|
|
@ -6,17 +6,18 @@ import routes from './routes'
|
||||||
import serialize from 'serialize-javascript'
|
import serialize from 'serialize-javascript'
|
||||||
import manifest from '../../public/static/manifest.json'
|
import manifest from '../../public/static/manifest.json'
|
||||||
import config from '../../config/config.json'
|
import config from '../../config/config.json'
|
||||||
import head from '../../config/head.json'
|
|
||||||
|
|
||||||
export class ServerRenderer {
|
export class ServerRenderer {
|
||||||
|
|
||||||
constructor () {
|
constructor (head) {
|
||||||
console.log('ServerRenderer')
|
this.head = head
|
||||||
}
|
}
|
||||||
|
|
||||||
render (req, res, next) {
|
render (req, res, next) {
|
||||||
|
|
||||||
const activeRoute = routes.find((route) => matchPath(req.url, route)) || {}
|
const activeRoute = routes.find((route) => matchPath(req.url, route)) || {}
|
||||||
|
const head = this.head
|
||||||
|
|
||||||
const promise = activeRoute.getData
|
const promise = activeRoute.getData
|
||||||
? activeRoute.getData(req.path)
|
? activeRoute.getData(req.path)
|
||||||
: Promise.resolve()
|
: Promise.resolve()
|
||||||
|
@ -28,12 +29,12 @@ export class ServerRenderer {
|
||||||
</Router>
|
</Router>
|
||||||
)
|
)
|
||||||
|
|
||||||
res.status(200).send(renderFullPage(markup, data))
|
res.status(200).send(renderFullPage(markup, head, data))
|
||||||
}).catch(next)
|
}).catch(next)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderFullPage (html, data) {
|
function renderFullPage (html, head, data) {
|
||||||
return `
|
return `
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
|
|
Loading…
Reference in New Issue