Skip to content
This repository has been archived by the owner on Jan 2, 2018. It is now read-only.

Commit

Permalink
Merge pull request #55 from rocjs/feature/more-data-in-templates
Browse files Browse the repository at this point in the history
Added new data to the template that can be used when rendering
  • Loading branch information
andreasrs authored Jan 5, 2017
2 parents c731a3c + fe9a5f2 commit d9e75f0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ export function initRenderPage({ script, css }, distMode, devMode, Header) {
const bundleName = script[0];
const styleName = css[0];

return (
head,
return ({
content = '',
fluxState = {},
customTemplateValues = {},
error,
head,
redialProps = [],
customTemplateValues = {}
) => {
reduxState = {},
request,
status,
}) => {
const { dev, build, ...rest } = rocConfig; // eslint-disable-line

const rocConfigClient = distMode ? rest : { ...rest, dev };
Expand All @@ -55,16 +58,19 @@ export function initRenderPage({ script, css }, distMode, devMode, Header) {
}

return nunjucks.render(rocConfig.runtime.template.name, {
head,
content,
fluxState: serialize(fluxState),
bundleName,
styleName,
content,
custom: customTemplateValues,
dist: distMode,
serializedRocConfig: serialize(rocConfigClient),
serializedAppConfig: serialize(appConfig),
error,
fluxState: serialize(reduxState),
head,
redialProps: serialize(redialProps),
custom: customTemplateValues,
request,
serializedAppConfig: serialize(appConfig),
serializedRocConfig: serialize(rocConfigClient),
status,
styleName,
});
};
}
Expand All @@ -76,6 +82,7 @@ export function reactRender({
createRoutes,
renderPage,
koaState,
request,
staticRender = false,
hasTemplateValues,
templateValues,
Expand All @@ -102,13 +109,13 @@ export function reactRender({
log('Router error', pretty.render(error));
return resolve({
status: 500,
body: renderPage(),
body: renderPage({ error, request, status: 500 }),
});
} else if (!renderProps) {
log('No renderProps, most likely the path does not exist');
return resolve({
status: 404,
body: renderPage(),
body: renderPage({ request, status: 404 }),
});
}

Expand Down Expand Up @@ -162,32 +169,41 @@ export function reactRender({
);
}

const page = staticRender ? renderToStaticMarkup(component) : renderToString(component);
const content = staticRender ? renderToStaticMarkup(component) : renderToString(component);
const head = Helmet.rewind();
const state = store ? store.getState() : {};
const reduxState = store ? store.getState() : {};
const status = ServerStatus.rewind() || 200;

let computedTemplateValues;
if (hasTemplateValues) {
// Provides settings, Redux state and Koa state
computedTemplateValues = templateValues.default({
koaState,
settings: rocConfig,
reduxState: state,
reduxState,
});
}

return resolve({
body: renderPage(head, page, state, redialProps, computedTemplateValues),
status: ServerStatus.rewind() || 200,
body: renderPage({
computedTemplateValues,
content,
head,
redialProps,
reduxState,
request,
status,
}),
status,
});
})
.catch((err) => {
if (err) {
log('Fetching error', pretty.render(err));
log('General error', pretty.render(err));
}
return resolve({
status: 500,
body: renderPage(),
body: renderPage({ error: err, request, status: 500 }),
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export default function reactRouter({
createRoutes,
renderPage,
koaState: this.state,
request: this.request,
hasTemplateValues,
templateValues,
reduxSagas,
Expand Down

0 comments on commit d9e75f0

Please sign in to comment.