Skip to content

Commit

Permalink
feat(libsvelte): adding functionality for disabling ssr.
Browse files Browse the repository at this point in the history
  • Loading branch information
razshare committed Jan 11, 2025
1 parent d51c17e commit 3f49a4f
Showing 1 changed file with 53 additions and 25 deletions.
78 changes: 53 additions & 25 deletions libsvelte.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,23 @@ import (
"strings"
)

// EchoSvelte renders and echos the svelte application.
func EchoSvelte(response *Response, props map[string]interface{}) {
func render(response *Response, stringProps string) (string, string, error) {
renderFileName := filepath.Join(response.server.wwwDirectory, "dist", "server", "render.server.js")

renderEsmBytes, readError := response.server.embeddedFileSystem.ReadFile(renderFileName)
if readError != nil {
ServerNotifyError(response.server, readError)
return
return "", "", readError
}

renderEsm := string(renderEsmBytes)

renderCjs, javaScriptBundleError := JavaScriptBundle(response.server.wwwDirectory, api.FormatCommonJS, renderEsm)
if javaScriptBundleError != nil {
ServerNotifyError(response.server, javaScriptBundleError)
return
return "", "", javaScriptBundleError
}

renderIif := fmt.Sprintf("const module={exports:{}}; const render = \n(function(){\n%s\nreturn render;\n})()", renderCjs)

bytesProps, jsonError := json.Marshal(props)
if jsonError != nil {
ServerNotifyError(response.server, jsonError)
return
}
stringProps := string(bytesProps)

doneEsm := fmt.Sprintf(
`
%s
Expand All @@ -50,8 +40,7 @@ func EchoSvelte(response *Response, props map[string]interface{}) {

doneCjs, bundleError := JavaScriptBundle(response.server.wwwDirectory, api.FormatCommonJS, doneEsm)
if bundleError != nil {
ServerNotifyError(response.server, bundleError)
return
return "", "", bundleError
}

head := ""
Expand All @@ -75,25 +64,64 @@ func EchoSvelte(response *Response, props map[string]interface{}) {
},
)
if javaScriptError != nil {
ServerNotifyError(response.server, javaScriptError)
return
return head, body, javaScriptError
}
defer destroy()

return head, body, nil
}

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

scriptTarget := fmt.Sprintf(`<script type="application/javascript">function target(){return document.getElementById("app")}</script>`)
scriptProps := fmt.Sprintf(`<script type="application/javascript">function props(){return %s}</script>`, stringProps)
scriptBody := fmt.Sprintf(`<div id="app">%s</div>`, body)
bytesProps, jsonError := json.Marshal(props)
if jsonError != nil {
ServerNotifyError(response.server, jsonError)
return
}
stringProps := string(bytesProps)

head := ""
body := ""
if ssr {
headLocal, bodyLocal, renderError := render(response, stringProps)
if renderError != nil {
ServerNotifyError(response.server, renderError)
return
}
head = headLocal
body = bodyLocal
}

index := strings.Replace(string(indexBytes), "<!--app-head-->", head, 1)
index = strings.Replace(index, "<!--app-target-->", scriptTarget, 1)
index = strings.Replace(index, "<!--app-props-->", scriptProps, 1)
index = strings.Replace(index, "<!--app-body-->", scriptBody, 1)
stringIndex := strings.Replace(
strings.Replace(
strings.Replace(
strings.Replace(
string(indexBytes),
"<!--app-target-->",
`<script type="application/javascript">function target(){return document.getElementById("app")}</script>`,
1,
),
"<!--app-body-->",
fmt.Sprintf(`<div id="app">%s</div>`, body),
1,
),
"<!--app-head-->",
head,
1,
),
"<!--app-props-->",
fmt.Sprintf(
`<script type="application/javascript">function props(){return %s}</script>`,
stringProps,
),
1,
)

Header(response, "Content-Type", "text/html")
Echo(response, index)
Echo(response, stringIndex)
}

0 comments on commit 3f49a4f

Please sign in to comment.