Skip to content

Commit 60a67ba

Browse files
committed
feat: give access to language in error
1 parent 272a595 commit 60a67ba

File tree

7 files changed

+67
-57
lines changed

7 files changed

+67
-57
lines changed

packages/smooth/src/client/ErrorBoundary.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default class ErrorBoundary extends React.Component {
1717
if (error) {
1818
return (
1919
<Status code={error.statusCode || 500}>
20-
<ErrorPage error={error} />
20+
<ErrorPage lang={this.props.lang || null} error={error} />
2121
</Status>
2222
)
2323
}
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
import React from 'react'
22

3-
export default React.createContext({ error: null })
3+
const ErrorContext = React.createContext({ error: null })
4+
5+
export function ErrorContextProvider({ error, children }) {
6+
const value = React.useMemo(() => ({ error }), [error])
7+
return <ErrorContext.Provider value={value}>{children}</ErrorContext.Provider>
8+
}
9+
10+
export function useError() {
11+
const { error } = React.useContext(ErrorContext)
12+
return error
13+
}

packages/smooth/src/client/Root.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
import React from 'react'
2+
import { Route } from '../router'
23
import Routes from './Routes'
34
import ErrorBoundary from './ErrorBoundary'
4-
import ErrorContext from './ErrorContext'
5+
import { useError } from './ErrorContext'
6+
import { HiddenHistoryProvider } from '../router/HiddenHistory'
57

68
export default function Root() {
9+
const error = useError()
710
return (
8-
<ErrorContext.Consumer>
9-
{({ error }) => (
10-
<ErrorBoundary error={error}>
11-
<Routes />
12-
</ErrorBoundary>
13-
)}
14-
</ErrorContext.Consumer>
11+
<HiddenHistoryProvider>
12+
<Route
13+
path="/:lang(.{2})?"
14+
render={routeProps => (
15+
<ErrorBoundary lang={routeProps.match.params.lang} error={error}>
16+
<Routes {...routeProps} />
17+
</ErrorBoundary>
18+
)}
19+
/>
20+
</HiddenHistoryProvider>
1521
)
1622
}

packages/smooth/src/client/Routes.js

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,39 @@ import React from 'react'
22
import { Switch, Route } from '../router'
33
import Page, { getPages } from '../page/Page'
44
import { HiddenRouter } from '../router/HiddenRouter'
5-
import { HiddenHistoryProvider } from '../router/HiddenHistory'
65

76
const pages = getPages()
87

9-
export default function Routes() {
8+
export default function Routes({
9+
match: {
10+
url,
11+
params: { lang = null },
12+
},
13+
}) {
14+
const routes = (
15+
<Switch>
16+
{pages.map((page, index) => (
17+
<Route
18+
key={index}
19+
path={`${url}${page.routePath}`.replace(/\/\//, '/')}
20+
render={({ history, match, location }) => (
21+
<Page
22+
lang={lang}
23+
indexUrl={`${url}${page.indexPath}`}
24+
page={page}
25+
history={history}
26+
match={match}
27+
location={location}
28+
/>
29+
)}
30+
/>
31+
))}
32+
</Switch>
33+
)
1034
return (
11-
<HiddenHistoryProvider>
12-
<Route
13-
path="/:lang(.{2})?"
14-
render={({
15-
match: {
16-
url,
17-
params: { lang = null },
18-
},
19-
}) => {
20-
const routes = (
21-
<Switch>
22-
{pages.map((page, index) => (
23-
<Route
24-
key={index}
25-
path={`${url}${page.routePath}`.replace(/\/\//, '/')}
26-
render={({ history, match, location }) => (
27-
<Page
28-
lang={lang}
29-
indexUrl={`${url}${page.indexPath}`}
30-
page={page}
31-
history={history}
32-
match={match}
33-
location={location}
34-
/>
35-
)}
36-
/>
37-
))}
38-
</Switch>
39-
)
40-
return (
41-
<>
42-
{routes}
43-
<HiddenRouter>{routes}</HiddenRouter>
44-
</>
45-
)
46-
}}
47-
/>
48-
</HiddenHistoryProvider>
35+
<>
36+
{routes}
37+
<HiddenRouter>{routes}</HiddenRouter>
38+
</>
4939
)
5040
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import 'core-js'
22

33
export { default as Html } from '__smooth_html'
44
export { default as Root } from './Root'
5-
export { default as ErrorContext } from './ErrorContext'
5+
export { ErrorContextProvider } from './ErrorContext'

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import { ApolloProvider } from 'react-apollo'
77
import { loadableReady } from '@loadable/component'
88
import { createApolloClient } from './apollo'
99
import Root from './Root'
10-
import ErrorContext from './ErrorContext'
10+
import { ErrorContextProvider } from './ErrorContext'
1111
import { BrowserRouter } from '../router'
1212
import { RouterHooks } from '../router/RouterHooks'
1313

1414
loadableReady(() => {
1515
ReactDOM.hydrate(
16-
<ErrorContext.Provider value={{ error: window.__SMOOTH_ERROR__ }}>
16+
<ErrorContextProvider error={window.__SMOOTH_ERROR__}>
1717
<ApolloProvider client={createApolloClient()}>
1818
<BrowserRouter>
1919
<>
@@ -22,7 +22,7 @@ loadableReady(() => {
2222
</>
2323
</BrowserRouter>
2424
</ApolloProvider>
25-
</ErrorContext.Provider>,
25+
</ErrorContextProvider>,
2626
document.getElementById('___smooth'),
2727
)
2828
})

packages/smooth/src/server/ssr.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ export default function ssrMiddleware({
3232
outputPath: path.join(config.cachePath, 'node/static'),
3333
})
3434

35-
const { Root, Html, ErrorContext } = nodeExtractor.requireEntrypoint()
35+
const {
36+
Root,
37+
Html,
38+
ErrorContextProvider,
39+
} = nodeExtractor.requireEntrypoint()
3640

3741
const webExtractor = new ChunkExtractor({ statsFile: webStats })
3842
const routerContext = {}
@@ -44,13 +48,13 @@ export default function ssrMiddleware({
4448
})
4549

4650
let jsx = (
47-
<ErrorContext.Provider value={{ error }}>
51+
<ErrorContextProvider error={error}>
4852
<ApolloProvider client={apolloClient}>
4953
<StaticRouter location={req.url} context={routerContext}>
5054
<Root error={error} />
5155
</StaticRouter>
5256
</ApolloProvider>
53-
</ErrorContext.Provider>
57+
</ErrorContextProvider>
5458
)
5559

5660
const rootElement = wrapRootElement(config)({

0 commit comments

Comments
 (0)