-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-browser.js
59 lines (50 loc) · 1.4 KB
/
gatsby-browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* Implement Gatsby's Browser APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/browser-apis/
*/
import "src/styles/global.scss"
// Load i18n graphql fragment
import { querying } from "src/i18n/querying" // eslint-disable-line
// Load imaging graphql fragments
// eslint-disable-next-line
import * as imaging from "src/imaging"
/**
* Automatically redirect to /en if lang prefix is missing
*/
export const onClientEntry = () => {
const hasLangPrefix =
window.location.pathname.startsWith("/en") ||
window.location.pathname.startsWith("/cs")
if (window.location.pathname.startsWith("/en")) {
const pathname = window.location.pathname.replace("/en", "");
window.location.pathname = `/cs${pathname}`
}
if (!hasLangPrefix) {
// window.location.pathname = `/en${window.location.pathname}`
window.location.pathname = `/cs${window.location.pathname}`
}
}
export const onRouteUpdate = ({ location }) => {
anchorScroll(location)
return true
}
export const shouldUpdateScroll = ({
routerProps: { location },
getSavedScrollPosition,
}) => {
if (location && location.hash) {
anchorScroll(location)
return false
} else {
return true
}
}
function anchorScroll(location) {
if (location && location.hash) {
setTimeout(() => {
const elm = document.querySelector(`${location.hash}`)
elm.scrollIntoView({ behavior: "smooth" })
}, 20)
}
}