-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
utils.js
54 lines (49 loc) · 1.21 KB
/
utils.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
import { VALID_NODE_NAMES } from "./constants"
/**
* Filter the props coming from a page down to just the ones that are relevant for head.
* This e.g. filters out properties that are undefined during SSR.
*/
export function filterHeadProps(input) {
return {
location: {
pathname: input.location.pathname,
},
params: input.params,
data: input.data || {},
pageContext: input.pageContext,
}
}
/**
* Throw error if Head export is not a valid
*/
export function headExportValidator(head) {
if (typeof head !== `function`)
throw new Error(
`Expected "Head" export to be a function got "${typeof head}".`
)
}
/**
* Warn once for same messsage
*/
let warnOnce = _ => {}
if (process.env.NODE_ENV !== `production`) {
const warnings = new Set()
warnOnce = msg => {
if (!warnings.has(msg)) {
console.warn(msg)
}
warnings.add(msg)
}
}
/**
* Warn for invalid tags in head.
* @param {string} tagName
*/
export function warnForInvalidTags(tagName) {
if (process.env.NODE_ENV !== `production`) {
const warning = `<${tagName}> is not a valid head element. Please use one of the following: ${VALID_NODE_NAMES.join(
`, `
)}`
warnOnce(warning)
}
}