Skip to content

Commit 589d488

Browse files
committed
fix: fix preview redirect on index, add useRouter hooks
1 parent 891101a commit 589d488

File tree

12 files changed

+74
-77
lines changed

12 files changed

+74
-77
lines changed

packages/smooth/src/client/Routes.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import React from 'react'
2-
import { Switch, Route } from 'react-router-dom'
2+
import { Switch, Route } from '../router'
33
import Page, { getPages } from '../page/Page'
4-
import HiddenRouter from '../router/HiddenRouter'
5-
import { Provider as HiddenHistoryContextProvider } from '../router/HiddenHistoryContext'
4+
import { HiddenRouter } from '../router/HiddenRouter'
5+
import { HiddenHistoryProvider } from '../router/HiddenHistory'
66

77
const pages = getPages()
88

99
export default function Routes() {
1010
return (
11-
<HiddenHistoryContextProvider>
11+
<HiddenHistoryProvider>
1212
<Route
1313
path="/:lang(.{2})?"
1414
render={({
@@ -45,6 +45,6 @@ export default function Routes() {
4545
)
4646
}}
4747
/>
48-
</HiddenHistoryContextProvider>
48+
</HiddenHistoryProvider>
4949
)
5050
}

packages/smooth/src/client/main-web.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import 'regenerator-runtime/runtime'
33

44
import React from 'react'
55
import ReactDOM from 'react-dom'
6-
import { BrowserRouter } from 'react-router-dom'
76
import { ApolloProvider } from 'react-apollo'
87
import { loadableReady } from '@loadable/component'
98
import { createApolloClient } from './apollo'
109
import Root from './Root'
1110
import ErrorContext from './ErrorContext'
12-
import RouterHooks from '../router/RouterHooks'
11+
import { BrowserRouter } from '../router'
12+
import { RouterHooks } from '../router/RouterHooks'
1313

1414
loadableReady(() => {
1515
ReactDOM.hydrate(

packages/smooth/src/content/Query.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import camelcase from 'camelcase'
33
import gql from 'graphql-tag'
4-
import { Redirect } from 'react-router-dom'
4+
import { Redirect, useRouter } from '../router'
55
import { usePageContext } from '../page/PageContext'
66
import { HTTPError } from '../router/HTTPError'
77
import { applyHook } from '../plugin/browser'
@@ -57,9 +57,10 @@ function Handler({ children, ...props }) {
5757

5858
export function Query({ children }) {
5959
const { page } = usePageContext()
60+
const { location } = useRouter()
6061

6162
if (page.slug === 'index') {
62-
return <Redirect to={page.indexUrl} />
63+
return <Redirect to={`${page.indexUrl}${location.search}`} />
6364
}
6465

6566
return (

packages/smooth/src/router/HiddenHistoryContext.js renamed to packages/smooth/src/router/HiddenHistory.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import React, { useMemo, useCallback, createContext, useState } from 'react'
1+
import React, {
2+
useContext,
3+
useMemo,
4+
useCallback,
5+
createContext,
6+
useState,
7+
useRef,
8+
} from 'react'
29
import { createLocation } from 'history'
310

411
const HiddenHistoryContext = createContext()
@@ -10,7 +17,7 @@ function createHiddenLocation(state) {
1017
return location
1118
}
1219

13-
export function Provider({ children }) {
20+
export function HiddenHistoryProvider({ children }) {
1421
const [state, setState] = useState(null)
1522
const reset = useCallback(() => setState(null), [])
1623
const push = useCallback(
@@ -39,4 +46,19 @@ export function Provider({ children }) {
3946
)
4047
}
4148

42-
export default HiddenHistoryContext
49+
export function useHiddenHistory() {
50+
return useContext(HiddenHistoryContext)
51+
}
52+
53+
// Create a local history, a mix with original history & hiddenHistory
54+
export function useMixedHiddenHistory(originalHistory = {}) {
55+
const hiddenHistory = useHiddenHistory()
56+
const mixedHistory = useRef({})
57+
Object.assign(mixedHistory.current, originalHistory)
58+
mixedHistory.current.push = hiddenHistory.push
59+
mixedHistory.current.replace = hiddenHistory.replace
60+
if (hiddenHistory.location) {
61+
mixedHistory.current.location = hiddenHistory.location
62+
}
63+
return mixedHistory.current
64+
}
Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
1-
import React, { useContext, useRef } from 'react'
2-
import { withRouter, Router } from 'react-router-dom'
3-
import HiddenHistoryContext from './HiddenHistoryContext'
1+
import React from 'react'
2+
import { Router, useRouter } from './Router'
3+
import { useMixedHiddenHistory } from './HiddenHistory'
44

5-
// Create a local history, a mix with original history & hiddenHistory
6-
function useHiddenHistory(originalHistory) {
7-
const hiddenHistory = useContext(HiddenHistoryContext)
8-
const mixedHistory = useRef({})
9-
Object.assign(mixedHistory.current, originalHistory)
10-
mixedHistory.current.push = hiddenHistory.push
11-
mixedHistory.current.replace = hiddenHistory.replace
12-
if (hiddenHistory.location) {
13-
mixedHistory.current.location = hiddenHistory.location
14-
}
15-
return mixedHistory.current
16-
}
17-
18-
function HiddenLinkRouter({ history, children }) {
19-
const hiddenHistory = useHiddenHistory(history)
5+
export function HiddenLinkRouter({ children }) {
6+
const { history } = useRouter()
7+
const hiddenHistory = useMixedHiddenHistory(history)
208
return <Router history={hiddenHistory}>{children}</Router>
219
}
22-
23-
export default withRouter(HiddenLinkRouter)

packages/smooth/src/router/HiddenRouter.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import React, {
77
useEffect,
88
} from 'react'
99
import { createPath } from 'history'
10-
import { Router, withRouter } from 'react-router-dom'
11-
import HiddenHistoryContext from './HiddenHistoryContext'
10+
import { Router, useRouter } from './Router'
11+
import { useHiddenHistory } from './HiddenHistory'
1212

13-
export const HiddenRouterContext = createContext()
13+
const HiddenRouterContext = createContext()
1414

1515
export function useHiddenRouter() {
1616
return useContext(HiddenRouterContext)
@@ -38,8 +38,9 @@ function createURL(location) {
3838
return typeof location === 'string' ? location : createPath(location)
3939
}
4040

41-
function HiddenRouter({ history, children }) {
42-
const hiddenHistory = useContext(HiddenHistoryContext)
41+
export function HiddenRouter({ children }) {
42+
const { history } = useRouter()
43+
const hiddenHistory = useHiddenHistory()
4344
const promises = useRef([])
4445

4546
const flush = useCallback(() => {
@@ -93,5 +94,3 @@ function HiddenRouter({ history, children }) {
9394
</div>
9495
) : null
9596
}
96-
97-
export default withRouter(HiddenRouter)

packages/smooth/src/router/Link.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import { Link as BaseLink, NavLink as BaseNavLink } from 'react-router-dom'
33
import { useLang } from '../page/PageContext'
4-
import HiddenLinkRouter from './HiddenLinkRouter'
4+
import { HiddenLinkRouter } from './HiddenLinkRouter'
55

66
function computeTo(to, lang) {
77
if (lang) {

packages/smooth/src/router/Router.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { useContext } from 'react'
2+
import { __RouterContext } from 'react-router-dom'
3+
4+
export {
5+
Router,
6+
BrowserRouter,
7+
StaticRouter,
8+
Route,
9+
Redirect,
10+
Prompt,
11+
Switch,
12+
generatePath,
13+
matchPath,
14+
withRouter,
15+
} from 'react-router-dom'
16+
17+
export function useRouter() {
18+
return useContext(__RouterContext)
19+
}

packages/smooth/src/router/RouterHooks.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useEffect, useRef } from 'react'
2-
import { withRouter } from 'react-router-dom'
2+
import { useRouter } from './Router'
33
import { applyHook } from '../plugin/browser'
44

55
function usePreviousLocation(location) {
@@ -19,17 +19,16 @@ function useScrollTop({ location, prevLocation }) {
1919
}, [location, prevLocation])
2020
}
2121

22-
function RouterHooks({ location }) {
22+
function RouterHooksInternal({ location }) {
2323
const prevLocation = usePreviousLocation(location)
2424
const options = { location, prevLocation }
2525
useScrollTop(options)
2626
applyHook('onRouteUpdate', options)
2727
return null
2828
}
2929

30-
function RouterHooksGuard({ location }) {
30+
export function RouterHooks() {
31+
const { location } = useRouter()
3132
if (location.hidden) return null
32-
return <RouterHooks location={location} />
33+
return <RouterHooksInternal location={location} />
3334
}
34-
35-
export default withRouter(RouterHooksGuard)

packages/smooth/src/router/ScrollToTop.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)