Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve #317 #618

Merged
merged 2 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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