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
11
11
exports[`Helmet Data server renders without context 1`] = `"<base data-rh =\\"true\\" target =\\"_blank\\" href =\\"http://localhost/\\"/>"`;
12
12
13
13
exports[`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', () => {
73
73
expect ( head . base . toString ) . toBeDefined ( ) ;
74
74
expect ( head . base . toString ( ) ) . toMatchSnapshot ( ) ;
75
75
} ) ;
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
+ } ) ;
76
98
} ) ;
77
99
78
100
describe ( 'browser' , ( ) => {
Original file line number Diff line number Diff line change @@ -80,6 +80,10 @@ declare module 'react-helmet-async' {
80
80
context ?: { } ;
81
81
}
82
82
83
+ export class HelmetData {
84
+ constructor ( context : any , instances ?: Array < any > )
85
+ }
86
+
83
87
export class HelmetProvider extends React . Component < ProviderProps > {
84
88
static canUseDOM : boolean ;
85
89
}
Original file line number Diff line number Diff line change @@ -19,9 +19,13 @@ export default class HelmetData {
19
19
} ,
20
20
} ;
21
21
22
- constructor ( context ) {
22
+ constructor ( context , instances ) {
23
23
this . context = context ;
24
24
25
+ if ( instances ) {
26
+ this . instances = instances ;
27
+ }
28
+
25
29
if ( ! HelmetData . canUseDOM ) {
26
30
context . helmet = mapStateOnServer ( {
27
31
baseTag : [ ] ,
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
3
3
import fastCompare from 'react-fast-compare' ;
4
4
import invariant from 'invariant' ;
5
5
import { Context } from './Provider' ;
6
+ import HelmetData from './HelmetData' ;
6
7
import Dispatcher from './Dispatcher' ;
7
8
import { TAG_NAMES , VALID_TAG_NAMES , HTML_TAG_MAP } from './constants' ;
8
9
@@ -220,13 +221,18 @@ export class Helmet extends Component {
220
221
}
221
222
222
223
render ( ) {
223
- const { children, helmetData , ...props } = this . props ;
224
+ const { children, ...props } = this . props ;
224
225
let newProps = { ...props } ;
226
+ let { helmetData } = props ;
225
227
226
228
if ( children ) {
227
229
newProps = this . mapChildrenToProps ( children , newProps ) ;
228
230
}
229
231
232
+ if ( helmetData && ! ( helmetData instanceof HelmetData ) ) {
233
+ helmetData = new HelmetData ( helmetData . context , helmetData . instances ) ;
234
+ }
235
+
230
236
return helmetData ? (
231
237
// eslint-disable-next-line react/jsx-props-no-spreading
232
238
< Dispatcher { ...newProps } context = { helmetData . value } />
You can’t perform that action at this time.
0 commit comments