Skip to content

Commit

Permalink
feat(libsvelte): "Svelte" now takes "SvelteOptions" instead of props …
Browse files Browse the repository at this point in the history
…directly.
  • Loading branch information
razshare committed Jan 11, 2025
1 parent 787d27d commit 390073e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
13 changes: 8 additions & 5 deletions libsvelte.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,19 @@ func render(response *Response, stringProps string) (string, string, error) {
return head, body, nil
}

type SvelteOptions struct {
Ssr bool
Props map[string]interface{}
}

// Svelte renders and echos the svelte application.
func Svelte(response *Response, props map[string]interface{}) {
func Svelte(response *Response, options SvelteOptions) {
indexBytes, readError := response.server.embeddedFileSystem.ReadFile(filepath.Join(response.server.wwwDirectory, "dist", "client", "index.html"))
if readError != nil {
return
}

ssr := props["ssr"].(bool)

bytesProps, jsonError := json.Marshal(props)
bytesProps, jsonError := json.Marshal(options.Props)
if jsonError != nil {
ServerNotifyError(response.server, jsonError)
return
Expand All @@ -89,7 +92,7 @@ func Svelte(response *Response, props map[string]interface{}) {

head := ""
body := ""
if ssr {
if options.Ssr {
headLocal, bodyLocal, renderError := render(response, stringProps)
if renderError != nil {
ServerNotifyError(response.server, renderError)
Expand Down
16 changes: 10 additions & 6 deletions libsvelte_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ func TestEchoSvelteWithSsr(test *testing.T) {
test.Fatal(err)
})
ServerOnRequest(server, "GET /", func(server *Server, request *Request, response *Response) {
Svelte(response, map[string]interface{}{
"ssr": true,
"name": "world",
Svelte(response, SvelteOptions{
Ssr: true,
Props: map[string]interface{}{
"name": "world",
},
})
})
go ServerStart(server)
Expand Down Expand Up @@ -45,9 +47,11 @@ func TestEchoSvelteWithoutSsr(test *testing.T) {
test.Fatal(err)
})
ServerOnRequest(server, "GET /", func(server *Server, request *Request, response *Response) {
Svelte(response, map[string]interface{}{
"ssr": false,
"name": "world",
Svelte(response, SvelteOptions{
Ssr: false,
Props: map[string]interface{}{
"name": "world",
},
})
})
go ServerStart(server)
Expand Down

0 comments on commit 390073e

Please sign in to comment.