fix(build): accept standard pwa manifest rel tokens
This commit is contained in:
parent
ea051a3c25
commit
cb5ebefc3d
2 changed files with 50 additions and 1 deletions
|
|
@ -11,6 +11,7 @@ import (
|
|||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"dappco.re/go/core"
|
||||
"dappco.re/go/core/build/internal/ax"
|
||||
|
|
@ -118,7 +119,7 @@ func findManifestURL(htmlContent, baseURL string) (string, error) {
|
|||
href = a.Val
|
||||
}
|
||||
}
|
||||
if rel == "manifest" && href != "" {
|
||||
if relIncludesManifest(rel) && href != "" {
|
||||
manifestPath = href
|
||||
return
|
||||
}
|
||||
|
|
@ -146,6 +147,17 @@ func findManifestURL(htmlContent, baseURL string) (string, error) {
|
|||
return manifestURL.String(), nil
|
||||
}
|
||||
|
||||
// relIncludesManifest reports whether a rel attribute declares a manifest link.
|
||||
// HTML allows multiple space-separated tokens and case-insensitive values.
|
||||
func relIncludesManifest(rel string) bool {
|
||||
for _, token := range strings.Fields(rel) {
|
||||
if strings.EqualFold(token, "manifest") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// fetchManifest downloads and parses a PWA manifest.
|
||||
func fetchManifest(ctx context.Context, manifestURL string) (map[string]any, error) {
|
||||
resp, err := getWithContext(ctx, manifestURL)
|
||||
|
|
|
|||
37
cmd/build/cmd_pwa_test.go
Normal file
37
cmd/build/cmd_pwa_test.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package buildcmd
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPwa_FindManifestURL_Good(t *testing.T) {
|
||||
t.Run("accepts a standard manifest link", func(t *testing.T) {
|
||||
htmlContent := `<html><head><link rel="manifest" href="/manifest.json"></head></html>`
|
||||
|
||||
got, err := findManifestURL(htmlContent, "https://example.test/app/")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "https://example.test/manifest.json", got)
|
||||
})
|
||||
|
||||
t.Run("accepts case-insensitive tokenised rel values", func(t *testing.T) {
|
||||
htmlContent := `<html><head><link rel="Manifest icon" href="manifest.json"></head></html>`
|
||||
|
||||
got, err := findManifestURL(htmlContent, "https://example.test/app/")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "https://example.test/app/manifest.json", got)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPwa_FindManifestURL_Bad(t *testing.T) {
|
||||
t.Run("returns an error when no manifest link exists", func(t *testing.T) {
|
||||
htmlContent := `<html><head><link rel="icon" href="/icon.png"></head></html>`
|
||||
|
||||
got, err := findManifestURL(htmlContent, "https://example.test/app/")
|
||||
assert.Error(t, err)
|
||||
assert.Empty(t, got)
|
||||
assert.Contains(t, err.Error(), "pwa.findManifestURL")
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue