Skip to content

Commit

Permalink
Fixed issue where html files without header didn't get mirrored
Browse files Browse the repository at this point in the history
  • Loading branch information
baalimago committed Aug 18, 2024
1 parent cfbac98 commit d6da47a
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions internal/wsinject/wsinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Fileserver struct {
wsDispatcherStartedMu *sync.Mutex
}

var ErrNoHeaderTagFound = errors.New("no header tag found")

const deltaStreamer = `<!-- This script has been injected by wd-40 and allows hot reloads -->
<script type="module" src="delta-streamer.js"></script>`

Expand Down Expand Up @@ -59,7 +61,6 @@ func (fs *Fileserver) mirrorFile(origPath string) error {
}
injected, injectedBytes, err := injectWebsocketScript(fileB)
if err != nil {

Check failure on line 63 in internal/wsinject/wsinject.go

View workflow job for this annotation

GitHub Actions / call-workflow / validate

empty branch (SA9003)
return fmt.Errorf("failed to ineject websocket script: %v", err)
}
if injected {
ancli.PrintfNotice("injected delta-streamer script loading tag in: '%v'", origPath)
Expand Down Expand Up @@ -167,7 +168,7 @@ func injectScript(html []byte, scriptTag string) ([]byte, error) {
// Find the location of the closing `</header>` tag
idx := strings.Index(htmlStr, "</head>")
if idx == -1 {
return nil, fmt.Errorf("no </head> tag found in the HTML")
return html, ErrNoHeaderTagFound
}

var buf bytes.Buffer
Expand All @@ -193,14 +194,20 @@ func injectScript(html []byte, scriptTag string) ([]byte, error) {

func injectWebsocketScript(b []byte) (bool, []byte, error) {
contentType := http.DetectContentType(b)
injected := false
// Only act on html files
if !strings.Contains(contentType, "text/html") {
return false, b, nil
return injected, b, nil
}
b, err := injectScript(b, deltaStreamer)
injected = true
if err != nil {
return false, nil, fmt.Errorf("failed to inject script tag: %w", err)
if !errors.Is(err, ErrNoHeaderTagFound) {
return injected, nil, fmt.Errorf("failed to inject script tag: %w", err)
} else {
injected = false
}
}

return true, b, nil
return injected, b, nil
}

0 comments on commit d6da47a

Please sign in to comment.