Skip to content

Commit e918932

Browse files
committed
Fix streaming response
1 parent de8cf6f commit e918932

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

internal/app/dev/styling.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ func (s *AppStyle) setupTailwindConfig(templateLocations []string, sourceFS *app
211211
buf.WriteString(fmt.Sprintf("'%s'", path.Join(sourceFS.Root, loc)))
212212
}
213213

214+
buf.WriteString(", ")
215+
buf.WriteString(fmt.Sprintf("'%s'", path.Join(sourceFS.Root, "static", "*.js")))
216+
214217
configContents := fmt.Sprintf(TAILWIND_CONFIG_CONTENTS, buf.String(), daisyPlugin, daisyThemes)
215218
if err := workFS.Write(configPath, []byte(configContents)); err != nil {
216219
return fmt.Errorf("error writing tailwind config file : %s", err)

internal/app/handler.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,6 @@ func (a *App) handleStreamResponse(w http.ResponseWriter, r *http.Request, rtype
532532
} else {
533533
w.Header().Set("Content-Type", "text/html; charset=utf-8")
534534
}
535-
w.WriteHeader(http.StatusOK)
536535

537536
retValue := streamResponse["value"]
538537
if retValue == nil {
@@ -552,27 +551,44 @@ func (a *App) handleStreamResponse(w http.ResponseWriter, r *http.Request, rtype
552551
return
553552
}
554553

554+
w.WriteHeader(http.StatusOK)
555555
for v := range retSeq {
556-
if rtype == apptype.HTML_TYPE {
557-
err := a.executeTemplate(w, "", fragment, v)
556+
if rtype == apptype.TEXT || (rtype == apptype.HTML_TYPE && (fragment == "" || fragment == "-")) {
557+
vStr, ok := v.(string)
558+
if !ok {
559+
vStr = fmt.Sprintf("%v", v)
560+
}
561+
vStr = types.StripQuotes(vStr)
562+
_, err := fmt.Fprint(w, vStr)
558563
if err != nil {
559564
http.Error(w, err.Error(), http.StatusInternalServerError)
560565
return
561566
}
562-
} else if rtype == apptype.JSON {
563-
err := json.NewEncoder(w).Encode(retValue)
567+
} else if rtype == apptype.HTML_TYPE {
568+
err := a.executeTemplate(w, "", fragment, v)
564569
if err != nil {
565570
http.Error(w, err.Error(), http.StatusInternalServerError)
566571
return
567572
}
568-
} else if rtype == apptype.TEXT {
569-
_, err := fmt.Fprint(w, retValue)
573+
} else if rtype == apptype.JSON {
574+
err := json.NewEncoder(w).Encode(v)
570575
if err != nil {
571576
http.Error(w, err.Error(), http.StatusInternalServerError)
572577
return
573578
}
574579
}
575580

581+
_, err := fmt.Fprint(w, "\n")
582+
if err != nil {
583+
http.Error(w, err.Error(), http.StatusInternalServerError)
584+
return
585+
}
586+
587+
flusher.Flush()
588+
}
589+
590+
if rtype == apptype.HTML_TYPE {
591+
w.Write([]byte("<!--cl_stream_end-->\n\n"))
576592
flusher.Flush()
577593
}
578594

internal/app/tests/styling_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ app = ace.app("testApp", custom_layout=True, routes = [ace.html("/")],
8181

8282
data, err = workFS.ReadFile("style/tailwind.config.js")
8383
testutil.AssertNoError(t, err)
84-
testutil.AssertStringMatch(t, "tailwind.config.js", `module.exports = { content: ['action/*.go.html', '*.go.html', 'base_templates/*.go.html'], theme: { extend: {}, }, plugins: [ ], }`, string(data))
84+
testutil.AssertStringMatch(t, "tailwind.config.js", `module.exports = { content: ['action/*.go.html', '*.go.html', 'base_templates/*.go.html', 'static/*.js'], theme: { extend: {}, }, plugins: [ ], }`, string(data))
8585
}
8686

8787
func TestStyleDaisyUI(t *testing.T) {
@@ -103,7 +103,7 @@ app = ace.app("testApp", custom_layout=True, routes = [ace.html("/")],
103103

104104
data, err = workFS.ReadFile("style/tailwind.config.js")
105105
testutil.AssertNoError(t, err)
106-
testutil.AssertStringMatch(t, "tailwind.config.js", `module.exports = { content: ['action/*.go.html', '*.go.html', 'base_templates/*.go.html'], theme: { extend: {}, }, plugins: [ require("daisyui") ], daisyui: { themes: ["emerald", "night"], }, }`, string(data))
106+
testutil.AssertStringMatch(t, "tailwind.config.js", `module.exports = { content: ['action/*.go.html', '*.go.html', 'base_templates/*.go.html', 'static/*.js'], theme: { extend: {}, }, plugins: [ require("daisyui") ], daisyui: { themes: ["emerald", "night"], }, }`, string(data))
107107
}
108108

109109
func TestStyleDaisyUIThemes(t *testing.T) {
@@ -125,7 +125,7 @@ app = ace.app("testApp", custom_layout=True, routes = [ace.html("/")],
125125

126126
data, err = workFS.ReadFile("style/tailwind.config.js")
127127
testutil.AssertNoError(t, err)
128-
testutil.AssertStringMatch(t, "tailwind.config.js", `module.exports = { content: ['action/*.go.html', '*.go.html', 'base_templates/*.go.html'], theme: { extend: {}, }, plugins: [ require("daisyui") ], daisyui: { themes: ["cupcake", "dark", "emerald", "night"], }, }`, string(data))
128+
testutil.AssertStringMatch(t, "tailwind.config.js", `module.exports = { content: ['action/*.go.html', '*.go.html', 'base_templates/*.go.html', 'static/*.js'], theme: { extend: {}, }, plugins: [ require("daisyui") ], daisyui: { themes: ["cupcake", "dark", "emerald", "night"], }, }`, string(data))
129129
}
130130

131131
func TestStyleDaisyUILight(t *testing.T) {
@@ -147,7 +147,7 @@ app = ace.app("testApp", custom_layout=True, routes = [ace.html("/")],
147147

148148
data, err = workFS.ReadFile("style/tailwind.config.js")
149149
testutil.AssertNoError(t, err)
150-
testutil.AssertStringMatch(t, "tailwind.config.js", `module.exports = { content: ['action/*.go.html', '*.go.html', 'base_templates/*.go.html'], theme: { extend: {}, }, plugins: [ require("daisyui") ], daisyui: { themes: ["abc", "cupcake", "xyz"], }, }`, string(data))
150+
testutil.AssertStringMatch(t, "tailwind.config.js", `module.exports = { content: ['action/*.go.html', '*.go.html', 'base_templates/*.go.html', 'static/*.js'], theme: { extend: {}, }, plugins: [ require("daisyui") ], daisyui: { themes: ["abc", "cupcake", "xyz"], }, }`, string(data))
151151
}
152152

153153
func TestStyleCustom(t *testing.T) {

0 commit comments

Comments
 (0)