Skip to content

Commit c5f5c3f

Browse files
committed
fix: various fixes around waitBeforeTransition
1 parent 046e2ea commit c5f5c3f

File tree

4 files changed

+43
-32
lines changed

4 files changed

+43
-32
lines changed

packages/smooth-core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"graphql-iso-date": "^3.6.1",
6666
"graphql-tag": "^2.10.1",
6767
"graphql-tools": "^4.0.4",
68+
"history": "^4.7.2",
6869
"humanize-string": "^1.0.2",
6970
"merge-deep": "^3.0.2",
7071
"pluralize": "^7.0.0",

packages/smooth-core/src/client/Routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function Routes() {
2222
{pages.map((page, index) => (
2323
<Route
2424
key={index}
25-
path={`${url}${page.routePath}`}
25+
path={`${url}${page.routePath}`.replace(/^\/\//, '/')}
2626
render={({ history, match, location }) => (
2727
<Page
2828
lang={lang}
Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1-
import React, { useContext, useMemo } from 'react'
1+
import React, { useContext, useRef } from 'react'
22
import { withRouter, Router } from 'react-router-dom'
3+
import { createLocation } from 'history'
34
import HiddenHistoryContext from './HiddenHistoryContext'
45

5-
function HiddenLinkRouter({ history, children }) {
6+
// Create a local history, a mix with original history & hiddenHistory
7+
function useHiddenHistory(originalHistory) {
68
const hiddenHistory = useContext(HiddenHistoryContext)
7-
const newHistory = useMemo(() => ({ ...history }), [history])
8-
newHistory.push = hiddenHistory.push
9-
return <Router history={newHistory}>{children}</Router>
9+
const mixedHistory = useRef({})
10+
Object.assign(mixedHistory.current, originalHistory)
11+
mixedHistory.current.push = hiddenHistory.push
12+
mixedHistory.current.replace = hiddenHistory.replace
13+
if (hiddenHistory.location) {
14+
mixedHistory.current.location = createLocation(hiddenHistory.location)
15+
}
16+
return mixedHistory.current
17+
}
18+
19+
function HiddenLinkRouter({ history, children }) {
20+
const hiddenHistory = useHiddenHistory(history)
21+
return <Router history={hiddenHistory}>{children}</Router>
1022
}
1123

1224
export default withRouter(HiddenLinkRouter)

packages/smooth-core/src/router/HiddenRouter.js

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,32 @@ export const HiddenRouterContext = createContext()
66

77
function HiddenRouter({ history, children }) {
88
const hiddenHistory = useContext(HiddenHistoryContext)
9-
return (
10-
<div style={{ display: 'none' }}>
11-
{hiddenHistory.location && (
12-
<HiddenRouterContext.Provider
13-
value={{
14-
onPrefetched: () => {
15-
switch (hiddenHistory.action) {
16-
case 'PUSH':
17-
history.push(hiddenHistory.location)
18-
break
19-
case 'REPLACE':
20-
history.replace(hiddenHistory.location)
21-
break
22-
default:
23-
return
24-
}
9+
return hiddenHistory.location ? (
10+
<div style={{ display: 'none' }} hidden>
11+
<HiddenRouterContext.Provider
12+
value={{
13+
onPrefetched: () => {
14+
switch (hiddenHistory.action) {
15+
case 'PUSH':
16+
history.push(hiddenHistory.location)
17+
break
18+
case 'REPLACE':
19+
history.replace(hiddenHistory.location)
20+
break
21+
default:
22+
return
23+
}
2524

26-
hiddenHistory.push(null)
27-
},
28-
}}
29-
>
30-
<StaticRouter context={{}} location={hiddenHistory.location}>
31-
{children}
32-
</StaticRouter>
33-
</HiddenRouterContext.Provider>
34-
)}
25+
hiddenHistory.push(null)
26+
},
27+
}}
28+
>
29+
<StaticRouter context={{}} location={hiddenHistory.location}>
30+
{children}
31+
</StaticRouter>
32+
</HiddenRouterContext.Provider>
3533
</div>
36-
)
34+
) : null
3735
}
3836

3937
export default withRouter(HiddenRouter)

0 commit comments

Comments
 (0)