Skip to content
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

ReactOnRails is not defined when including application.js at bottom of body #71

Closed
hisapy opened this issue Oct 26, 2015 · 6 comments
Closed

Comments

@hisapy
Copy link

hisapy commented Oct 26, 2015

When including application.js at bottom of body like this:

<body>
 <!-- some other stuff here (layouts/application.html.erb) -->
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true, 'data-turbolinks-eval' => false %>
</body>

and using react_component helper to render a component in a view (i.e.: /admin/purchases/new)

<%= react_component("PurchaseForm", {}, prerender: false, trace: true, generator_function: false) %>

I get a Uncaught ReferenceError: ReactOnRails is not defined. Obviously this is because the Javascript written by the helper is executed when the DOM is loading, and before the application.js is included.

In order to avoid the error what I've done is the following:

<body>
  <!-- some other stuff here (layouts/application.html.erb) -->
  <div id="reactRoot"></div>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true, 'data-turbolinks-eval' => false %>
  <%= content_for :react_components %>
</body>

and in the view:

<% content_for(:react_components) do
    concat react_component("ClientApp", {}, id: 'reactRoot', prerender: false, trace: true, generator_function: false)
  end %>

I just wanted to share my experience using this gem... Is there any better approach for this? If not maybe you'd like to include this strategy in the README.

@justin808
Copy link
Member

@hisapy application.js should be included in the head of the document. Any reason you're placing it in the body?

@justin808
Copy link
Member

Feel free to re-open this ticket if you're sure that there's a case for putting application.js in the body.

@hisapy
Copy link
Author

hisapy commented Oct 26, 2015

Actually, it seems no there is need anymore to move it out from the head.

@rupurt
Copy link
Contributor

rupurt commented Dec 1, 2015

@justin808 Yahoo recommends loading scripts at the bottom of the page to avoid blocking parallel downloads. How do you feel about attaching the components once the JS has loaded?

@justin808
Copy link
Member

@rupurt we did a little research and concluded that the Yahoo advice is no longer worthwhile due to typical caching of the bundled JS file as well as the use of turbolinks. That being said, if you can create a simple demo of a performance gain, we'd be very happy to change the way we're organizing the JS code.

@justin808
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants