From 733579f73598d37bfbf5ba1a71d76042fc50b678 Mon Sep 17 00:00:00 2001
From: dj-oyu <68707227+dj-oyu@users.noreply.github.com>
Date: Sun, 22 Feb 2026 21:23:03 +0900
Subject: [PATCH] feat: auto-inject path rewrite script into dev proxy HTML
responses
The reverse proxy now injects a `
+
+// injectDevProxyScript inserts the dev proxy rewrite script into an HTML document.
+// Insertion priority: before , after
, or prepend to document.
+func injectDevProxyScript(html []byte) []byte {
+ script := []byte(devProxyScript)
+
+ // Priority 1: before
+ if idx := bytes.Index(bytes.ToLower(html), []byte("")); idx >= 0 {
+ out := make([]byte, 0, len(html)+len(script))
+ out = append(out, html[:idx]...)
+ out = append(out, script...)
+ out = append(out, html[idx:]...)
+ return out
+ }
+
+ // Priority 2: after
+ lower := bytes.ToLower(html)
+ if idx := bytes.Index(lower, []byte("= 0 {
+ // Find the closing '>' of the tag
+ closeIdx := bytes.IndexByte(lower[idx:], '>')
+ if closeIdx >= 0 {
+ insertAt := idx + closeIdx + 1
+ out := make([]byte, 0, len(html)+len(script))
+ out = append(out, html[:insertAt]...)
+ out = append(out, script...)
+ out = append(out, html[insertAt:]...)
+ return out
+ }
+ }
+
+ // Priority 3: prepend
+ out := make([]byte, 0, len(html)+len(script))
+ out = append(out, script...)
+ out = append(out, html...)
+ return out
+}
+
// escapeHTMLString escapes HTML special characters in a string.
func escapeHTMLString(s string) string {
s = strings.ReplaceAll(s, "&", "&")
diff --git a/pkg/miniapp/miniapp_test.go b/pkg/miniapp/miniapp_test.go
index 4108a647b..3f962c4fb 100644
--- a/pkg/miniapp/miniapp_test.go
+++ b/pkg/miniapp/miniapp_test.go
@@ -13,6 +13,7 @@ import (
"net/url"
"runtime"
"sort"
+ "strconv"
"strings"
"sync/atomic"
"testing"
@@ -1813,6 +1814,202 @@ func TestDevProxy_HostHeaderForwarded(t *testing.T) {
}
}
+// ── injectDevProxyScript tests ──
+
+func TestInjectDevProxyScript_HeadTag(t *testing.T) {
+ html := []byte(`TestHi
`)
+ result := injectDevProxyScript(html)
+ s := string(result)
+ if !strings.Contains(s, `