@@ -45,6 +45,18 @@ defmodule LiveSvelte do
4545 doc: "Class to apply to the Svelte component" ,
4646 examples: [ "my-class" , "my-class another-class" ]
4747
48+ attr :csp_nonce , :string ,
49+ default: nil ,
50+ doc: "A Content-Security-Policy nonce for the generated <script> and <style> tags"
51+
52+ attr :csp_script_nonce , :string ,
53+ default: nil ,
54+ doc: "A Content-Security-Policy nonce for the generated <script> tag"
55+
56+ attr :csp_style_nonce , :string ,
57+ default: nil ,
58+ doc: "A Content-Security-Policy nonce for the generated <style> tag"
59+
4860 attr :ssr , :boolean ,
4961 default: true ,
5062 doc: "Whether to render the component via NodeJS on the server" ,
@@ -124,6 +136,8 @@ defmodule LiveSvelte do
124136
125137 streams_diff = calculate_streams_diff ( assigns , init or dead )
126138
139+ csp_attrs = if nonce = assigns . csp_nonce , do: [ nonce: nonce ]
140+
127141 assigns =
128142 assigns
129143 |> assign ( :init , init )
@@ -134,9 +148,17 @@ defmodule LiveSvelte do
134148 |> assign ( :use_diff , use_diff )
135149 |> assign ( :props_diff , props_diff )
136150 |> assign ( :streams_diff , streams_diff )
151+ |> assign (
152+ :csp_script_attrs ,
153+ csp_attrs || if ( nonce = assigns . csp_script_nonce , do: [ nonce: nonce ] , else: [ ] )
154+ )
155+ |> assign (
156+ :csp_style_attrs ,
157+ csp_attrs || if ( nonce = assigns . csp_style_nonce , do: [ nonce: nonce ] , else: [ ] )
158+ )
137159
138160 ~H"""
139- < script >
161+ < script { @ csp_script_attrs } >
140162 <%= raw ( @ ssr_render [ "head" ] ) %>
141163 </ script >
142164 < div
@@ -154,7 +176,7 @@ defmodule LiveSvelte do
154176 >
155177 < div id = { "#{ @ svelte_id } -target" } data-svelte-target >
156178 { raw ( @ ssr_render [ "head" ] ) }
157- < style >
179+ < style { @ csp_style_attrs } >
158180 <%= raw ( @ ssr_render [ "css" ] [ "code" ] ) %>
159181 </ style >
160182 { raw ( @ ssr_render [ "html" ] ) }
0 commit comments