Skip to content

Commit 69503b6

Browse files
blittlestaylor
authored andcommitted
fix: only use instances singleton within the browser
1 parent 6e46a51 commit 69503b6

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/HelmetData.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,29 @@ export function clearInstances() {
77
}
88

99
export default class HelmetData {
10+
instances = [];
11+
1012
value = {
1113
setHelmet: serverState => {
1214
this.context.helmet = serverState;
1315
},
1416
helmetInstances: {
15-
get: () => instances,
17+
get: () => (this.canUseDOM ? instances : this.instances),
1618
add: instance => {
17-
instances.push(instance);
19+
(this.canUseDOM ? instances : this.instances).push(instance);
1820
},
1921
remove: instance => {
20-
const index = instances.indexOf(instance);
21-
instances.splice(index, 1);
22+
const index = (this.canUseDOM ? instances : this.instances).indexOf(instance);
23+
(this.canUseDOM ? instances : this.instances).splice(index, 1);
2224
},
2325
},
2426
};
2527

26-
constructor(context) {
28+
constructor(context, canUseDOM = typeof document !== 'undefined') {
2729
this.context = context;
30+
this.canUseDOM = canUseDOM;
2831

29-
if (!HelmetData.canUseDOM) {
32+
if (!canUseDOM) {
3033
context.helmet = mapStateOnServer({
3134
baseTag: [],
3235
bodyAttributes: {},

src/Provider.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default class Provider extends Component {
3636
constructor(props) {
3737
super(props);
3838

39-
this.helmetData = new HelmetData(this.props.context);
39+
this.helmetData = new HelmetData(this.props.context, Provider.canUseDOM);
4040
}
4141

4242
render() {

0 commit comments

Comments
 (0)