File tree Expand file tree Collapse file tree 5 files changed +40
-2
lines changed Expand file tree Collapse file tree 5 files changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -11,3 +11,5 @@ exports[`Helmet Data server renders declarative without context 1`] = `"<base da
1111exports[`Helmet Data server renders without context 1`] = `"<base data-rh =\\"true\\" target =\\"_blank\\" href =\\"http://localhost/\\"/>"`;
1212
1313exports[`Helmet Data server sets base tag based on deepest nested component 1`] = `"<base data-rh =\\"true\\" href =\\"http://mysite.com/public\\"/>"`;
14+
15+ exports[`Helmet Data server works with the same context object but separate HelmetData instances 1`] = `"<base data-rh =\\"true\\" href =\\"http://mysite.com/public\\"/>"`;
Original file line number Diff line number Diff line change @@ -73,6 +73,28 @@ describe('Helmet Data', () => {
7373 expect ( head . base . toString ) . toBeDefined ( ) ;
7474 expect ( head . base . toString ( ) ) . toMatchSnapshot ( ) ;
7575 } ) ;
76+
77+ it ( 'works with the same context object but separate HelmetData instances' , ( ) => {
78+ const context = { } ;
79+ const instances = [ ] ;
80+
81+ render (
82+ < div >
83+ < Helmet helmetData = { new HelmetData ( context , instances ) } >
84+ < base href = "http://mysite.com" />
85+ </ Helmet >
86+ < Helmet helmetData = { new HelmetData ( context , instances ) } >
87+ < base href = "http://mysite.com/public" />
88+ </ Helmet >
89+ </ div >
90+ ) ;
91+
92+ const head = context . helmet ;
93+
94+ expect ( head . base ) . toBeDefined ( ) ;
95+ expect ( head . base . toString ) . toBeDefined ( ) ;
96+ expect ( head . base . toString ( ) ) . toMatchSnapshot ( ) ;
97+ } ) ;
7698 } ) ;
7799
78100 describe ( 'browser' , ( ) => {
Original file line number Diff line number Diff line change @@ -80,6 +80,10 @@ declare module 'react-helmet-async' {
8080 context ?: { } ;
8181 }
8282
83+ export class HelmetData {
84+ constructor ( context : any , instances ?: Array < any > )
85+ }
86+
8387 export class HelmetProvider extends React . Component < ProviderProps > {
8488 static canUseDOM : boolean ;
8589 }
Original file line number Diff line number Diff line change @@ -19,9 +19,13 @@ export default class HelmetData {
1919 } ,
2020 } ;
2121
22- constructor ( context ) {
22+ constructor ( context , instances ) {
2323 this . context = context ;
2424
25+ if ( instances ) {
26+ this . instances = instances ;
27+ }
28+
2529 if ( ! HelmetData . canUseDOM ) {
2630 context . helmet = mapStateOnServer ( {
2731 baseTag : [ ] ,
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
33import fastCompare from 'react-fast-compare' ;
44import invariant from 'invariant' ;
55import { Context } from './Provider' ;
6+ import HelmetData from './HelmetData' ;
67import Dispatcher from './Dispatcher' ;
78import { TAG_NAMES , VALID_TAG_NAMES , HTML_TAG_MAP } from './constants' ;
89
@@ -220,13 +221,18 @@ export class Helmet extends Component {
220221 }
221222
222223 render ( ) {
223- const { children, helmetData , ...props } = this . props ;
224+ const { children, ...props } = this . props ;
224225 let newProps = { ...props } ;
226+ let { helmetData } = props ;
225227
226228 if ( children ) {
227229 newProps = this . mapChildrenToProps ( children , newProps ) ;
228230 }
229231
232+ if ( helmetData && ! ( helmetData instanceof HelmetData ) ) {
233+ helmetData = new HelmetData ( helmetData . context , helmetData . instances ) ;
234+ }
235+
230236 return helmetData ? (
231237 // eslint-disable-next-line react/jsx-props-no-spreading
232238 < Dispatcher { ...newProps } context = { helmetData . value } />
You can’t perform that action at this time.
0 commit comments