svelte-on-rails 14.4.0 → 14.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f41c1763ffa52bdf71790ca32a11bcd6268a44d9cd0d5ada872dc56d70ee954b
4
- data.tar.gz: 02ead98b87f000e4e58f1b1b8e51578a901faea729ba28a7c505e69f5de15842
3
+ metadata.gz: d12a749b2285a777b0f61a381785f60a076e089b19cf1849d71ca4481460546a
4
+ data.tar.gz: 4c8d743ad0a80a6e64a4c3273e0c0b0360faf01f2f5c9497f8eb9aee4cc99540
5
5
  SHA512:
6
- metadata.gz: 1e2e47f05ac02bed21ebc112f3cc6803313f8dfbedade0d59a7da1904cc6d3e00195501d113f2f6acb6fe47d70b54a1717bde927e13d41a90df29e6128b59773
7
- data.tar.gz: 80df87c8d6b31be2cd8cfaa142c19d379d58b12327810271ffcb4766bab8c7f62eb5331f4b87ebbe0c32fc1b60db294cf63198d83bdfd9c61e0019b486a3f068
6
+ metadata.gz: cc42096e49e80ef471749fe347e79ad5caef7c0824ddd38571dbf898936c8e93130a595073ce654f4380de5834070017c6dbda1c3dc8b55f6495e4eb08c96214
7
+ data.tar.gz: 14887f7e4edbe5c2078378cdb45f735099bc2cced66f1a74d25b800ce2a53ec7e906572976f41098f1846041f3522f5579e2b5a7af4d0b889e7d80317b4be62e
@@ -7,8 +7,8 @@ require "svelte_on_rails/ssr_server"
7
7
 
8
8
  require "svelte_on_rails/railtie" if defined?(Rails)
9
9
 
10
- require "svelte_on_rails/renderer/renderer"
11
10
 
11
+ require "svelte_on_rails/lib/fallback_renderer"
12
12
  require "svelte_on_rails/lib/utils"
13
13
  require "svelte_on_rails/lib/metrics"
14
14
  require "svelte_on_rails/lib/log_patch"
@@ -33,7 +33,6 @@ require 'generators/showcase_generator'
33
33
 
34
34
  require 'digest/xxhash'
35
35
  require 'benchmark'
36
- #require 'faraday'
37
36
 
38
37
  module SvelteOnRails
39
38
  class << self
@@ -0,0 +1,89 @@
1
+ module SvelteOnRails
2
+ module Lib
3
+ class FallbackRenderer
4
+
5
+ require 'open3'
6
+ require 'base64'
7
+ require 'json'
8
+
9
+ def self.render(paths, props, debug)
10
+ utils = SvelteOnRails::Lib::Utils
11
+ cnf = SvelteOnRails::Configuration.instance
12
+ mn = cnf.manifest(debug, paths[:name])
13
+ manifest = mn[paths[:path]]
14
+
15
+ if manifest.nil?
16
+ 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."
17
+ end
18
+
19
+ cmd = [
20
+ cnf.node_bin_path,
21
+ cnf.rails_root.join('node_modules/@csedl/svelte-on-rails/src/ssr/render.js'),
22
+ cnf.assets_folder_path.join(manifest['file'])
23
+ ].join(' ')
24
+
25
+ stdout, stderr, status = Open3.capture3(
26
+ cmd,
27
+ stdin_data: props.to_json,
28
+ chdir: cnf.rails_root,
29
+ )
30
+
31
+ ary = stdout.split('$$ResponseSeparator$$')
32
+
33
+ js = JSON.parse(ary.second || {})
34
+ if js['status'] == 'SUCCESS'
35
+
36
+ htm = js['html'].delete_prefix("<!--[-->").delete_suffix("<!--]-->")
37
+
38
+ {
39
+ success: true,
40
+ html: htm,
41
+ head: js['head'],
42
+ }
43
+ else
44
+
45
+ utils = SvelteOnRails::Lib::Utils
46
+ utils.error_log(
47
+ "RENDER #{paths[:file_basename]}.svelte",
48
+ [
49
+ "command:\n#{cmd}",
50
+ "stdout:\n#{stdout.split('$$ResponseSeparator$$').first.strip}",
51
+ "stderr:\n#{stderr.strip}",
52
+ ]
53
+ )
54
+
55
+ { success: false }
56
+ end
57
+
58
+ # unless ary.length == 2 || (JSON.parse(ary.second) rescue true)
59
+ # utils = SvelteOnRails::Lib::Utils
60
+ # utils.error_log(
61
+ # "RENDER #{paths[:file_basename]}.svelte",
62
+ # [
63
+ # "command:\n#{cmd}",
64
+ # "stdout:\n#{stdout}",
65
+ # "stderr:\n#{stderr}",
66
+ # ]
67
+ # )
68
+ # return { success: false }
69
+ # end
70
+
71
+ # begin
72
+ #
73
+ # raw_response = JSON.parse(ary[1])
74
+ #
75
+ # htm = raw_response['html'].delete_prefix("<!--[-->").delete_suffix("<!--]-->")
76
+ #
77
+ # return {
78
+ # success: true,
79
+ # html: htm,
80
+ # head: raw_response['head'],
81
+ # }
82
+ #
83
+ # rescue => e
84
+ # raise "MY_ERR => #{e}\n\n+++\n#{e.backtrace.join("\n")}\n+++"
85
+ # end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -7,6 +7,8 @@ module SvelteOnRails
7
7
 
8
8
  if !cnf.build_status['passed']
9
9
  'SVELTE BUILD FAILED!'
10
+ elsif m[:render_failed]
11
+ 'SVELTE RENDER FAILED!'
10
12
  else
11
13
 
12
14
  w = (cnf.watch_changes? ? cnf.build_status['warnings_count'] : nil)
@@ -93,6 +93,20 @@ module SvelteOnRails
93
93
  0.0
94
94
  end
95
95
  end
96
+
97
+ def self.error_log(title, messages)
98
+ sep_char = '='
99
+ sep_line = sep_char*100
100
+ inner_sep = '-'*100
101
+ puts sep_line
102
+ puts "ERROR: #{title}"
103
+ puts sep_line
104
+ puts messages.join("\n#{inner_sep}\n")
105
+ puts sep_line
106
+ puts "END #{title.downcase}"
107
+ puts sep_line
108
+ $stdout.flush
109
+ end
96
110
  end
97
111
  end
98
112
  end
@@ -23,17 +23,33 @@ module SvelteOnRails
23
23
 
24
24
  if !@conf.manifest_json_path.exist? ||
25
25
  !@conf.dev_module_map_path.exist? ||
26
- @conf.build_status['mtime'].to_f != @utils.manifest_file_mtime
26
+
27
+ @conf.build_status['mtime'].to_f != @utils.manifest_file_mtime ||
28
+ # if a developer has run build from the console, we must update the build-status object
29
+ # this is importand for having reliable error-messages
30
+
31
+ (!@conf.build_status['passed'] && !@conf.request_metrics[:build_failed])
32
+ # when build has failed the developer sees the error message
33
+ # at this point it is importand to see the build-error in the logs
34
+ # but only ONCE for a request, so we check metrics too
35
+
27
36
  run_vite_build
37
+
38
+ return unless @conf.build_status['passed']
28
39
  end
29
40
 
30
- validate_options
41
+ # now we are sure to have a valid manifest
42
+ # and are able to collect the dependend source files,
43
+ # check theyr mtime
44
+ # and run a build if something has changed (is only the case if the build above not was run)
31
45
  ensure_valid_component_and_manifest!(component)
32
-
33
46
  if @conf.component_mtime_changed?(component_paths[:path], debug?, component_paths[:file_basename])
34
47
  run_vite_build
35
48
  end
36
49
 
50
+ # finally, validate the given options
51
+ validate_options
52
+
37
53
  end
38
54
 
39
55
  if @conf.configs[:cache_warmer] && caching
@@ -118,9 +134,12 @@ module SvelteOnRails
118
134
  @cached_content.html_safe
119
135
  else
120
136
  msg = "Rendered"
137
+ @skip_caching = false
121
138
  @conf.debug_log(debug?, component_paths[:name], "rendered and stored to cache (key: «#{cache_key}»)") do
122
139
  r = view_context.instance_eval(&block)
123
- @conf.redis_instance.set(cache_key, r)
140
+ unless @skip_caching
141
+ @conf.redis_instance.set(cache_key, r)
142
+ end
124
143
  r
125
144
  end
126
145
  end
@@ -140,7 +159,15 @@ module SvelteOnRails
140
159
 
141
160
  def set_request_metrics(action_key)
142
161
 
143
- if @conf.watch_changes? && ![:build, :build_failed, :from_cache, :empty, :rendered].include?(action_key)
162
+ if @conf.watch_changes? &&
163
+ ![
164
+ :build,
165
+ :build_failed,
166
+ :render_failed,
167
+ :from_cache,
168
+ :empty,
169
+ :rendered
170
+ ].include?(action_key)
144
171
  raise "[SOR] set_request_metrics: invalid action_key"
145
172
  end
146
173
 
@@ -106,20 +106,27 @@ module SvelteOnRails
106
106
  shorten_logfile
107
107
 
108
108
  raw_response = parse_body_json(response)
109
- htm = if raw_response['error']
110
- <<~HTML
111
- <div style="border: 1px solid red; padding: 5px; margin: 5px;">
112
- <strong style="color: red;">#{raw_response['error']}</strong>
113
- <p>#{raw_response['details']}</p>
114
- </div>/
115
- HTML
116
- else
117
- raw_response['html'].to_s.delete_prefix("<!--[-->").delete_suffix("<!--]-->")
118
- end
119
- {
120
- html: htm,
121
- head: raw_response['head'].to_s,
122
- }
109
+ if raw_response['status'] == 'SUCCESS'
110
+ htm = raw_response['html'].to_s.delete_prefix("<!--[-->").delete_suffix("<!--]-->")
111
+
112
+ {
113
+ success: true,
114
+ html: htm,
115
+ head: raw_response['head'].to_s,
116
+ }
117
+ else
118
+ utils = SvelteOnRails::Lib::Utils
119
+ utils.error_log(
120
+ "Render #{component[:file_basename]}.svelte",
121
+ [
122
+ raw_response['errorLog'].join("\n")
123
+ ]
124
+ )
125
+ {
126
+ success: false,
127
+ }
128
+ end
129
+
123
130
  end
124
131
 
125
132
  def parse_body_json(response_string)
@@ -16,7 +16,7 @@ module SvelteOnRails
16
16
 
17
17
  if support.ssr?
18
18
  if config.watch_changes? && !config.build_status['passed']
19
- render_build_failed_tag(support, wrapper_html, props)
19
+ build_failed_tag(support, wrapper_html, props)
20
20
  elsif caching
21
21
  support.set_request_metrics(:from_cache)
22
22
  support.render_cached(self) do
@@ -38,28 +38,22 @@ module SvelteOnRails
38
38
 
39
39
  if support.ssr?
40
40
 
41
- support.set_request_metrics(:rendered)
42
-
43
- ssr_result = if !support.conf.build_status['passed']
44
- wrapper_style = 'color: white; background-color: red; padding: 10px; border-radius: 10px; display:inline-block; font-size:22px;'
45
- html = <<~HTML
46
- <strong style="#{wrapper_style}">
47
- <p style="margin:4px;">Vite build failed.</p>
48
- <p style="margin:4px;">Please run «npm run build:ssr» to fix.</p>
49
- </strong>
50
- HTML
51
- {
52
- html: html.html_safe,
53
- head: ''.html_safe
54
- }
55
- elsif SvelteOnRails::SsrServer.instance.alive?
41
+ ssr_result = if SvelteOnRails::SsrServer.instance.alive?
56
42
  SvelteOnRails::SsrServer.instance.render(support.component_paths, props, support.debug?)
57
43
  else
58
- SvelteOnRails::Renderer.render(support.component_paths, props, support.debug?)
44
+ SvelteOnRails::Lib::FallbackRenderer.render(support.component_paths, props, support.debug?)
59
45
  end
60
- content_tag(:div, support.tag_attributes(html_options, props)) do
61
- concat(ssr_result[:head].html_safe)
62
- concat(ssr_result[:html].html_safe)
46
+
47
+ if ssr_result[:success]
48
+ support.set_request_metrics(:rendered)
49
+ content_tag(:div, support.tag_attributes(html_options, props)) do
50
+ concat(ssr_result[:head].html_safe)
51
+ concat(ssr_result[:html].html_safe)
52
+ end
53
+ else
54
+ support.set_request_metrics(:render_failed)
55
+ support.instance_variable_set(:@skip_caching, true)
56
+ render_failed_tag(support, html_options, props)
63
57
  end
64
58
 
65
59
  else
@@ -76,26 +70,45 @@ module SvelteOnRails
76
70
  content_tag(:div, support.tag_attributes(html_options, props)) {}
77
71
  end
78
72
 
79
- def render_build_failed_tag(support, html_options, props)
73
+ def build_failed_tag(support, html_options, props)
80
74
 
81
- support.set_request_metrics(:empty)
75
+ # support.set_request_metrics(:empty)
82
76
 
83
77
  content_tag(:div, support.tag_attributes(html_options, props)) do
84
- style = [
85
- 'background-color:red',
86
- 'color:white',
87
- 'padding:8px',
88
- 'border-radius:8px',
89
- 'font-size:16px',
90
- 'font-weight:bold',
91
- 'border:3px solid white'
92
- ]
93
- content_tag(:span, style: style.join(';')) do
78
+ content_tag(
79
+ :span,
80
+ class: 'svelte-on-rails-render-error',
81
+ style: failed_tag_style
82
+ ) do
94
83
  'BUILD FAILED'
95
84
  end
96
85
  end
97
86
  end
98
87
 
88
+ def render_failed_tag(support, html_options, props)
89
+ content_tag(:div, support.tag_attributes(html_options, props)) do
90
+ content_tag(
91
+ :span,
92
+ class: 'svelte-on-rails-render-error',
93
+ style: failed_tag_style
94
+ ) do
95
+ 'RENDER FAILED'
96
+ end
97
+ end
98
+ end
99
+
100
+ def failed_tag_style
101
+ [
102
+ 'background-color:red',
103
+ 'color:white',
104
+ 'padding:8px',
105
+ 'border-radius:8px',
106
+ 'font-size:16px',
107
+ 'font-weight:bold',
108
+ 'border:3px solid white'
109
+ ].join(';')
110
+ end
111
+
99
112
  def calling_view_dir
100
113
  rails_views_path = Rails.root.join('app', 'views').to_s
101
114
  raw_path_full = caller_locations.find do |loc|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svelte-on-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 14.4.0
4
+ version: 14.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Sedlmair
@@ -99,6 +99,7 @@ files:
99
99
  - lib/svelte_on_rails/installer/utils.rb
100
100
  - lib/svelte_on_rails/lib/cache_warmer.rb
101
101
  - lib/svelte_on_rails/lib/development_utils.rb
102
+ - lib/svelte_on_rails/lib/fallback_renderer.rb
102
103
  - lib/svelte_on_rails/lib/log_patch.rb
103
104
  - lib/svelte_on_rails/lib/metrics.rb
104
105
  - lib/svelte_on_rails/lib/to_svelte_labels.rb
@@ -108,7 +109,6 @@ files:
108
109
  - lib/svelte_on_rails/lib/view_helper_support.rb
109
110
  - lib/svelte_on_rails/lib/watch_asset_changes.rb
110
111
  - lib/svelte_on_rails/railtie.rb
111
- - lib/svelte_on_rails/renderer/renderer.rb
112
112
  - lib/svelte_on_rails/ssr_server.rb
113
113
  - lib/svelte_on_rails/turbo_stream.rb
114
114
  - lib/svelte_on_rails/view_helpers.rb
@@ -1,64 +0,0 @@
1
- module SvelteOnRails
2
- class
3
- Renderer
4
-
5
- require 'open3'
6
- require 'base64'
7
- require 'json'
8
-
9
- def self.render(paths, props, debug)
10
- utils = SvelteOnRails::Lib::Utils
11
- cnf = SvelteOnRails::Configuration.instance
12
- mn = cnf.manifest(debug, paths[:name])
13
- manifest = mn[paths[:path]]
14
-
15
- if manifest.nil?
16
- 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."
17
- end
18
-
19
- cmd = [
20
- cnf.node_bin_path,
21
- cnf.rails_root.join('node_modules/@csedl/svelte-on-rails/src/ssr/render.js'),
22
- cnf.assets_folder_path.join(manifest['file'])
23
- ].join(' ')
24
-
25
- stdout, stderr, status = Open3.capture3(
26
- cmd,
27
- stdin_data: props.to_json,
28
- chdir: cnf.rails_root,
29
- )
30
-
31
- ary = stdout.split('$$ResponseSeparator$$')
32
- ary_t = ary.first.split('<time>')
33
- start = ary_t.first.to_f
34
- completed = ary_t.last.to_f
35
- cnf.debug_log(debug, paths[:name], "Render Server-side (#{(completed - start).round(2)}ms)")
36
-
37
- unless ary.length == 2
38
- raise "[SOR] #{paths[:name]}: RENDER ERROR\n\ncommand:\n+++\n#{cmd}\n+++\n\nstdout:\n+++\n#{stdout}+++\n\n\nstderr:\n+++\n#{stderr}+++"
39
- end
40
-
41
- begin
42
-
43
- unless status.to_s.match(/^pid [0-9]+ exit 0$/)
44
- cmp = "#{paths[:path]} was returned «#{status.to_s}»\n\n"
45
- msg = "#{cmp}output from render.js (stderr) =>\n+++\n" + stderr + "+++\n\nRender Svelte Server-side =>\n#{cmd}\n\n"
46
- utils.puts_warning(msg)
47
- end
48
-
49
- raw_response = JSON.parse(ary[1])
50
-
51
- htm = raw_response['html'].delete_prefix("<!--[-->").delete_suffix("<!--]-->")
52
-
53
- return {
54
- html: htm,
55
- head: raw_response['head'],
56
- }
57
-
58
- rescue => e
59
- raise "MY_ERR => #{e}\n\n+++\n#{e.backtrace.join("\n")}\n+++"
60
- end
61
- end
62
-
63
- end
64
- end