Packages
live_svelte
0.18.0
0.18.0
0.18.0-rc0
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.0
0.15.0
0.15.0-rc.6
0.15.0-rc.5
0.15.0-rc.4
0.15.0-rc.3
0.15.0-rc.2
0.15.0-rc.1
0.14.1
0.14.0
0.13.3
0.13.3-rc.1
0.13.2
0.13.1
0.13.0
0.12.0
0.11.0
0.10.2
0.10.1
0.10.0
0.9.0
0.9.0-rc2
0.9.0-rc1
0.9.0-rc0
0.8.0
0.7.1
0.7.0
0.6.0
0.5.1
0.5.0
0.5.0-rc0
0.4.2
0.4.2-rc0
0.4.1
0.4.1-rc0
0.4.0
0.3.5
0.3.5-rc1
0.3.5-rc0
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.3.0-rc1
0.3.0-rc0
0.2.0
0.2.0-rc1
0.2.0-rc0
0.1.0
0.1.0-rc5
0.1.0-rc4
0.1.0-rc3
0.1.0-rc2
0.1.0-rc1
0.1.0-rc0
E2E reactivity for Svelte and LiveView
Current section
Files
Jump to
Current section
Files
lib/ssr/vite_js.ex
defmodule LiveSvelte.SSR.ViteJS do
@moduledoc """
Implements SSR by making a POST request to `http://{:vite_host}/ssr_render`.
`ssr_render` is implemented as a Vite plugin. Add it to the `vite.config.js` plugins section:
```javascript
import liveSveltePlugin from "live_svelte/vitePlugin"
export default {
plugins: [liveSveltePlugin()],
// ...
}
```
## Configuration
In `config/dev.exs`:
```elixir
config :live_svelte, ssr_module: LiveSvelte.SSR.ViteJS
config :live_svelte, vite_host: "http://localhost:5173"
```
"""
@behaviour LiveSvelte.SSR
def render(name, props, slots) do
prepared_props = LiveSvelte.JSON.prepare(props)
prepared_slots = LiveSvelte.JSON.prepare(slots)
data = Jason.encode!(%{name: name, props: prepared_props, slots: prepared_slots})
url = vite_path("/ssr_render")
params = {String.to_charlist(url), [], ~c"application/json", data}
case :httpc.request(:post, params, [], []) do
{:ok, {{_, 200, _}, _headers, body}} ->
Jason.decode!(:erlang.list_to_binary(body))
{:ok, {{_, 500, _}, _headers, body}} ->
body_binary = :erlang.list_to_binary(body)
message =
case Jason.decode(body_binary) do
{:ok, %{"error" => %{"message" => msg, "loc" => loc, "frame" => frame}}} ->
"#{msg}\n#{loc["file"]}:#{loc["line"]}:#{loc["column"]}\n#{frame}"
{:ok, %{"error" => %{"stack" => stack}}} ->
stack
_ ->
"Unexpected Vite SSR response: 500 #{body_binary}"
end
raise %LiveSvelte.SSR.NotConfigured{message: message}
{:ok, {{_, status, code}, _headers, _body}} ->
raise %LiveSvelte.SSR.NotConfigured{
message: "Unexpected Vite SSR response: #{status} #{:erlang.list_to_binary(code)}"
}
{:error, {:failed_connect, [{:to_address, {host, port}}, {_, _, code}]}} ->
message = """
Unable to connect to Vite #{host}:#{port}: #{code}
Ensure Vite is running:
cd assets && npx vite
Or switch back to NodeJS SSR in config/dev.exs:
config :live_svelte, ssr_module: LiveSvelte.SSR.NodeJS
"""
raise %LiveSvelte.SSR.NotConfigured{message: message}
{:error, reason} ->
raise %LiveSvelte.SSR.NotConfigured{
message: "ViteJS SSR connection error: #{inspect(reason)}"
}
end
end
@doc """
Returns a path relative to the configured Vite JS host.
"""
def vite_path(path) do
case Application.get_env(:live_svelte, :vite_host) do
nil ->
message = """
Vite.js host is not configured. Please add the following to config/dev.exs:
config :live_svelte, vite_host: "http://localhost:5173"
and ensure Vite is running.
"""
raise %LiveSvelte.SSR.NotConfigured{message: message}
host ->
Path.join(host, path)
end
end
end