Class: SvelteOnRails::Lib::FallbackRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/svelte_on_rails/lib/fallback_renderer.rb

Class Method Summary collapse

Class Method Details

.render(paths, props, debug) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/svelte_on_rails/lib/fallback_renderer.rb', line 9

def self.render(paths, props, debug)
  cnf = SvelteOnRails::Configuration.instance
  mn = cnf.manifest(debug, paths[:name])
  manifest = mn[paths[:path]]

  if manifest.nil?
    raise "[SOR] #{paths[:file_basename]}: ERROR: No manifest found!\nAvailable keys in current manifest.json are:\n\n+++\n • #{cnf.manifest.keys.join("\n • ")}\n+++\n."
  end

  cmd = [
    cnf.node_bin_path,
    cnf.rails_root.join('node_modules/@csedl/svelte-on-rails/src/ssr/render.js'),
    cnf.assets_folder_path.join(manifest['file'])
  ].join(' ')

  stdout, stderr, status = Open3.capture3(
    cmd,
    stdin_data: props.to_json,
    chdir: cnf.rails_root,
  )

  ary = stdout.split('$$ResponseSeparator$$')

  js = JSON.parse(ary.second || '{}')
  if js['status'] == 'SUCCESS'

    htm = js['html']
            .gsub('<!--[-->', '')
            .gsub('<!--]-->', '')
            .gsub('<!---->', '')

    {
      success: true,
      html: htm,
      head: js['head'],
    }
  else

    utils = SvelteOnRails::Lib::Utils
    utils.error_log(
      "RENDER #{paths[:file_basename]}.svelte",
      [
        "command:\n#{cmd}",
        "stdout:\n#{stdout.split('$$ResponseSeparator$$').first.strip}",
        "stderr:\n#{stderr.strip}",
      ]
    )

    { success: false }
  end

end