├── .babelrc ├── .eslintrc ├── .gitignore ├── .npmrc ├── .travis.yml ├── LICENSE ├── README.md ├── package.json ├── src └── index.js └── test ├── .eslintrc ├── components ├── ExampleReactComponent.js └── HypernovaExampleReact.js ├── init.js ├── renderReact-test.js └── renderReactStatic-test.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["airbnb"] 3 | } 4 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "root": true, 3 | "extends": "airbnb" 4 | } 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | node_modules 28 | 29 | # Optional npm cache directory 30 | .npm 31 | 32 | # Optional REPL history 33 | .node_repl_history 34 | 35 | TODO 36 | coverage 37 | lib 38 | 39 | # Only apps should have lockfiles 40 | npm-shrinkwrap.json 41 | yarn.lock 42 | package-lock.json 43 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | os: 3 | - linux 4 | node_js: 5 | - "10" 6 | - "8" 7 | - "6" 8 | - "4" 9 | before_install: 10 | - 'nvm install-latest-npm' 11 | install: 12 | - 'if [ "${TRAVIS_NODE_VERSION}" = "0.6" ]; then nvm install --latest-npm 0.8 && npm install && nvm use "${TRAVIS_NODE_VERSION}"; else npm install; fi;' 13 | script: 14 | - 'if [ -n "${PRETEST-}" ]; then npm run pretest ; fi' 15 | - 'if [ -n "${POSTTEST-}" ]; then npm run posttest ; fi' 16 | - 'if [ -n "${COVERAGE-}" ]; then npm run coverage ; fi' 17 | - 'if [ -n "${REACT-}" ]; then npm run tests-only ; fi' 18 | sudo: false 19 | env: 20 | - REACT=0.14 21 | - REACT=15.4 22 | - REACT=15 23 | - REACT=16 24 | matrix: 25 | fast_finish: true 26 | include: 27 | - node_js: "lts/*" 28 | env: PRETEST=true 29 | - node_js: "4" 30 | env: COVERAGE=true 31 | allow_failures: 32 | - os: osx 33 | - env: TEST=true ALLOW_FAILURE=true 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Airbnb 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | | :exclamation: Deprecation Notice | 2 | |:-| 3 | |We want to express our sincere gratitude for your support and contributions to the Hypernova open source project. As we are no longer using this technology internally, we have come to the decision to archive the Hypernova repositories. While we won't be providing further updates or support, the existing code and resources will remain accessible for your reference. We encourage anyone interested to fork the repository and continue the project's legacy independently. Thank you for being a part of this journey and for your patience and understanding.| 4 | --- 5 | 6 | # hypernova-react 7 | 8 | [React](https://github.com/facebook/react) bindings for [Hypernova](https://github.com/airbnb/hypernova). 9 | 10 | On the server, wraps the component in a function to render it to a HTML string given its props. 11 | 12 | On the client, calling this function with your component scans the DOM for any server-side rendered instances of it. It then resumes those components using the server-specified props. 13 | 14 | ## Install 15 | 16 | ```sh 17 | npm install hypernova-react 18 | ``` 19 | 20 | ## Usage 21 | 22 | Here's how to use it in your module: 23 | 24 | ```js 25 | import { renderReact } from 'hypernova-react'; 26 | import MyComponent from './src/MyComponent.jsx'; 27 | 28 | export default renderReact( 29 | 'MyComponent.hypernova.js', // this file's name (or really any unique name) 30 | MyComponent, 31 | ); 32 | ``` 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hypernova-react", 3 | "version": "2.1.0", 4 | "description": "React bindings for Hypernova", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "prepublish": "npm run build", 8 | "clean": "rimraf lib coverage", 9 | "prebuild": "npm run clean", 10 | "build": "babel src -d lib", 11 | "lint": "eslint src test", 12 | "pretest": "npm run lint", 13 | "test": "npm run coverage", 14 | "react": "enzyme-adapter-react-install 16", 15 | "pretests-only": "npm run react", 16 | "tests-only": "npm run test:quick", 17 | "precoverage": "npm run react && npm run build", 18 | "coverage": "babel-node node_modules/.bin/istanbul cover --report=html node_modules/.bin/_mocha -- -R tap test/*-test.js", 19 | "pretest:quick": "npm run build", 20 | "test:quick": "babel-node node_modules/.bin/_mocha -R tap test/*-test.js" 21 | }, 22 | "repository": { 23 | "type": "git", 24 | "url": "git+https://github.com/airbnb/hypernova-react.git" 25 | }, 26 | "keywords": [ 27 | "react", 28 | "hypernova", 29 | "server", 30 | "render", 31 | "isomorphic", 32 | "universal" 33 | ], 34 | "files": [ 35 | "README.md", 36 | "lib", 37 | "src", 38 | "test", 39 | ".eslintrc", 40 | ".babelrc" 41 | ], 42 | "author": "Josh Perez ", 43 | "license": "MIT", 44 | "bugs": { 45 | "url": "https://github.com/airbnb/hypernova-react/issues" 46 | }, 47 | "homepage": "https://github.com/airbnb/hypernova-react#readme", 48 | "peerDependencies": { 49 | "hypernova": "^2.0.0", 50 | "react": "^0.14 || ^15 || ^16", 51 | "react-dom": "^0.14 || ^15 || ^16" 52 | }, 53 | "devDependencies": { 54 | "babel-cli": "^6.26.0", 55 | "babel-preset-airbnb": "^2.5.3", 56 | "chai": "^4.1.2", 57 | "enzyme": "^3.4.1", 58 | "enzyme-adapter-react-helper": "^1.3.0", 59 | "eslint": "^5.3.0", 60 | "eslint-config-airbnb": "^17.1.0", 61 | "eslint-plugin-import": "^2.14.0", 62 | "eslint-plugin-jsx-a11y": "^6.1.1", 63 | "eslint-plugin-react": "^7.11.1", 64 | "hypernova": "^2.3.0", 65 | "istanbul": "^0.4.5", 66 | "jsdom": "^9.12.0", 67 | "mocha": "^4.1.0", 68 | "prop-types": "^15.6.2", 69 | "react": "^0.14 || ^15 || ^16", 70 | "react-dom": "^0.14 || ^15 || ^16", 71 | "rimraf": "^2.6.2", 72 | "sinon": "^6.1.5", 73 | "sinon-sandbox": "^2.0.0" 74 | }, 75 | "engines": { 76 | "node": ">= 4.0" 77 | }, 78 | "greenkeeper": { 79 | "ignore": [ 80 | "jsdom", 81 | "mocha" 82 | ] 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import ReactDOMServer from 'react-dom/server'; 4 | import hypernova, { serialize, load } from 'hypernova'; 5 | 6 | export const renderReact = (name, component) => hypernova({ 7 | server() { 8 | return (props) => { 9 | const contents = ReactDOMServer.renderToString(React.createElement(component, props)); 10 | return serialize(name, contents, props); 11 | }; 12 | }, 13 | 14 | client() { 15 | const payloads = load(name); 16 | 17 | if (payloads) { 18 | payloads.forEach((payload) => { 19 | const { node, data } = payload; 20 | const element = React.createElement(component, data); 21 | 22 | if (ReactDOM.hydrate) { 23 | ReactDOM.hydrate(element, node); 24 | } else { 25 | ReactDOM.render(element, node); 26 | } 27 | }); 28 | } 29 | 30 | return component; 31 | }, 32 | }); 33 | 34 | export const renderReactStatic = (name, component) => hypernova({ 35 | server() { 36 | return props => ReactDOMServer.renderToStaticMarkup(React.createElement(component, props)); 37 | }, 38 | 39 | client() {}, 40 | }); 41 | -------------------------------------------------------------------------------- /test/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "mocha": true 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /test/components/ExampleReactComponent.js: -------------------------------------------------------------------------------- 1 | const React = require('react'); 2 | const PropTypes = require('prop-types'); 3 | 4 | function ExampleReactComponent({ name }) { 5 | const hi = ['Hello', name]; 6 | return React.createElement('div', { id: 'example' }, hi.join(' ')); 7 | } 8 | 9 | ExampleReactComponent.propTypes = { 10 | name: PropTypes.string, 11 | }; 12 | ExampleReactComponent.defaultProps = { 13 | name: '', 14 | }; 15 | 16 | module.exports = ExampleReactComponent; 17 | -------------------------------------------------------------------------------- /test/components/HypernovaExampleReact.js: -------------------------------------------------------------------------------- 1 | const { renderReact } = require('../../'); 2 | const Component = require('./ExampleReactComponent.js'); 3 | 4 | module.exports = renderReact('HypernovaExampleReact.js', Component); 5 | -------------------------------------------------------------------------------- /test/init.js: -------------------------------------------------------------------------------- 1 | import sinon from 'sinon-sandbox'; 2 | 3 | afterEach(() => { 4 | sinon.restore(); 5 | }); 6 | -------------------------------------------------------------------------------- /test/renderReact-test.js: -------------------------------------------------------------------------------- 1 | import jsdom from 'jsdom'; 2 | import { assert } from 'chai'; 3 | import sinon from 'sinon'; 4 | import ReactDOM from 'react-dom'; 5 | import ifReact from 'enzyme-adapter-react-helper/build/ifReact'; 6 | 7 | import ExampleReactComponent from './components/ExampleReactComponent'; 8 | import { renderReact } from '..'; 9 | 10 | describe('renderReact', () => { 11 | let result; 12 | beforeEach(() => { 13 | result = renderReact('ExampleReactComponent', ExampleReactComponent)({ name: 'Desmond' }); 14 | }); 15 | 16 | it('exists', () => { 17 | assert.isFunction(renderReact); 18 | assert.equal(renderReact.length, 2); 19 | }); 20 | 21 | it('has correct markup on server', () => { 22 | assert.isString(result); 23 | assert.match(result, /Hello Desmond/); 24 | }); 25 | 26 | ifReact('>= 16', it, it.skip)('calls hypernova.client (hydrate method)', (done) => { 27 | jsdom.env(result, (err, window) => { 28 | if (err) { 29 | done(err); 30 | return; 31 | } 32 | 33 | global.window = window; 34 | global.document = window.document; 35 | 36 | const hydrateMethod = sinon.spy(ReactDOM, 'hydrate'); 37 | 38 | // Calling it again for the client. 39 | renderReact('ExampleReactComponent', ExampleReactComponent); 40 | 41 | assert(hydrateMethod.calledOnce); 42 | 43 | delete global.window; 44 | delete global.document; 45 | 46 | hydrateMethod.restore(); 47 | 48 | done(); 49 | }); 50 | }); 51 | 52 | it('calls hypernova.client (render method)', (done) => { 53 | jsdom.env(result, (err, window) => { 54 | if (err) { 55 | done(err); 56 | return; 57 | } 58 | 59 | const sandbox = sinon.createSandbox(); 60 | if (ReactDOM.hydrate) { 61 | sandbox.stub(ReactDOM, 'hydrate').value(undefined); 62 | } 63 | 64 | const renderMethod = sinon.spy(ReactDOM, 'render'); 65 | 66 | global.window = window; 67 | global.document = window.document; 68 | 69 | // Calling it again for the client. 70 | renderReact('ExampleReactComponent', ExampleReactComponent); 71 | 72 | assert(renderMethod.calledOnce); 73 | 74 | sandbox.restore(); 75 | 76 | delete global.window; 77 | delete global.document; 78 | 79 | done(); 80 | }); 81 | }); 82 | }); 83 | -------------------------------------------------------------------------------- /test/renderReactStatic-test.js: -------------------------------------------------------------------------------- 1 | import { assert } from 'chai'; 2 | 3 | import ExampleReactComponent from './components/ExampleReactComponent'; 4 | import { renderReactStatic } from '..'; 5 | 6 | describe('renderReactStatic', () => { 7 | let result; 8 | beforeEach(() => { 9 | result = renderReactStatic('ExampleReactComponent', ExampleReactComponent)({ name: 'Zack' }); 10 | }); 11 | 12 | it('exists', () => { 13 | assert.isFunction(renderReactStatic); 14 | assert.equal(renderReactStatic.length, 2); 15 | }); 16 | 17 | it('has correct markup on server', () => { 18 | assert.isString(result); 19 | assert.match(result, /Hello Zack/); 20 | }); 21 | }); 22 | --------------------------------------------------------------------------------