Skip to content

Commit

Permalink
fix: only use instances singleton within the browser
Browse files Browse the repository at this point in the history
  • Loading branch information
blittle authored and staylor committed Dec 6, 2021
1 parent 6e46a51 commit 69503b6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/HelmetData.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,29 @@ export function clearInstances() {
}

export default class HelmetData {
instances = [];

value = {
setHelmet: serverState => {
this.context.helmet = serverState;
},
helmetInstances: {
get: () => instances,
get: () => (this.canUseDOM ? instances : this.instances),
add: instance => {
instances.push(instance);
(this.canUseDOM ? instances : this.instances).push(instance);
},
remove: instance => {
const index = instances.indexOf(instance);
instances.splice(index, 1);
const index = (this.canUseDOM ? instances : this.instances).indexOf(instance);
(this.canUseDOM ? instances : this.instances).splice(index, 1);
},
},
};

constructor(context) {
constructor(context, canUseDOM = typeof document !== 'undefined') {
this.context = context;
this.canUseDOM = canUseDOM;

if (!HelmetData.canUseDOM) {
if (!canUseDOM) {
context.helmet = mapStateOnServer({
baseTag: [],
bodyAttributes: {},
Expand Down
2 changes: 1 addition & 1 deletion src/Provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class Provider extends Component {
constructor(props) {
super(props);

this.helmetData = new HelmetData(this.props.context);
this.helmetData = new HelmetData(this.props.context, Provider.canUseDOM);
}

render() {
Expand Down

0 comments on commit 69503b6

Please sign in to comment.