Skip to content

Commit

Permalink
feat: add hooks for body also (#16)
Browse files Browse the repository at this point in the history
* feat: add hooks for body also

* chore: move the renderTags function outside
  • Loading branch information
crash7 authored and lfantone committed Feb 4, 2019
1 parent d7f828e commit abcd587
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
19 changes: 11 additions & 8 deletions flow-typed/documentComponent_vx.x.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ declare module 'Document.component' {
error: Error
};

declare type ExtraTag = {
name: string,
tag: string,
content: string,
attribs?: {
[key: string]: string
}
};

declare type DocumentInitialProps = {
assets: Assets,
criticalCSS: boolean | string,
Expand All @@ -66,14 +75,8 @@ declare module 'Document.component' {
errorComponent: React$ComponentType<ErrorProps>,
filterServerData: (data: DataType) => DataType,
html: string,
extraHeadTags: Array<{
name: string,
tag: string,
content: string,
attribs?: {
[key: string]: string
}
}>,
extraHeadTags: Array<ExtraTag>,
extraBodyTags: Array<ExtraTag>,
[key: string]: any
};

Expand Down
20 changes: 15 additions & 5 deletions src/Document.component.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// @flow strict
import type { DocumentInitialProps, Context, Extractor, DataType } from 'Document.component';
import type {
ExtraTag,
DocumentInitialProps,
Context,
Extractor,
DataType
} from 'Document.component';
import React, { PureComponent } from 'react';
import { F, identity, path } from 'ramda';
import Error from './Error.component';
Expand All @@ -11,6 +17,10 @@ const getHeaderTags = (extractor: Extractor) => [
...extractor.getLinkElements()
];

const renderTags = ({ tag: Tag, content, name, attribs }: ExtraTag) => (
<Tag key={name} {...attribs} dangerouslySetInnerHTML={{ __html: content }} />
);

export class DocumentComponent extends PureComponent<DocumentInitialProps> {
static async getInitialProps({
assets,
Expand All @@ -35,7 +45,8 @@ export class DocumentComponent extends PureComponent<DocumentInitialProps> {
errorComponent: ErrorComponent,
filterServerData = identity,
extractor,
extraHeadTags = []
extraHeadTags = [],
extraBodyTags = []
} = this.props;
// get attributes from React Helmet
const htmlAttrs = helmet.htmlAttributes.toComponent();
Expand All @@ -57,9 +68,7 @@ export class DocumentComponent extends PureComponent<DocumentInitialProps> {
{extractor && getHeaderTags(extractor)}
{criticalCSS !== false && criticalCSS}
{clientCss && <link rel="stylesheet" href={clientCss} />}
{extraHeadTags.map(({ tag: Tag, content, name, attribs }) => (
<Tag key={name} {...attribs} dangerouslySetInnerHTML={{ __html: content }} />
))}
{extraHeadTags.map(renderTags)}
</head>
<body {...bodyAttrs}>
<Root />
Expand All @@ -71,6 +80,7 @@ export class DocumentComponent extends PureComponent<DocumentInitialProps> {
<Error message={error.message} stack={error.stack} />
)
) : null}
{extraBodyTags.map(renderTags)}
</body>
</html>
);
Expand Down

0 comments on commit abcd587

Please sign in to comment.