lib/bridgetown-lit-renderer/renderer.rb in bridgetown-lit-renderer-1.1.1 vs lib/bridgetown-lit-renderer/renderer.rb in bridgetown-lit-renderer-2.0.0.beta1
- old
+ new
@@ -52,66 +52,64 @@
def cache
@cache ||= Bridgetown::Cache.new("LitSSR")
end
+ def call_http_server(payload)
+ Faraday.post(
+ "http://127.0.0.1:#{self.class.serverport}",
+ payload,
+ "Authorization" => "Bearer #{self.class.authtoken}"
+ ).body.force_encoding("utf-8")
+ end
+
def esbuild(code)
raise "You must first assign the `site' accessor" unless site
unless @esbuild_notice_printed
Bridgetown.logger.info "Lit SSR:", "Bundling with esbuild..."
@esbuild_notice_printed = true
end
+ # TODO: shouldn't this use the sidecar Node process as well?
IO.popen(["node", site.in_root_dir("./config/lit-ssr.config.js")], "r+") do |pipe|
pipe.puts({ code: code }.to_json)
pipe.close_write
pipe.read
end
end
- def render(code, data:, entry:, caching: true) # rubocop:todo Metrics/MethodLength, Metrics/AbcSize
+ def render(code, data:, entry:)
raise "You must first assign the `site' accessor" unless site
cache_key = "esbuild-#{code}#{entry}#{entry_key(entry)}"
- esbuild_fn = -> { esbuild(js_code_block(entry, code)) }
+ built_code = cache.getset(cache_key) { esbuild(js_code_block(entry, code)) }
- built_code = if caching
- cache.getset(cache_key) { esbuild_fn.() }
- else
- esbuild_fn.()
- end
-
unless @render_notice_printed
Bridgetown.logger.info "Lit SSR:", "Rendering components..."
@render_notice_printed = true
end
self.class.start_node_server(site.in_root_dir("node_modules"))
- output = Faraday.post(
- "http://127.0.0.1:#{self.class.serverport}",
- "const data = #{data.to_json}; #{built_code}",
- "Authorization" => "Bearer #{self.class.authtoken}"
- ).body
+ output = call_http_server("const data = #{data.to_json}; #{built_code}")
if output == "SCRIPT NOT VALID!"
output = <<~HTML
<ssr-error style="display:block; padding:0.3em 0.5em; color:white; background:maroon; font-weight:bold">
Lit SSR error in #{entry}, see logs
</ssr-error>
HTML
- cache.delete(cache_key) if caching
+ cache.delete(cache_key)
end
output.html_safe
end
def js_code_block(entry, code)
entry_import = "import #{entry.to_json}"
<<~JS
- import { Readable } from "stream"
import { render } from "@lit-labs/ssr/lib/render-with-global-dom-shim.js"
import { html } from "lit"
#{entry_import}
const ssrResult = render(html`