Skip to content

Commit

Permalink
move the triggerHooks function
Browse files Browse the repository at this point in the history
  • Loading branch information
bertho-zero committed Apr 18, 2019
1 parent 4668d24 commit 3bf1031
Showing 1 changed file with 28 additions and 32 deletions.
60 changes: 28 additions & 32 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,44 +76,40 @@ initSocket();
persistConfig
});

const hydrate = _routes => {
const triggerHooks = async (pathname, indicator = true) => {
if (indicator) {
NProgress.start();
}

const { components, match, params } = await asyncMatchRoutes(_routes, pathname);
const triggerLocals = {
...providers,
store,
match,
params,
history,
location: history.location
};

await trigger('inject', components, triggerLocals);

// Don't fetch data for initial route, server has already done the work:
if (window.__PRELOADED__) {
// Delete initial data so that subsequent data fetches can occur:
delete window.__PRELOADED__;
} else {
// Fetch mandatory data dependencies for 2nd route change onwards:
await trigger('fetch', components, triggerLocals);
}
await trigger('defer', components, triggerLocals);

if (indicator) {
NProgress.done();
}
const triggerHooks = async (_routes, pathname) => {
NProgress.start();

const { components, match, params } = await asyncMatchRoutes(_routes, pathname);
const triggerLocals = {
...providers,
store,
match,
params,
history,
location: history.location
};

await trigger('inject', components, triggerLocals);

// Don't fetch data for initial route, server has already done the work:
if (window.__PRELOADED__) {
// Delete initial data so that subsequent data fetches can occur:
delete window.__PRELOADED__;
} else {
// Fetch mandatory data dependencies for 2nd route change onwards:
await trigger('fetch', components, triggerLocals);
}
await trigger('defer', components, triggerLocals);

NProgress.done();
};

const hydrate = _routes => {
const element = (
<HotEnabler>
<Provider store={store} {...providers}>
<Router history={history}>
<RouterTrigger trigger={triggerHooks}>{renderRoutes(_routes)}</RouterTrigger>
<RouterTrigger trigger={pathname => triggerHooks(_routes, pathname)}>{renderRoutes(_routes)}</RouterTrigger>
</Router>
</Provider>
</HotEnabler>
Expand Down

0 comments on commit 3bf1031

Please sign in to comment.