Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions lib/live_svelte.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ defmodule LiveSvelte do
doc: "Class to apply to the Svelte component",
examples: ["my-class", "my-class another-class"]

attr :csp_nonce, :string,
default: nil,
doc: "A Content-Security-Policy nonce for the generated <script> and <style> tags"

attr :csp_script_nonce, :string,
default: nil,
doc: "A Content-Security-Policy nonce for the generated <script> tag"

attr :csp_style_nonce, :string,
default: nil,
doc: "A Content-Security-Policy nonce for the generated <style> tag"

attr :ssr, :boolean,
default: true,
doc: "Whether to render the component via NodeJS on the server",
Expand Down Expand Up @@ -83,15 +95,20 @@ defmodule LiveSvelte do
end
end

csp_attrs = if(nonce = assigns.csp_nonce, do: [nonce: nonce])

assigns =
assigns
|> assign(:init, init)
|> assign(:slots, slots)
|> assign(:ssr_render, ssr_code)
assign(assigns,
csp_script_attrs: csp_attrs || if(nonce = assigns.csp_script_nonce, do: [nonce: nonce]),
csp_style_attrs: csp_attrs || if(nonce = assigns.csp_style_nonce, do: [nonce: nonce]),
init: init,
slots: slots,
ssr_render: ssr_code
)

~H"""
<.live_json live_json_props={@live_json_props}>
<script>
<script {@csp_script_attrs}>
<%= raw(@ssr_render["head"]) %>
</script>
<div
Expand All @@ -108,7 +125,7 @@ defmodule LiveSvelte do
class={@class}
>
<%= raw(@ssr_render["head"]) %>
<style>
<style {@csp_style_attrs}>
<%= raw(@ssr_render["css"]["code"]) %>
</style>
<%= raw(@ssr_render["html"]) %>
Expand Down