Class: RailsReactViews::Prerenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_react_views/prerenderer.rb

Class Method Summary collapse

Class Method Details

.build_context(props: {}, view:, path:) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/rails_react_views/prerenderer.rb', line 20

def build_context(props: {}, view:, path:)
  {
    view: view,
    path: base_path(path),
    props: props
  }
end

.decode_text(text) ⇒ Object



33
34
35
36
# File 'lib/rails_react_views/prerenderer.rb', line 33

def decode_text(text)
  decoded_text = Base64.strict_decode64(text)
  Zlib::Inflate.inflate(decoded_text).force_encoding('utf-8')
end

.encode_text(text) ⇒ Object



28
29
30
31
# File 'lib/rails_react_views/prerenderer.rb', line 28

def encode_text(text)
  gzipped_text = Zlib::Deflate.deflate(text)
  Base64.strict_encode64(gzipped_text)
end

.prerender(view: '', path: '', props: {}, context: nil, context_json: nil, cache: true) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rails_react_views/prerenderer.rb', line 6

def prerender(view: '', path: '', props: {}, context: nil, context_json: nil, cache: true)
  context_json ||= (context || build_context(props: props, view: view, path: path)).to_json

  if cache && Rails.configuration.action_controller.perform_caching
    context_digest = Digest::MD5.hexdigest(context_json)

    Rails.cache.fetch([RailsReactViews::digest, :rails_react_views, :prerenderer, :prerender, context_digest]) do
      process_prerender(context_json)
    end
  else
    process_prerender(context_json)
  end
end

.split_html(html = '') ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rails_react_views/prerenderer.rb', line 38

def split_html(html = '')
  head_start = html.index('<head>')
  head_end = html.index('</head>')
  head = (head_start && head_end ? html[head_start + 6..head_end - 1] : '').html_safe

  body_start = html.index('<body>')
  body_end = html.index('</body>')
  body = (body_start && body_end ? html[body_start + 6..body_end - 1] : '').html_safe

  { head: head, body: body }
end