Skip to content

feat(ServerRendering) support per-request renderers #559

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

Merged
merged 6 commits into from
Apr 5, 2017

Conversation

rmosolgo
Copy link
Member

@rmosolgo rmosolgo commented Jun 28, 2016

Currently, a renderer is "checked out" for each call to react_component. However, if you want some state in that context, it's inefficient: you have to load the state before each call, then clear it after each call.

This PR adds support for one-renderer-per-request, so you can load the JS state before rendering the view, then unload it afterwards.

You could do like this:

class ThingsController < ApplicationController 
  # This around-action is provided by react-rails: 
  per_request_react_renderer

  def show 
     # `react_rails_prerenderer` is assigned by react-rails 
     js_context = react_rails_prerenderer.context 
     js_context.eval(setup_code)
     render :show 
     js_context.eval(teardown_code)
  end 
end 

Of course, you could further generalize this solution with before_/after_/around_actions

Todo

  • End-to-end test using __prerenderer.context
  • Improve the API?

@rmosolgo rmosolgo force-pushed the per-request-js-ctx branch from 727a8d6 to 841da3a Compare April 5, 2017 14:28
@rmosolgo rmosolgo force-pushed the per-request-js-ctx branch from 841da3a to f7544d3 Compare April 5, 2017 15:51
@rmosolgo rmosolgo merged commit 79101ff into 2.0-dev Apr 5, 2017
@rmosolgo rmosolgo deleted the per-request-js-ctx branch April 5, 2017 18:44
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

Successfully merging this pull request may close these issues.

2 participants