-
Notifications
You must be signed in to change notification settings - Fork 755
Prerender fails with react and jQuery loaded from npm #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Update: I found a workaround to make prerendering, JEST based testing and CommonJS
//= require jquery
//= require jquery_ujs
//= require react_ujs
//= require components
React = require('react');
// Explicitely ONLY require components that are used inside Rails helpers
// NOT SubComponents
DemoComponent = require('./components/DemoComponent'); In the
var React = require('react');
var uniq = require('uniq');
var SubComponent = require('../subcomponents/SubComponent');
var DemoComponent = React.createClass({
render: function() {
console.log("Uniq: ", uniq([1,2,2,3,3,3]));
return(
<div>
...
<SubComponent />
</div>
);
}
});
module.exports = DemoComponent And subcomponents defined in
var React = require('react');
var SubComponent = React.createClass({
render: function() {
return(
<div>
Hello from SubComponent
</div>
);
}
});
module.exports = SubComponent JEST specs are defined for top level components and subcomponents in jest.dontMock('../../subcomponents/SubComponent');
describe('SubComponent', function() {
it('should tell us it is a sub component', function() {
var React = require('react/addons');
var TestUtils = React.addons.TestUtils;
var SubComponent = require('../../subcomponents/SubComponent');
var subComponent = TestUtils.renderIntoDocument(<SubComponent />);
expect(subComponent.getDOMNode().textContent).toBe('Hello from SubComponent');
});
}); With a {
"name": "rexa-webapp",
"devDependencies" : {
"browserify": "~8.1.0",
"browserify-incremental": "^1.5.0",
"reactify": "^1.1.0",
"uniq": "^1.0.1",
"react": "^0.12.0",
"react-tools": "^0.12.1",
"jest-cli": "^0.2.0"
},
"scripts": {
"test": "node ./node_modules/jest-cli/bin/jest.js"
},
"jest": {
"rootDir": "./app/assets/javascripts/components",
"scriptPreprocessor": "<rootDir>/__tests__/preprocessor.js",
"moduleFileExtensions": [ "js", "jsx"],
"unmockedModulePathPatterns": [
"react"
],
"testFileExtensions": ["js", "jsx"],
"testPathIgnorePatterns": [ "preprocessor.js" ]
},
"license": "MIT",
"engines": {
"node": ">= 0.10"
}
} all seems to work fine. Inside the browser components work properly w/ or w/o prerendering. JEST can be used to test all components, and Besides the fact that this is a little more manually work to be done, are there any downsides in this approach, that I do not see yet? |
@janroesner I'm using the same approach to have |
In order to have CommonJS
require()
and JEST work, I followed the setup described here. So inapp/assets/javascripts
I created anapplication.js
with:Besides a
components.js
with:and a
npm_jquery.js
with:When I render my component in my view with prerendering enabled like so:
I receive the following error:
React::Renderer::PrerenderError in Pages#index
Showing /Users/jondoe/react/rails/test-app/app/views/pages/index.html.erb where line #2 raised:
Encountered error "Error: Cannot find module 'function (document, window) {
// jQuery is optional. Use it to support legacy browsers.
var $ = (typeof window.jQuery !== 'undefined') && window.jQuery;
// create the namespace
window.ReactRailsUJS = {
CLASS_NAME_ATTR: 'data-react-class', ...
Rendering w/o
prerender
set works totally fine and yields expected results. Is this a problem with my setup, or did I encounter a bug?The text was updated successfully, but these errors were encountered: