-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-browser.js
35 lines (30 loc) · 940 Bytes
/
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
/**
* Implement Gatsby's Browser APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/browser-apis/
*/
import React from 'react';
import { LocationProvider } from '@reach/router';
import { navigate } from 'gatsby';
const isIos = () => {
const userAgent = window.navigator.userAgent.toLowerCase();
return /iphone|ipad|ipod/.test(userAgent);
};
const useStickyLocation = current => {
const previous = localStorage.getItem('location') || null;
localStorage.setItem('location', current);
if (window.history.length <= 1 && previous !== null && previous !== current) {
navigate(previous);
}
};
export const wrapRootElement = ({ element }) => (
<LocationProvider>
{({ location }) => {
// Save navigated location on iOS PWA; Restore it on reload
if (isIos() && window.navigator.standalone) {
useStickyLocation(location.pathname);
}
return element;
}}
</LocationProvider>
);