Skip to content

Commit 3492cac

Browse files
toerndevhedgepigdaniel
authored andcommitted
refactor: 💡 Add options to the "call" middleware (#17)
"call" now needs the new options "runOnServer", "runOnHydrate" to be passed explicitly, rather than using hard-coded logic to determine where to run. "runOnClientRender" has not been added since there's currently no use-case for skipping client render. This aims to clarify when each callback runs.
1 parent dc2c86c commit 3492cac

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

packages/rudy/src/core/createRouter.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @flow
22
import qs from 'qs'
3+
import { createSelector } from '@respond-framework/utils'
34
import type {
45
Options,
56
Store,
@@ -22,8 +23,6 @@ import {
2223
onError as defaultOnError,
2324
} from '../utils'
2425

25-
import { createSelector } from '@respond-framework/utils'
26-
2726
import {
2827
serverRedirect,
2928
pathlessRoute,
@@ -42,14 +41,16 @@ export default (
4241
anonymousThunk,
4342
pathlessRoute('thunk'),
4443
transformAction, // pipeline starts here
44+
// Hydrate: skip callbacks called on server to produce initialState (beforeEnter, thunk, etc)
45+
// Server: don't allow client-centric callbacks (onEnter, onLeave, beforeLeave)
4546
call('beforeLeave', { prev: true }),
46-
call('beforeEnter'),
47+
call('beforeEnter', { runOnServer: true }),
4748
enter,
4849
changePageTitle(),
4950
call('onLeave', { prev: true }),
50-
call('onEnter'),
51-
call('thunk', { cache: true }),
52-
call('onComplete'),
51+
call('onEnter', { runOnHydrate: true }),
52+
call('thunk', { cache: true, runOnServer: true }),
53+
call('onComplete', { runOnServer: true }),
5354
],
5455
) => {
5556
const {
@@ -82,7 +83,9 @@ export default (
8283
const has = (name: string) => wares[name]
8384
const ctx = { busy: false }
8485
const api = { routes, history, options, register, has, ctx }
85-
const onError = call('onError')(api)
86+
const onError = call('onError', { runOnServer: true, runOnHydrate: true })(
87+
api,
88+
)
8689
const nextPromise = options.compose(
8790
middlewares,
8891
api,

packages/rudy/src/middleware/call/utils/shouldCall.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
// @flow
2-
import type { LocationState } from '../../../flow-types'
32
import { isServer } from '@respond-framework/utils'
3+
import type { LocationState } from '../../../flow-types'
44
import { isHydrate } from '../../../utils'
55

6-
export default (name, route, req) => {
6+
export default (name, route, req, { runOnServer, runOnHydrate }) => {
77
if (!route[name] && !req.options[name]) return false
88

9-
// skip callbacks (beforeEnter, thunk, etc) called on server, which produced initialState
10-
if (isHydrate(req) && !/onEnter|onError/.test(name)) return false
9+
if (isHydrate(req) && !runOnHydrate) return false
1110

12-
// dont allow these client-centric callbacks on the server
13-
if (isServer() && /onEnter|Leave/.test(name)) return false
11+
if (isServer() && !runOnServer) return false
1412

1513
return allowBoth
1614
}

packages/rudy/src/middleware/pathlessRoute.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ export default (...names) => (api) => {
44
names[0] = names[0] || 'thunk'
55
names[1] = names[1] || 'onComplete'
66

7-
const middlewares = names.map((name) => call(name, { skipOpts: true }))
7+
const middlewares = names.map((name) =>
8+
call(name, { runOnServer: true, skipOpts: true }),
9+
)
810

911
const pipeline = api.options.compose(
1012
middlewares,

0 commit comments

Comments
 (0)