diff --git a/src/containers/MainContainer.js b/src/containers/MainContainer.js index 1ab60af..a531cb9 100644 --- a/src/containers/MainContainer.js +++ b/src/containers/MainContainer.js @@ -16,16 +16,16 @@ export default class MainContainer extends Component { data = window.__INITIAL_DATA__ delete window.__INITIAL_DATA__ } else { - data = props.staticContext.data + data = props.staticContext.context } - + this.state = { isLoadingBlog: !data[0].posts, isLoadingAbout: !data[0].other.about, about: data[0].other.about, posts: data[0].posts, config: data[1] - } + } } render () { diff --git a/src/containers/PostContainer.js b/src/containers/PostContainer.js index 7eb3e5f..c34b034 100644 --- a/src/containers/PostContainer.js +++ b/src/containers/PostContainer.js @@ -16,7 +16,7 @@ export default class PostContainer extends Component { data = window.__INITIAL_DATA__ delete window.__INITIAL_DATA__ } else { - data = props.staticContext.data + data = props.staticContext.context } this.state = { diff --git a/src/server.js b/src/server.js index 6e87ef9..089c14d 100644 --- a/src/server.js +++ b/src/server.js @@ -1,7 +1,6 @@ import express from 'express' import helmet from 'helmet' import expressStaticGzip from 'express-static-gzip' -import config from '../config/config.json' import path from 'path' import jsonfile from 'jsonfile' import { ServerRenderer } from './utils/serverRender' @@ -10,7 +9,12 @@ import { Scanner } from './utils/scanner' const port = process.env.PORT || 3000 const app = express() -const scanner = new Scanner() +let config = jsonfile.readFileSync(path.join(process.cwd(), 'config/config.json')) +if (config == null) { + throw new Error('Config file not found!') +} + +const scanner = new Scanner(config) scanner.scan() app.use(helmet.contentSecurityPolicy({ @@ -38,7 +42,7 @@ if (head == null) { } } -const serverRenderer = new ServerRenderer(head) +const serverRenderer = new ServerRenderer(head, config) app.get('*', serverRenderer.render.bind(serverRenderer)) app.listen(port, function (error) { diff --git a/src/utils/api.js b/src/utils/api.js index 3502c6f..e176c95 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -4,16 +4,10 @@ import path from 'path' export function getData (reqPath = '') { if (reqPath === '') { - return Promise.all([ - readJson(path.join(process.cwd(), 'data.json')), - readJson(path.join(process.cwd(), 'config/config.json')) - ]) + return readJson(path.join(process.cwd(), 'data.json')) } else { const fileName = path.join(process.cwd(), 'content', reqPath + '.md') - return Promise.all([ - readFile(fileName, 'utf8'), - readJson(path.join(process.cwd(), 'config/config.json')) - ]) + return readFile(fileName, 'utf8') } }; @@ -31,4 +25,4 @@ function readJson (dataPath) { err ? reject(err) : resolve(data) }) }) -} +} \ No newline at end of file diff --git a/src/utils/scanner.js b/src/utils/scanner.js index c1b6965..7a64ed4 100644 --- a/src/utils/scanner.js +++ b/src/utils/scanner.js @@ -1,17 +1,17 @@ import fs from 'fs' import path from 'path' -import config from '../../config/config.json' import fm from 'front-matter' import moment from 'moment' import jsonfile from 'jsonfile' import zlib from 'zlib' export class Scanner { - constructor () { + constructor (config) { this.data = { 'posts': [], 'other': {} } + this.config = config } readdir (dirname) { @@ -76,7 +76,7 @@ export class Scanner { const filePath = path.join(process.cwd(), 'content', file) const metadata = this.fileMetadata(filePath) - if (config['non-content-files'].indexOf(file) === -1) { + if (this.config['non-content-files'].indexOf(file) === -1) { const frontMatter = fm(data) if (frontMatter.attributes.draft) { diff --git a/src/utils/serverRender.js b/src/utils/serverRender.js index cb757f5..3f8f7c5 100644 --- a/src/utils/serverRender.js +++ b/src/utils/serverRender.js @@ -8,38 +8,41 @@ import manifest from '../../public/static/manifest.json' export class ServerRenderer { - constructor (head) { - this.head = head + constructor (head, config) { + this.head = head + this.config = config } render (req, res, next) { const activeRoute = routes.find((route) => matchPath(req.url, route)) || {} const head = this.head + const config = this.config const promise = activeRoute.getData ? activeRoute.getData(req.path) : Promise.resolve() promise.then((data) => { - console.log(data) + const context = [data, config] const markup = renderToString( - + ) - res.status(200).send(renderFullPage(markup, head, data)) + res.status(200).send(renderFullPage(markup, head, data, config)) }).catch(next) } } -function renderFullPage (html, head, data) { +function renderFullPage (html, head, data, config) { + const initialData = [data, config] return ` - ${data[1].title} + ${config.title} @@ -47,7 +50,7 @@ function renderFullPage (html, head, data) { - + ${head.scripts.join('\n')}