This repository has been archived by the owner on Sep 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the way html files are wrapped. Now it's way more flexible.
- Loading branch information
Showing
12 changed files
with
207 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,37 @@ | ||
declare module "react-helmet" { | ||
declare function rewind(): void; | ||
/* eslint-disable max-len */ | ||
// https://github.com/flowtype/flow-typed/tree/master/definitions/npm/react-helmet_v3.x.x | ||
|
||
declare interface ReactHelmet { | ||
rewind: rewind | ||
declare module "react-helmet" { | ||
declare type Props = { | ||
htmlAttributes?: Object, | ||
title?: string, | ||
defaultTitle?: string, | ||
titleTemplate?: string, | ||
base?: Object, | ||
meta?: Array<Object>, | ||
link?: Array<Object>, | ||
script?: Array<Object>, | ||
noscript?: Array<Object>, | ||
style?: Array<Object>, | ||
onChangeClientState?: (newState: Object, addedTags: Object, removeTags: Object) => void | mixed, | ||
}; | ||
declare interface HeadAttribute { | ||
toString(): string; | ||
toComponent(): React$Element<*>; | ||
} | ||
declare interface Head { | ||
htmlAttributes: HeadAttribute; | ||
title: HeadAttribute; | ||
base: HeadAttribute; | ||
meta: HeadAttribute; | ||
link: HeadAttribute; | ||
script: HeadAttribute; | ||
style: HeadAttribute; | ||
} | ||
|
||
declare var exports: ReactHelmet | ||
declare class Helmet extends React$Component { | ||
static rewind(): Head; | ||
props: Props; | ||
} | ||
declare var exports: typeof Helmet; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
exports[`test should render Html componnent 1`] = ` | ||
<html | ||
lang="en"> | ||
<head> | ||
<title | ||
data-react-helmet={true} /> | ||
<meta | ||
charSet="utf-8" | ||
data-react-helmet={true} /> | ||
<meta | ||
content="IE=edge" | ||
data-react-helmet={true} | ||
httpEquiv="X-UA-Compatible" /> | ||
<meta | ||
content="width=device-width, initial-scale=1" | ||
data-react-helmet={true} | ||
name="viewport" /> | ||
<link | ||
data-react-helmet={true} | ||
href="test.css" | ||
rel="stylesheet" /> | ||
</head> | ||
<body> | ||
<div /> | ||
<script /> | ||
<script | ||
data-react-helmet={true} | ||
src="test.css" /> | ||
</body> | ||
</html> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,18 @@ | ||
import test from "jest-ava-api" | ||
import React from "react" | ||
import { createRenderer } from "react-addons-test-utils" | ||
import expect from "expect" | ||
import expectJSX from "expect-jsx" | ||
|
||
import Html from "../index.js" | ||
|
||
expect.extend(expectJSX) | ||
|
||
const head = "head" | ||
const body = "body" | ||
const script = "script" | ||
const htmlProps = { lang: "en" } | ||
|
||
const renderer = (...args) => { | ||
const render = createRenderer() | ||
render.render(...args) | ||
return render.getRenderOutput() | ||
} | ||
|
||
test("should render Html componnent", () => { | ||
expect( | ||
renderer( | ||
<Html | ||
htmlProps={ htmlProps } | ||
head={ head } | ||
body={ body } | ||
script={ script } | ||
config={{ clientScripts: true }} | ||
> | ||
<p>{ "foo" }</p> | ||
</Html> | ||
) | ||
).toEqualJSX( | ||
<html | ||
lang="en" | ||
> | ||
<head dangerouslySetInnerHTML={{ __html: head }} /> | ||
<body> | ||
<div | ||
dangerouslySetInnerHTML={{ __html: body }} | ||
id="phenomic" | ||
/> | ||
<script dangerouslySetInnerHTML={{ __html: script }} /> | ||
<p>{ "foo" }</p> | ||
</body> | ||
</html> | ||
const renderer = createRenderer() | ||
renderer.render( | ||
/* eslint-disable react/jsx-no-bind */ | ||
<Html | ||
css={ [ "test.css" ] } | ||
js={ [ "test.css" ] } | ||
renderBody={ () => <div /> } | ||
renderScript={ () => <script /> } | ||
/> | ||
) | ||
expect(renderer.getRenderOutput()).toMatchSnapshot() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,65 @@ | ||
// @flow | ||
|
||
import React from "react" | ||
import { renderToString } from "react-dom/server" | ||
import Helmet from "react-helmet" | ||
|
||
type Props = { | ||
htmlProps: Object, | ||
head: string, | ||
body: string, | ||
script: string, | ||
config: PhenomicStaticConfig, | ||
children?: any, | ||
css: Array<string>, | ||
js: Array<string>, | ||
renderBody: () => React$Element<any>, | ||
renderScript: () => React$Element<any>, | ||
} | ||
|
||
const Html = (props: Props) => ( | ||
<html { ...props.htmlProps }> | ||
<head dangerouslySetInnerHTML={{ __html: props.head }} /> | ||
<body> | ||
<div | ||
id="phenomic" | ||
dangerouslySetInnerHTML={{ __html: props.body }} | ||
/> | ||
{ | ||
props.config.clientScripts && | ||
<script dangerouslySetInnerHTML={{ __html: props.script }} /> | ||
} | ||
{ props.config.clientScripts && props.children } | ||
</body> | ||
</html> | ||
) | ||
const defaultHtmlAttributes = { | ||
"lang": "en", | ||
} | ||
const defaultMeta = [ | ||
// <meta charset="utf-8" /> | ||
{ "charset": "utf-8" }, | ||
// <meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
{ "http-equiv": "X-UA-Compatible", "content": "IE=edge" }, | ||
// <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
{ "name": "viewport", "content": "width=device-width, initial-scale=1" }, | ||
] | ||
|
||
const Html = (props: Props) => { | ||
// Inject default html metas before | ||
// Those need to be rendered somehow otherwise Helmet won't consider those | ||
renderToString( | ||
<Helmet | ||
htmlAttributes={ defaultHtmlAttributes } | ||
meta={ defaultMeta } | ||
link={ [ | ||
...props.css.map((file) => ({ rel: "stylesheet", href: file })), | ||
] } | ||
script={ [ | ||
...props.js.map((file) => ({ src: file })), | ||
] } | ||
/> | ||
) | ||
|
||
// render body | ||
const body = props.renderBody() | ||
// rewind html metas | ||
const head = Helmet.rewind() | ||
|
||
// <!doctype html> is automatically prepended | ||
return ( | ||
<html { ...head.htmlAttributes.toComponent() }> | ||
<head> | ||
{ head.base.toComponent() } | ||
{ head.title.toComponent() } | ||
{ head.meta.toComponent() } | ||
{ head.link.toComponent() } | ||
</head> | ||
<body> | ||
{ body } | ||
{ props.renderScript() } | ||
{ head.script.toComponent() } | ||
</body> | ||
</html> | ||
) | ||
} | ||
|
||
export default Html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.