Skip to content

Commit 73326cd

Browse files
authored
Merge pull request #63 from fujikky/master
Fix redirect & changeLocale with prefix path
2 parents a80d2b0 + 20bd538 commit 73326cd

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/link.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ export const changeLocale = (language, to) => {
5555
}
5656
const { routed } = window.___gatsbyIntl
5757

58+
const removePrefix = pathname => {
59+
const base =
60+
typeof __BASE_PATH__ !== `undefined` ? __BASE_PATH__ : __PATH_PREFIX__
61+
if (base && pathname.indexOf(base) === 0) {
62+
pathname = pathname.slice(base.length)
63+
}
64+
return pathname
65+
}
66+
5867
const removeLocalePart = pathname => {
5968
if (!routed) {
6069
return pathname
@@ -63,7 +72,8 @@ export const changeLocale = (language, to) => {
6372
return pathname.substring(i)
6473
}
6574

66-
const pathname = to || removeLocalePart(window.location.pathname)
75+
const pathname =
76+
to || removeLocalePart(removePrefix(window.location.pathname))
6777
// TODO: check slash
6878
const link = `/${language}${pathname}${window.location.search}`
6979
localStorage.setItem("gatsby-intl-language", language)

src/wrap-page.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default ({ element, props }) => {
4343

4444
const { pageContext, location } = props
4545
const { intl } = pageContext
46-
const { language, languages, redirect, routed } = intl
46+
const { language, languages, redirect, routed, originalPath } = intl
4747

4848
if (typeof window !== "undefined") {
4949
window.___gatsbyIntl = intl
@@ -52,7 +52,7 @@ export default ({ element, props }) => {
5252
const isRedirect = redirect && !routed
5353

5454
if (isRedirect) {
55-
const { pathname, search } = location
55+
const { search } = location
5656

5757
// Skip build, Browsers only
5858
if (typeof window !== "undefined") {
@@ -68,7 +68,7 @@ export default ({ element, props }) => {
6868
}
6969

7070
const queryParams = search || ""
71-
const newUrl = withPrefix(`/${detected}${pathname}${queryParams}`)
71+
const newUrl = withPrefix(`/${detected}${originalPath}${queryParams}`)
7272
window.localStorage.setItem("gatsby-intl-language", detected)
7373
window.location.replace(newUrl)
7474
}

0 commit comments

Comments
 (0)