Skip to content

Commit 649ae7d

Browse files
author
Ben Taylor
authored
Merge pull request #4 from flybayer/fix-ssr
Use dangerouslySetInnerHTML to ensure everything renders properly
2 parents 06fbd58 + c3d83fd commit 649ae7d

4 files changed

Lines changed: 22 additions & 9 deletions

File tree

dist/JSONLD.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ var JSONLD = function JSONLD(props) {
3434
var firstChild = new ChildClass(props.children.props).getJSON(true, schema);
3535
json = ChildClass.name === 'JSONLDNodeCollection' ? _extends({ '@context': 'http://schema.org/' }, _defineProperty({}, type, firstChild)) : _extends({ '@context': 'http://schema.org/' }, firstChild);
3636
}
37-
return _react2.default.createElement(
37+
38+
return props.dangerouslyExposeHtml ? _react2.default.createElement('script', { type: 'application/ld+json', dangerouslySetInnerHTML: { __html: JSON.stringify(json) } }) : _react2.default.createElement(
3839
'script',
3940
{ type: 'application/ld+json' },
4041
JSON.stringify(json)
@@ -43,7 +44,12 @@ var JSONLD = function JSONLD(props) {
4344

4445
exports.JSONLD = JSONLD;
4546
JSONLD.propTypes = {
46-
additionalType: _propTypes2.default.object
47+
additionalType: _propTypes2.default.object,
48+
dangerouslyExposeHtml: _propTypes2.default.bool
49+
};
50+
51+
JSONLD.defaultProps = {
52+
dangerouslyExposeHtml: false
4753
};
4854

4955
exports.default = JSONLD;

dist/schemas/Generic.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ var _JSONLDAbstractNode3 = _interopRequireDefault(_JSONLDAbstractNode2);
1616

1717
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1818

19-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
20-
2119
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
2220

2321
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
@@ -45,7 +43,8 @@ var ChildNode = function (_JSONLDAbstractNode) {
4543
var details = {
4644
'@type': this.props.jsonldtype
4745
};
48-
return isFirstChildNode ? _extends.apply(undefined, [_extends({}, details, schema)].concat(_toConsumableArray(parseChildren))) : _extends(_defineProperty({}, this.props.id, _extends({}, details, schema, parseChildren)));
46+
47+
return _extends.apply(undefined, [_extends({}, details, schema)].concat(_toConsumableArray(parseChildren)));
4948
}
5049
}]);
5150

src/core/JSONLD.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@ export const JSONLD = props => {
1111
? Object.assign({'@context': 'http://schema.org/'}, {[type]: firstChild})
1212
: Object.assign({'@context': 'http://schema.org/'}, firstChild)
1313
}
14-
return (
15-
<script type="application/ld+json">{ JSON.stringify(json) }</script>
14+
15+
return (props.dangerouslyExposeHtml
16+
? <script type="application/ld+json" dangerouslySetInnerHTML={{__html: JSON.stringify(json)}} />
17+
: <script type="application/ld+json">{ JSON.stringify(json) }</script>
1618
);
1719
}
1820

1921
JSONLD.propTypes = {
20-
additionalType: PropTypes.object
22+
additionalType: PropTypes.object,
23+
dangerouslyExposeHtml: PropTypes.bool
24+
};
25+
26+
JSONLD.defaultProps = {
27+
dangerouslyExposeHtml: false
2128
};
2229

2330
export default JSONLD;

src/example/containers/Example.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
Review,
99
Author,
1010
Location,
11-
Rating
11+
Rating,
12+
Generic
1213
} from 'react-structured-data';
1314

1415

0 commit comments

Comments
 (0)