forked from lgsvl/svlsimulator.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-ssr.js
33 lines (32 loc) · 1.16 KB
/
gatsby-ssr.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
/* eslint-disable @typescript-eslint/no-var-requires */
const React = require('react');
exports.onPreRenderHTML = ({ pathname, getHeadComponents, replaceHeadComponents }) => {
const headComponents = getHeadComponents();
// Remove any lingering service workers
headComponents.unshift(
<script dangerouslySetInnerHTML={{
__html: `
if (typeof navigator !== 'undefined' && navigator.serviceWorker && navigator.serviceWorker.getRegistrations) {
navigator.serviceWorker.getRegistrations().then(function(registrations) {
for(let registration of registrations) {
registration.unregister();
}
});
}
`
}} />
);
if (pathname.endsWith('/404.html') && pathname !== '/docs/404.html') {
// Add redirect to docs 404 page when URL originated from /docs/
headComponents.unshift(
<script dangerouslySetInnerHTML={{
__html: `
if (typeof window !== 'undefined' && /^\\/*(!?docs\\/|docs$)/.test(window.location.pathname)) {
window.location.pathname = '/docs/404.html';
}
`
}} />
);
}
replaceHeadComponents(headComponents);
}