forked from staylor/react-helmet-async
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelmetData.ts
67 lines (58 loc) · 1.71 KB
/
HelmetData.ts
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
61
62
63
64
65
66
67
import type HelmetDispatcher from './Dispatcher';
import mapStateOnServer from './server';
import type { HelmetServerState, MappedServerState } from './types';
const instances: HelmetDispatcher[] = [];
export function clearInstances() {
instances.length = 0;
}
export interface HelmetDataType {
instances: HelmetDispatcher[];
context: HelmetDataContext;
}
interface HelmetDataContext {
helmet: HelmetServerState;
}
export const isDocument = !!(
typeof window !== 'undefined' &&
window.document &&
window.document.createElement
);
export default class HelmetData implements HelmetDataType {
instances = [];
canUseDOM = isDocument;
context: HelmetDataContext;
value = {
setHelmet: (serverState: HelmetServerState) => {
this.context.helmet = serverState;
},
helmetInstances: {
get: () => (this.canUseDOM ? instances : this.instances),
add: (instance: HelmetDispatcher) => {
(this.canUseDOM ? instances : this.instances).push(instance);
},
remove: (instance: HelmetDispatcher) => {
const index = (this.canUseDOM ? instances : this.instances).indexOf(instance);
(this.canUseDOM ? instances : this.instances).splice(index, 1);
},
},
};
constructor(context: any, canUseDOM?: boolean) {
this.context = context;
this.canUseDOM = canUseDOM || false;
if (!canUseDOM) {
context.helmet = mapStateOnServer({
baseTag: [],
bodyAttributes: {},
encodeSpecialCharacters: true,
htmlAttributes: {},
linkTags: [],
metaTags: [],
noscriptTags: [],
scriptTags: [],
styleTags: [],
title: '',
titleAttributes: {},
} as MappedServerState);
}
}
}