module Preact module ViewHelper def self.included(base) base.include Isomorfeus::AssetManager::ViewHelper end def cached_mount_component(component_name, props = {}, skip_ssr: false, use_ssr: false, refresh: false) key = "#{Isomorfeus.current_user}#{component_name}#{props}" if !Isomorfeus.development? && !refresh render_result, @_ssr_styles, status = component_cache.fetch(key) Isomorfeus.ssr_response_status = status return render_result if render_result end render_result = mount_component(component_name, props, asset_key, skip_ssr: skip_ssr, use_ssr: use_ssr) status = ssr_response_status component_cache.store(key, render_result, ssr_styles, status) if status >= 200 && status < 300 render_result end def mount_component(component_name, props = {}, skip_ssr: false, use_ssr: false) ssr_start_time = Time.now if Isomorfeus.development? @ssr_styles = nil render_result = "
" render_result << (rendered_tree ? rendered_tree : "SSR didn't work") else if Isomorfeus.respond_to?(:current_user) && Isomorfeus.current_user && !Isomorfeus.current_user.anonymous? render_result << " data-iso-usids=#{Oj.dump(Isomorfeus.current_user.sid.to_s, mode: :strict)}" end render_result << " data-iso-nloc='#{props[:locale]}'>" end render_result << '
' if Isomorfeus.server_side_rendering && !skip_ssr render_result = "\n" << render_result puts "Preact::ViewHelper Server Side Rendering took ~#{((Time.now - ssr_start_time)*1000).to_i}ms" if Isomorfeus.development? end render_result end def ssr_response_status Isomorfeus.ssr_response_status end def ssr_styles @_ssr_styles || '' end private def component_cache @_component_cache ||= Isomorfeus.component_cache_init_block.call end def ssr_mod @_ssr_mod ||= Opal.compile(File.read(File.expand_path(File.join(File.dirname(__FILE__), 'ssr.rb'))), { use_strict: true }) end def top_level_mod @_top_level_mod ||= Opal.compile(File.read(File.expand_path(File.join(File.dirname(__FILE__), 'top_level_ssr.rb'))), { use_strict: true }) end end end