forked from TkDodo/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-ssr.js
60 lines (54 loc) · 2.47 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
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
60
import { jsx } from 'theme-ui'
import * as React from 'react'
import { withPrefix } from 'gatsby'
const noFlashDark = `(function () {
try {
var hasLocalStorage = localStorage.getItem('theme-ui-color-mode');
if (
window.matchMedia('(prefers-color-scheme: dark)').matches
) {
document.querySelector('html').setAttribute('data-theme', 'dark')
if (!hasLocalStorage) {
document.documentElement.classList.add('theme-ui-dark')
window.addEventListener('load', () => {
document.documentElement.classList.remove('theme-ui-dark')
});
}
}
} catch (err) {}
})();`
export const onRenderBody = ({ setPreBodyComponents }) => {
setPreBodyComponents([
jsx('script', {
key: 'theme-ui-no-flash-dark',
dangerouslySetInnerHTML: {
__html: noFlashDark,
},
}),
])
}
const fontUrl = `${withPrefix('fonts/Inter.woff2')}`
const font = `@font-face{font-family:'Inter';font-style:normal;font-weight:100;font-display:block;src:url(${fontUrl}) format('woff2');}@font-face{font-family:'Inter';font-style:normal;font-weight:200;font-display:block;src:url(${fontUrl}) format('woff2');}@font-face{font-family:'Inter';font-style:normal;font-weight:300;font-display:block;src:url(${fontUrl}) format('woff2');}@font-face{font-family:'Inter';font-style:normal;font-weight:400;font-display:block;src:url(${fontUrl}) format('woff2');}@font-face{font-family:'Inter';font-style:normal;font-weight:500;font-display:block;src:url(${fontUrl}) format('woff2');}@font-face{font-family:'Inter';font-style:normal;font-weight:600;font-display:block;src:url(${fontUrl}) format('woff2');}@font-face{font-family:'Inter';font-style:normal;font-weight:700;font-display:block;src:url(${fontUrl}) format('woff2');}@font-face{font-family:'Inter';font-style:normal;font-weight:800;font-display:block;src:url(${fontUrl}) format('woff2');}@font-face{font-family:'Inter';font-style:normal;font-weight:900;font-display:block;src:url(${fontUrl}) format('woff2');}`
export const onPreRenderHTML = ({
getHeadComponents,
replaceHeadComponents,
}) => {
const components = [
React.createElement('link', {
key: 'font-inter',
rel: 'preload',
href: fontUrl,
as: 'font',
type: 'font/woff2',
crossOrigin: 'anonymous',
}),
React.createElement('style', {
key: 'font-face.inter',
dangerouslySetInnerHTML: {
__html: font,
},
}),
...getHeadComponents(),
]
replaceHeadComponents(components)
}