feat(web): add network-only PWA installability (#6190)
* feat(web): add network-only PWA installability Serve the manifest, registration script, network-only service worker, and icons under the runtime web base path so panels remain installable at arbitrary configured URLs. This does not add offline caching or change panel, API, database, or Xray behavior. * chore(docs): remove development planning notes Keep the pull request focused on the PWA implementation, tests, and user-facing verification documentation. * feat(web): adopt the 3X logo PWA icon set from #1865 Replace the two placeholder SVG icons with the six-size PNG set (16/24/32/64/192/512) contributed by @Incognito-Coder in PR #1865. The PNGs have transparent rounded corners, so the manifest entries drop the maskable purpose claim and rely on the default any. --------- Co-authored-by: korsun009 <277924786+korsun009@users.noreply.github.com> Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
This commit is contained in:
1 parent
3f1dd4bf5a
commit
3a2f9b48da
16 files changed
+343
No files matched your search
@@ -71,6 +71,28 @@ func withServerBasePath(spec []byte, basePath string) ([]byte, error) {
|
||||
return json.Marshal(doc)
|
||||
}
|
||||
|
||||
func normalizeWebBasePath(basePath string) string {
|
||||
if basePath == "" {
|
||||
return "/"
|
||||
}
|
||||
if !strings.HasPrefix(basePath, "/") {
|
||||
basePath = "/" + basePath
|
||||
}
|
||||
if !strings.HasSuffix(basePath, "/") {
|
||||
basePath += "/"
|
||||
}
|
||||
return basePath
|
||||
}
|
||||
|
||||
func pwaHeadInjection(basePath, pageName string) []byte {
|
||||
if pageName != "index.html" && pageName != "login.html" {
|
||||
return nil
|
||||
}
|
||||
|
||||
basePath = normalizeWebBasePath(basePath)
|
||||
return []byte(`<link rel="manifest" href="` + htmlpkg.EscapeString(basePath+"manifest.webmanifest") + `"><script data-cfasync="false" defer src="` + htmlpkg.EscapeString(basePath+"pwa-register.js") + `"></script>`)
|
||||
}
|
||||
|
||||
func serveDistPage(c *gin.Context, name string) {
|
||||
body, err := fs.ReadFile(distFS, "dist/"+name)
|
||||
if err != nil {
|
||||
@@ -120,6 +142,7 @@ func serveDistPage(c *gin.Context, name string) {
|
||||
inject := []byte(script)
|
||||
inject = append(inject, csrfMeta...)
|
||||
inject = append(inject, basePathMeta...)
|
||||
inject = append(inject, pwaHeadInjection(basePath, name)...)
|
||||
inject = append(inject, []byte(`</head>`)...)
|
||||
out := bytes.Replace(body, []byte("</head>"), inject, 1)
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -40,3 +41,33 @@ func TestWithServerBasePathInvalidJSON(t *testing.T) {
|
||||
t.Errorf("expected error on invalid spec, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPWAHeadInjectionUsesRuntimeBasePath(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
basePath string
|
||||
wantPath string
|
||||
}{
|
||||
{name: "root", basePath: "/", wantPath: "/manifest.webmanifest"},
|
||||
{name: "secret path", basePath: "panel-secret", wantPath: "/panel-secret/manifest.webmanifest"},
|
||||
{name: "trailing slash", basePath: "/panel-secret/", wantPath: "/panel-secret/manifest.webmanifest"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
head := string(pwaHeadInjection(test.basePath, "login.html"))
|
||||
if !strings.Contains(head, `href="`+test.wantPath+`"`) {
|
||||
t.Fatalf("manifest URL = %q, want %q", head, test.wantPath)
|
||||
}
|
||||
if !strings.Contains(head, `src="`+strings.Replace(test.wantPath, "manifest.webmanifest", "pwa-register.js", 1)+`"`) {
|
||||
t.Fatalf("registration URL = %q", head)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPWAHeadInjectionSkipsSubscriptionPage(t *testing.T) {
|
||||
if got := pwaHeadInjection("/panel-secret/", "subpage.html"); got != nil {
|
||||
t.Fatalf("subpage injection = %q, want nil", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type pwaAsset struct {
|
||||
path string
|
||||
contentType string
|
||||
}
|
||||
|
||||
var pwaAssets = map[string]pwaAsset{
|
||||
"manifest.webmanifest": {path: "dist/manifest.webmanifest", contentType: "application/manifest+json; charset=utf-8"},
|
||||
"pwa-register.js": {path: "dist/pwa-register.js", contentType: "application/javascript; charset=utf-8"},
|
||||
"service-worker.js": {path: "dist/service-worker.js", contentType: "application/javascript; charset=utf-8"},
|
||||
"icons/3x-ui-16.png": {path: "dist/icons/3x-ui-16.png", contentType: "image/png"},
|
||||
"icons/3x-ui-24.png": {path: "dist/icons/3x-ui-24.png", contentType: "image/png"},
|
||||
"icons/3x-ui-32.png": {path: "dist/icons/3x-ui-32.png", contentType: "image/png"},
|
||||
"icons/3x-ui-64.png": {path: "dist/icons/3x-ui-64.png", contentType: "image/png"},
|
||||
"icons/3x-ui-192.png": {path: "dist/icons/3x-ui-192.png", contentType: "image/png"},
|
||||
"icons/3x-ui-512.png": {path: "dist/icons/3x-ui-512.png", contentType: "image/png"},
|
||||
}
|
||||
|
||||
func servePWAAsset(c *gin.Context, assetName string) {
|
||||
asset, ok := pwaAssets[assetName]
|
||||
if !ok {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := fs.ReadFile(distFS, asset.path)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
c.Header("Cache-Control", "no-cache, no-store, must-revalidate")
|
||||
c.Header("Pragma", "no-cache")
|
||||
c.Header("Expires", "0")
|
||||
c.Data(http.StatusOK, asset.contentType, body)
|
||||
}
|
||||
|
||||
func ServePWAManifest(c *gin.Context) {
|
||||
servePWAAsset(c, "manifest.webmanifest")
|
||||
}
|
||||
|
||||
func ServePWARegister(c *gin.Context) {
|
||||
servePWAAsset(c, "pwa-register.js")
|
||||
}
|
||||
|
||||
func ServePWAServiceWorker(c *gin.Context) {
|
||||
servePWAAsset(c, "service-worker.js")
|
||||
}
|
||||
|
||||
func ServePWAIcon(c *gin.Context) {
|
||||
servePWAAsset(c, "icons/"+c.Param("name"))
|
||||
}
|
||||
Loaded 3 of 16 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user