Skip to content

Commit

Permalink
fix: move to a singleton for tracking Helmet instances
Browse files Browse the repository at this point in the history
This resolves an issue where multiple HelmetProviders on the same
page don't know about each other.
  • Loading branch information
blittle authored and staylor committed Dec 6, 2021
1 parent 7a7c114 commit 906b53d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
2 changes: 2 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'raf/polyfill';
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import ReactDOM from 'react-dom';
import { clearInstances } from './src/HelmetData';

configure({ adapter: new Adapter() });

Expand Down Expand Up @@ -40,4 +41,5 @@ beforeEach(() => {

afterEach(() => {
ReactDOM.unmountComponentAtNode(mount);
clearInstances();
});
22 changes: 11 additions & 11 deletions src/HelmetData.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import mapStateOnServer from './server';

export default class HelmetData {
instances = [];
const instances = [];

export function clearInstances() {
instances.length = 0;
}

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

constructor(context, instances) {
constructor(context) {
this.context = context;

if (instances) {
this.instances = instances;
}

if (!HelmetData.canUseDOM) {
context.helmet = mapStateOnServer({
baseTag: [],
Expand Down

0 comments on commit 906b53d

Please sign in to comment.