-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgatsby-browser.ts
36 lines (30 loc) · 1.03 KB
/
gatsby-browser.ts
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
/* eslint-disable no-restricted-globals */
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
exports.onInitialClientRender = () => {
if (history.scrollRestoration) {
history.scrollRestoration = 'manual';
}
};
exports.shouldUpdateScroll = ({
routerProps: { location },
prevRouterProps = {},
getSavedScrollPosition,
}) => {
const [, currentPositionY] = getSavedScrollPosition(location);
const [, prevPositionY] = getSavedScrollPosition(prevRouterProps?.location ?? location);
const withHash = Boolean(location?.hash);
const isScrollDifferentFromPreviousPage =
prevRouterProps.location && prevPositionY !== currentPositionY;
const shouldScrollOnInitialLoad = !prevRouterProps.location && currentPositionY;
if (isScrollDifferentFromPreviousPage ?? shouldScrollOnInitialLoad) {
window.scrollTo({
top: currentPositionY,
...(!withHash && { behavior: 'instant' }),
});
}
return true;
};
exports.onPreRouteUpdate = ({ prevLocation }) => {
window.prevLocation = prevLocation;
};