-
Notifications
You must be signed in to change notification settings - Fork 34
/
gatsby-ssr.js
46 lines (40 loc) · 1.32 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
import React from 'react'
import { URL } from 'url'
function getLinkProps({ crossOrigin, pathname }) {
switch (typeof crossOrigin) {
case `string`:
return { crossOrigin }
case `function`:
return getLinkProps({ crossOrigin: crossOrigin(pathname), pathname })
default:
return { crossOrigin: `anonymous` }
}
}
export const onRenderBody = (
{ setHeadComponents, pathname = `/` },
{ crossOrigin = `anonymous` } = {}
) => {
const props = getLinkProps({ crossOrigin, pathname })
const assets = [
'https://fonts.gstatic.com/s/sourcecodepro/v11/HI_SiYsKILxRpg3hIP6sJ7fM7PqlPevT.ttf',
'https://fonts.gstatic.com/s/sourcesanspro/v13/6xK3dSBYKcSV-LCoeQqfX1RYOo3qOK7g.ttf',
'https://fonts.gstatic.com/s/sourcesanspro/v13/6xKydSBYKcSV-LCoeQqfX1RYOo3i54rwlxdr.ttf',
]
setHeadComponents(
assets.map((href) => {
let assetProps
// External urls should get the props from the plugin configuration.
// Local urls will be forced with `crossOrigin: "anonymous"`
try {
// check if URL is external, if not this constructor throws.
new URL(href)
assetProps = props
} catch (e) {
assetProps = { crossOrigin: `anonymous` }
}
return (
<link key={href} as="font" href={href} rel="preload" {...assetProps} />
)
})
)
}