Skip to content

Commit

Permalink
move server bundle modification check code to ServerRenderingPool
Browse files Browse the repository at this point in the history
  • Loading branch information
robwise committed Nov 3, 2015
1 parent 3224d05 commit 534c9af
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 30 deletions.
16 changes: 2 additions & 14 deletions app/helpers/react_on_rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,8 @@ def next_react_component_index
def server_rendered_react_component_html(options, props_string, react_component_name, data_variable_name, dom_id)
return ["", ""] unless prerender(options)

# Check if server bundle timestamp has changed.
# If it has, then clear the connection pool.
# Only for development.
reload_server_bundle_if_modified
# Make sure that we use up-to-date server-bundle
ReactOnRails::ServerRenderingPool.reset_pool_if_server_bundle_was_modified

wrapper_js = <<-JS
(function() {
Expand All @@ -163,16 +161,6 @@ def server_rendered_react_component_html(options, props_string, react_component_
raise ReactOnRails::ServerRenderingPool::PrerenderError.new(react_component_name, props_string, err)
end

def reload_server_bundle_if_modified
return unless ReactOnRails.configuration.reload_server_js_every_request
file_mtime = File.mtime(ReactOnRails.configuration.server_bundle_js_file)
@@server_bundle_timestamp ||= file_mtime
if @@server_bundle_timestamp != file_mtime
ReactOnRails::ServerRenderingPool.reset_pool
@@server_bundle_timestamp = file_mtime
end
end

def trace(options)
options.fetch(:trace) { ReactOnRails.configuration.trace }
end
Expand Down
16 changes: 0 additions & 16 deletions lib/generators/react_on_rails/generator_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,13 @@ def keep_file(destination)
# source_root, this symlinks a file in destination_root to a file also in
# destination_root.
def symlink_dest_file_to_dest_file(target, link)
# target = "app/assets/fonts"
# link = "client/assets/fonts"
target_pathname = Pathname.new(File.join(destination_root, target))
link_pathname = Pathname.new(File.join(destination_root, link))

link_directory = link_pathname.dirname
link_basename = link_pathname.basename
target_relative_path = target_pathname.relative_path_from(link_directory)

puts "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
# puts "link: #{link}"
# puts "target: #{target}"
# puts "destination_root: #{destination_root}"
# puts "target_pathname: #{target_pathname}"
# puts "link_pathname: #{link_pathname}"
# puts "link_diretory: #{link_directory}"
# puts "target_basename: #{target_basename}"
# puts "link_basename: #{link_basename}"
# puts "target_directory: #{target_directory}"
# puts "link_relative_path: #{link_relative_path}"
# puts "target_relative_path: #{target_relative_path}"
puts "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"

`cd #{link_directory} && ln -s #{target_relative_path} #{link_basename}`
end

Expand Down
9 changes: 9 additions & 0 deletions lib/react_on_rails/server_rendering_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ def self.reset_pool
@js_context_pool = ConnectionPool.new(options) { create_js_context }
end

def self.reset_pool_if_server_bundle_was_modified
return unless ReactOnRails.configuration.reload_server_js_every_request
file_mtime = File.mtime(ReactOnRails.configuration.server_bundle_js_file)
@server_bundle_timestamp ||= file_mtime
return if @server_bundle_timestamp == file_mtime
ReactOnRails::ServerRenderingPool.reset_pool
@server_bundle_timestamp = file_mtime
end

class PrerenderError < RuntimeError
def initialize(component_name, props, js_message)
message = ["Encountered error \"#{js_message}\" when prerendering #{component_name} with #{props}",
Expand Down

0 comments on commit 534c9af

Please sign in to comment.