Skip to content

Commit

Permalink
Merge pull request #8 from SathishGovindaraju/master
Browse files Browse the repository at this point in the history
Added preRender callback
  • Loading branch information
afenton90 authored Nov 23, 2017
2 parents a786b01 + b1447d9 commit 2a301eb
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 13 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ For example:
// Other callbacks
}));
```
### `preRender`

Callback function called before rendering `App`.
This callback can either be a normal function which returns a valid component or it can return a `Promise` which then resolves and returns a valid component.
The function accepts the following parameters:

* `component` - The `StaticRouter` wrapped around the `App`.

> This callback could be used to wrap the `component` with any other higher-order component before it gets rendered
### `onError`

Expand Down
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ const server = ({
App,
onError,
onRedirect,
onRender
onRender,
preRender = router => router
}) =>
async (ctx, next) => {
try {
const location = ctx.request.url;
const routerContext = {};

const view = renderToString(
const router = (
<StaticRouter
location={location}
context={routerContext}
Expand All @@ -22,6 +23,12 @@ const server = ({
</StaticRouter>
);

const updatedComponent = await preRender(router);

if (!updatedComponent) throw new Error('No component returned from preRender');

const view = renderToString(updatedComponent);

ctx.state = {
...ctx.state,
routerContext
Expand Down
46 changes: 45 additions & 1 deletion index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sinon from 'sinon';
import React from 'react';
import { Route, Redirect } from 'react-router';
import { Route, Redirect, StaticRouter } from 'react-router';
import koaReactRouter from './index';

const Container = ({ children }) =>
Expand Down Expand Up @@ -287,3 +287,47 @@ test('sets doctype in response body', async () => {
expect(next.calledOnce).toBe(true);
});

test('calls preRender with StaticRouter as argument', async () => {
const preRender = sinon.sandbox.spy();
const callbacks = mockCallbacks();
callbacks.preRender = preRender;

const ctx = {
request: { url: '/away' },
response: {}
};
const next = sinon.spy();

await koaReactRouter({
App,
...callbacks
})(ctx, next);

const expected = (
<StaticRouter location={ctx.request.url} context={{}}>
<App />
</StaticRouter>
);
expect(preRender.calledOnce).toBe(true);
expect(JSON.stringify(preRender.args[0][0])).toBe(JSON.stringify(expected));
});

test('should throw an error if preRender callback doesn\'t return component', async () => {
const preRender = () => {};
const callbacks = mockCallbacks();
callbacks.preRender = preRender;

const ctx = {
request: { url: '/away' },
response: {}
};
const next = sinon.spy();

await koaReactRouter({
App,
...callbacks
})(ctx, next);

expect(callbacks.onError.calledOnce).toBe(true);
expect(callbacks.onError.args[0][1].message).toEqual('No component returned from preRender');
});
27 changes: 17 additions & 10 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2a301eb

Please sign in to comment.