Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

reset prererendering log at beginning of prerender: #176

Closed
catmando opened this issue Oct 24, 2016 · 4 comments
Closed

reset prererendering log at beginning of prerender: #176

catmando opened this issue Oct 24, 2016 · 4 comments
Assignees

Comments

@catmando
Copy link
Contributor

catmando commented Oct 24, 2016

this one line fixes it (console.history = [])

module React
  module IsomorphicHelpers
    def self.included(base)
      base.extend(ClassMethods)
    end

    if RUBY_ENGINE != 'opal'
      def self.load_context(ctx, controller, name = nil)
        puts "************************** React Server Context Initialized #{name} *********************************************"
        @context = Context.new("#{controller.object_id}-#{Time.now.to_i}", ctx, controller, name)
      end
    else
      def self.load_context(unique_id = nil, name = nil)
        # can be called on the client to force re-initialization for testing purposes
        if !unique_id || !@context || @context.unique_id != unique_id
          if on_opal_server?
           `console.history = []` rescue nil  # <<<<<<<<<<----- add this line
            message = "************************ React Prerendering Context Initialized #{name} ***********************"
          else
            message = "************************ React Browser Context Initialized ****************************"
          end
          log(message)
          @context = Context.new(unique_id)
        end
        @context
      end
    end

Not sure why react-rails is not doing this for us... it could be because we screw around with the rendering mechanism to add our own custom footers...

@zetachang
Copy link
Member

Is there more context on this bug? I need some steps to reproduce it.

@catmando
Copy link
Contributor Author

just make a simple component, load in browser, then load again

You will see that every time you load you keep all the old log statements from the last pre-rerenders.

For production apps it makes pre-rerendering unworkable, as eventually the console log array chews up a significant amount of memory.

The above fix simple clears the console.history (which react-rails uses to simulate the console) at the beginning of each pre-rerender

@zetachang
Copy link
Member

Turns our this behavior happens in v8 runtime and not the default runtime by execjs. An issue is reported in react-rails.

But this also leads to another concern, the console.history is not the only object persist between requests, the whole context is. So if the code try to store stuff in global variable and rely on it, prerendering will potentially break the app.

@catmando
Copy link
Contributor Author

this is true, but... to resolve this issue here is what I have tried to do (may have problems)

There is a class method in React::IsomorphicHelpers called before_first_mount, classes can use this method to define a "boot" time initialization that will be (should be?) called as each rendering cycle starts. The method is called on both prerendering, and on client, so applications can do whatever initialization needs to be done. ReactiveRecord uses this for example to clear its cached db objects.

Its not a good situation however, and we should try to figure out why it can't be fixed properly in the engine.

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

No branches or pull requests

2 participants