Skip to content

Commit

Permalink
Merge pull request #618 from City-of-Helsinki/feature/add-git-hash
Browse files Browse the repository at this point in the history
Solve #317
  • Loading branch information
dotanthanh authored Mar 19, 2018
2 parents 71fd9d9 + 2722b94 commit 292eb31
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
7 changes: 5 additions & 2 deletions server/Html.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types';

export default class Html extends React.Component {
render() {
const {head, bundleSrc, content, initialState, apiBaseUrl, uiConfig} = this.props;
const {head, bundleSrc, content, initialState, apiBaseUrl, uiConfig, hearingData} = this.props;
const initialStateHtml = `
window.STATE = ${JSON.stringify(initialState || {})};
window.API_BASE_URL = ${JSON.stringify(apiBaseUrl)};
Expand All @@ -19,6 +19,8 @@ export default class Html extends React.Component {
{head ? head.title.toComponent() : <title>Kerrokantasi</title>}
{head ? head.meta.toComponent() : null}
{head ? head.link.toComponent() : null}
<meta property="og:image" content={hearingData.main_image.url} />
<meta property="og:description" content={hearingData.abstract.fi} />
</head>
<body>
<div id="root" dangerouslySetInnerHTML={{ __html: content || "" }}/>
Expand All @@ -36,5 +38,6 @@ Html.propTypes = {
bundleSrc: PropTypes.string.isRequired,
content: PropTypes.string,
head: PropTypes.object,
initialState: PropTypes.object
initialState: PropTypes.object,
hearingData: PropTypes.object
};
44 changes: 36 additions & 8 deletions server/render-middleware.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
import React from 'react';
import {renderToStaticMarkup} from 'react-dom/server';
import Html from './Html';
import fetch from 'node-fetch';

const hearingDataCache = {};
function getHearingDataCached(settings, hearingSlug, ttl = 30 * 60 * 1000) {
const now = +new Date(); // current time as milliseconds
if (
hearingDataCache[hearingSlug] && // in cache
now < hearingDataCache[hearingSlug].ts + ttl // not expired yet
) {
return Promise.resolve(hearingDataCache[hearingSlug].data);
}
return fetch(
`${settings.kerrokantasi_api_base}/v1/hearing/${hearingSlug}`
)
.then(res => (res.status === 404 ? null : res.json())) // Not found? Okay, never mind, must've been a false positive
.catch(err => { console.log(err); }) // Other error? Meh, render w/o metatags then
.then(data => {
// Save in cache while we're here. (Note that 404s and errors are also cached.)
hearingDataCache[hearingSlug] = { data, ts: now };
return data;
});
}

function renderHTMLSkeleton(req, res, settings) {
const html = renderToStaticMarkup(
<Html
bundleSrc={settings.bundleSrc || '/app.js'}
apiBaseUrl={settings.kerrokantasi_api_base}
uiConfig={settings.uiConfig}
/>
);
res.status(200).send(html);
const hearingSlugMatch = req.path.split('/');
const hearingDataPromise = hearingSlugMatch
? getHearingDataCached(settings, hearingSlugMatch[1])
: Promise.resolve(null);
return hearingDataPromise.then(hearingData => {
const html = renderToStaticMarkup(
<Html
bundleSrc={settings.bundleSrc || '/app.js'}
apiBaseUrl={settings.kerrokantasi_api_base}
uiConfig={settings.uiConfig}
hearingData={hearingData}
/>
);
res.status(200).send(html);
});
}

export default function renderMiddleware(settings) {
Expand Down

0 comments on commit 292eb31

Please sign in to comment.