UNPKG

3.9 kBJavaScriptView Raw
1// import history from "__KOOT_CLIENT_REQUIRE_HISTORY__"
2import createHistory from "__KOOT_CLIENT_REQUIRE_CREATE_HISTORY__"
3import { parsePath } from 'history/lib/PathUtils'
4import isI18nEnabled from '../i18n/is-enabled'
5
6/**
7 * History Enhancer: use basename
8 *
9 * Original useBasename enhancer from history also override all read methods
10 * `getCurrentLocation` `listenBefore` `listen`
11 * But as Diablohu tested, when read methods overrided, if the route matched used async method to get component, would fail
12 * that rendering blank page and no route match event fired
13 * So we only overrid write methods here. And modify the first level path in routes object to `:localeId`
14 *
15 * @param {Function} createHistory
16 * @returns {Object} History
17 */
18const kootUseBasename = (createHistory) =>
19 (options = {}) => {
20 const history = createHistory(options)
21 const { basename } = options
22
23 const addBasename = (location) => {
24 if (!location)
25 return location
26
27 if (basename && location.basename == null) {
28 if (location.pathname.toLowerCase().indexOf(basename.toLowerCase()) === 0) {
29 location.pathname = location.pathname.substring(basename.length)
30 location.basename = basename
31
32 if (location.pathname === '')
33 location.pathname = '/'
34 } else {
35 location.basename = ''
36 }
37 }
38
39 return location
40 }
41
42 const prependBasename = (location) => {
43 if (!basename)
44 return location
45
46 const object = typeof location === 'string' ? parsePath(location) : location
47 const pname = object.pathname
48 const normalizedBasename = basename.slice(-1) === '/' ? basename : `${basename}/`
49 const normalizedPathname = pname.charAt(0) === '/' ? pname.slice(1) : pname
50 const pathname = normalizedBasename + normalizedPathname
51
52 return {
53 ...object,
54 pathname
55 }
56 }
57
58 // Override all write methods with basename-aware versions.
59 const push = (location) =>
60 history.push(prependBasename(location))
61
62 const replace = (location) =>
63 history.replace(prependBasename(location))
64
65 const createPath = (location) =>
66 history.createPath(prependBasename(location))
67
68 const createHref = (location) =>
69 history.createHref(prependBasename(location))
70
71 const createLocation = (location, ...args) =>
72 addBasename(history.createLocation(prependBasename(location), ...args))
73
74 return {
75 ...history,
76 push,
77 replace,
78 createPath,
79 createHref,
80 createLocation
81 }
82 }
83
84let historyClient
85
86const history = (() => {
87 if (__CLIENT__) {
88 if (!historyClient) {
89 const initialState = window.__REDUX_STATE__ || {}
90 const historyConfig = { basename: '/' }
91 if (isI18nEnabled() &&
92 process.env.KOOT_I18N_URL_USE === 'router' &&
93 initialState.localeId
94 ) {
95 historyConfig.basename = `/${initialState.localeId}`
96 historyClient = kootUseBasename(createHistory)(historyConfig)
97 } else {
98 // historyClient = require("__KOOT_CLIENT_REQUIRE_HISTORY__")
99 historyClient = createHistory()
100 }
101 // console.log({
102 // historyConfig,
103 // historyClient
104 // })
105 }
106 return historyClient
107 }
108
109 if (__SERVER__) {
110 return undefined
111 }
112})()
113
114export default history