Skip to content

Commit

Permalink
Added options to disable cache so we don't need to restart server.js
Browse files Browse the repository at this point in the history
whenever there are JS changes
  • Loading branch information
Jim committed Dec 29, 2016
1 parent 576d949 commit 50f66c6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ reactRender({
// markup. Defaults to false.
toStaticMarkup: true

// A flag indicating if you wish to disable caching for components. This is
// especially useful in development. Defaults to false.
noCache: false

}, function(err, markup) {
if (err) throw err;

Expand Down
5 changes: 5 additions & 0 deletions lib/Component.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var React = require('react');
var ReactDOMServer = require('react-dom/server');
var decache = require('decache');

var Component = function Component(opts) {
this.opts = opts;
Expand Down Expand Up @@ -37,6 +38,10 @@ Component.prototype.getComponent = function getComponent(cb) {
}

try {
if (this.opts.noCache) {
decache(this.path);
}

this.component = require(this.path);
if (this.component && typeof this.component === 'object' && this.component.default) {
this.component = this.component.default
Expand Down
15 changes: 10 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ var components = {
};

var reactRender = function reactRender(opts, cb) {
var _opts = _.pick(opts, 'component', 'path');
var _opts = _.pick(opts, 'component', 'path', 'noCache');

var component = components.find(_opts);

if (!component) {
var component;
if (_opts.noCache) {
component = new Component(_opts);
components.add(component);
} else {
component = components.find(_opts);

if (!component) {
component = new Component(_opts);
components.add(component);
}
}

var props = opts.props;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"chai": "^3.0.0",
"mocha": "^2.2.5",
"react": "^0.14.2",
"react-dom": "^0.14.2"
"react-dom": "^0.14.2",
"decache": "^4.1.0"
},
"scripts": {
"test": "mocha"
Expand Down

0 comments on commit 50f66c6

Please sign in to comment.