Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(preview): share <head> insertion helpers across HTML injectors (#268)

fx-kurumi449441d47802
diff --git a/internal/preview/devserver.go b/internal/preview/devserver.go
index dd680f3b..381f406c 100644
--- a/internal/preview/devserver.go
+++ b/internal/preview/devserver.go
@@ -124,18 +124,10 @@ func injectDevServerIntercept(html, prefix string, port int) string {
tag := `<script data-af-dev-server>` + script + `</script>`
// First thing in the document: module scripts and inline boot code must
// already see the patched fetch/WebSocket.
- lower := strings.ToLower(html)
- if i := strings.Index(lower, "<head>"); i >= 0 {
- at := i + len("<head>")
- return html[:at] + tag + html[at:]
+ if out, ok := insertAfterHeadOpen(html, tag); ok {
+ return out
}
- if i := strings.Index(lower, "<head "); i >= 0 {
- if end := strings.Index(html[i:], ">"); end >= 0 {
- at := i + end + 1
- return html[:at] + tag + html[at:]
- }
- }
- if i := strings.Index(lower, "<html"); i >= 0 {
+ if i := strings.Index(strings.ToLower(html), "<html"); i >= 0 {
if end := strings.Index(html[i:], ">"); end >= 0 {
at := i + end + 1
return html[:at] + tag + html[at:]
diff --git a/internal/preview/gateway.go b/internal/preview/gateway.go
index c2824e42..a6574947 100644
--- a/internal/preview/gateway.go
+++ b/internal/preview/gateway.go
@@ -259,18 +259,36 @@ func injectBaseTag(html, prefix string) string {
href += "/"
}
tag := `<base href="` + href + `">`
+ if out, ok := insertAfterHeadOpen(html, tag); ok {
+ return out
+ }
+ return tag + html
+}
+
+// insertAfterHeadOpen places tag right after the first <head> or <head ...>
+// opening tag (case-insensitive). It reports false when there is none.
+func insertAfterHeadOpen(html, tag string) (string, bool) {
lower := strings.ToLower(html)
if i := strings.Index(lower, "<head>"); i >= 0 {
at := i + len("<head>")
- return html[:at] + tag + html[at:]
+ return html[:at] + tag + html[at:], true
}
if i := strings.Index(lower, "<head "); i >= 0 {
if end := strings.Index(html[i:], ">"); end >= 0 {
at := i + end + 1
- return html[:at] + tag + html[at:]
+ return html[:at] + tag + html[at:], true
}
}
- return tag + html
+ return html, false
+}
+
+// insertBeforeHeadClose places tag right before the first </head>
+// (case-insensitive). It reports false when there is none.
+func insertBeforeHeadClose(html, tag string) (string, bool) {
+ if i := strings.Index(strings.ToLower(html), "</head>"); i >= 0 {
+ return html[:i] + tag + html[i:], true
+ }
+ return html, false
}
// A page's own <meta name="referrer"> overrides the response header, and
@@ -284,9 +302,8 @@ func injectPreviewViewport(html string) string {
if strings.Contains(html, `data-af-preview-viewport`) {
return html
}
- lower := strings.ToLower(html)
- if i := strings.Index(lower, "</head>"); i >= 0 {
- return html[:i] + tag + html[i:]
+ if out, ok := insertBeforeHeadClose(html, tag); ok {
+ return out
}
return tag + html
}
@@ -314,16 +331,8 @@ func injectPreviewBackend(html, prefix, backend string) string {
// sent to the configured backend origin with path/query unchanged.
intercept := strings.Replace(previewBackendInterceptJS, "__AF_PREFIX__", string(prefixJSON), 1)
tag := `<script data-af-preview-backend>window.__AGENTFLOW_PREVIEW_BACKEND__=` + string(raw) + `;` + intercept + `</script>`
- lower := strings.ToLower(html)
- if i := strings.Index(lower, "<head>"); i >= 0 {
- at := i + len("<head>")
- return html[:at] + tag + html[at:]
- }
- if i := strings.Index(lower, "<head "); i >= 0 {
- if end := strings.Index(html[i:], ">"); end >= 0 {
- at := i + end + 1
- return html[:at] + tag + html[at:]
- }
+ if out, ok := insertAfterHeadOpen(html, tag); ok {
+ return out
}
return tag + html
}
@@ -345,9 +354,8 @@ func injectPreviewHistory(html, prefix string, routed bool) string {
// any <base> has resolved against the real preview URL.
shim := strings.NewReplacer("__AF_PREFIX__", string(prefixJSON), "__AF_ROUTED__", strconv.FormatBool(routed)).Replace(previewHistoryShimJS)
tag := `<script data-af-preview-history>` + shim + `</script>`
- lower := strings.ToLower(html)
- if i := strings.Index(lower, "</head>"); i >= 0 {
- return html[:i] + tag + html[i:]
+ if out, ok := insertBeforeHeadClose(html, tag); ok {
+ return out
}
return tag + html
}
diff --git a/internal/preview/head_insert_test.go b/internal/preview/head_insert_test.go
new file mode 100644
index 00000000..06cf8b5d
--- /dev/null
+++ b/internal/preview/head_insert_test.go
@@ -0,0 +1,94 @@
+package preview
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestInsertAfterHeadOpen(t *testing.T) {
+ cases := []struct {
+ name, html, want string
+ ok bool
+ }{
+ {"plain head", `<html><head></head></html>`, `<html><head>T</head></html>`, true},
+ {"upper head", `<HTML><HEAD></HEAD></HTML>`, `<HTML><HEAD>T</HEAD></HTML>`, true},
+ {"head with attrs", `<head lang="en"><title>x</title></head>`, `<head lang="en">T<title>x</title></head>`, true},
+ {"prefers bare head", `<head lang="en"></head><head>`, `<head lang="en"></head><head>T`, true},
+ {"no head", `<body>x</body>`, `<body>x</body>`, false},
+ {"unterminated head", `<head lang="en"`, `<head lang="en"`, false},
+ {"header is not head", `<header>x</header>`, `<header>x</header>`, false},
+ }
+ for _, tc := range cases {
+ got, ok := insertAfterHeadOpen(tc.html, "T")
+ if got != tc.want || ok != tc.ok {
+ t.Errorf("%s: got (%q, %v), want (%q, %v)", tc.name, got, ok, tc.want, tc.ok)
+ }
+ }
+}
+
+func TestInsertBeforeHeadClose(t *testing.T) {
+ cases := []struct {
+ name, html, want string
+ ok bool
+ }{
+ {"plain", `<head><title>x</title></head><body>`, `<head><title>x</title>T</head><body>`, true},
+ {"upper", `<HEAD></HEAD>`, `<HEAD>T</HEAD>`, true},
+ {"no close", `<head><body>`, `<head><body>`, false},
+ }
+ for _, tc := range cases {
+ got, ok := insertBeforeHeadClose(tc.html, "T")
+ if got != tc.want || ok != tc.ok {
+ t.Errorf("%s: got (%q, %v), want (%q, %v)", tc.name, got, ok, tc.want, tc.ok)
+ }
+ }
+}
+
+// The injectors keep their positions and fallbacks after sharing the helpers.
+func TestHeadInjectorsKeepPlacement(t *testing.T) {
+ const prefix = "/preview/x"
+ base := `<base href="/preview/x/">`
+ for html, want := range map[string]string{
+ `<head></head>`: `<head>` + base + `</head>`,
+ `<HEAD class="a"></HEAD>`: `<HEAD class="a">` + base + `</HEAD>`,
+ `<head`: base + `<head`,
+ `<p>x</p>`: base + `<p>x</p>`,
+ } {
+ if got := injectBaseTag(html, prefix); got != want {
+ t.Errorf("injectBaseTag(%q) = %q, want %q", html, got, want)
+ }
+ }
+
+ viewport := injectPreviewViewport(`<head><title>t</title></head>`)
+ if !strings.HasPrefix(viewport, `<head><title>t</title><style data-af-preview-viewport>`) || !strings.HasSuffix(viewport, `</style></head>`) {
+ t.Errorf("viewport not placed before </head>: %q", viewport)
+ }
+
+ backend := injectPreviewBackend(`<html><head lang="en"><title>t</title></head></html>`, prefix, "https://api.example")
+ if !strings.HasPrefix(backend, `<html><head lang="en"><script data-af-preview-backend>`) || !strings.HasSuffix(backend, `</script><title>t</title></head></html>`) {
+ t.Errorf("backend script not placed after <head ...>: %q", backend)
+ }
+ if got := injectPreviewBackend(`<head`, prefix, "https://api.example"); !strings.HasPrefix(got, `<script data-af-preview-backend>`) || !strings.HasSuffix(got, `</script><head`) {
+ t.Errorf("backend fallback changed: %q", got)
+ }
+
+ history := injectPreviewHistory(`<head><title>t</title></head>`, prefix, false)
+ if !strings.HasPrefix(history, `<head><title>t</title><script data-af-preview-history>`) || !strings.HasSuffix(history, `</script></head>`) {
+ t.Errorf("history shim not placed before </head>: %q", history)
+ }
+
+ for html, check := range map[string]func(string) bool{
+ `<head lang="en"></head>`: func(s string) bool {
+ return strings.HasPrefix(s, `<head lang="en"><script data-af-dev-server>`) && strings.HasSuffix(s, `</script></head>`)
+ },
+ `<html lang="en"><body></body></html>`: func(s string) bool {
+ return strings.HasPrefix(s, `<html lang="en"><script data-af-dev-server>`) && strings.HasSuffix(s, `</script><body></body></html>`)
+ },
+ `<p>x</p>`: func(s string) bool {
+ return strings.HasPrefix(s, `<script data-af-dev-server>`) && strings.HasSuffix(s, `</script><p>x</p>`)
+ },
+ } {
+ if got := injectDevServerIntercept(html, prefix, 5173); !check(got) {
+ t.Errorf("injectDevServerIntercept(%q) placement changed: %q", html, got)
+ }
+ }
+}