mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-02 14:56:38 +08:00
fix(cloudflare): restore group detail pages for ids other than 1
Static export only generates /cloudflare/groups/1.html. Unknown ids fell through to the dashboard index, bouncing the browser home and triggering React hydration error #418. Serve the generated group shell and resolve the real id from the pathname, matching the websites detail fallback.
This commit is contained in:
@@ -8,27 +8,49 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
//nolint:unused // Used by frontend.go in embed_frontend builds; default lint runs without that tag.
|
||||
var nextExportDynamicPrefixes = []string{"websites", "cloudflare/groups"}
|
||||
|
||||
//nolint:unused // Used by frontend.go in embed_frontend builds; default lint runs without that tag.
|
||||
func resolveNextExportDynamicFallback(subFS fs.FS, cleanPath string) (string, bool) {
|
||||
parts := strings.Split(cleanPath, "/")
|
||||
if len(parts) < 2 || parts[0] != "websites" || parts[1] == "" {
|
||||
templatePath, ok := nextExportDynamicTemplate(cleanPath)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
|
||||
var templatePath string
|
||||
switch {
|
||||
case len(parts) == 2 && !strings.Contains(parts[1], "."):
|
||||
templatePath = "websites/1.html"
|
||||
case len(parts) == 2 && strings.HasSuffix(parts[1], ".txt"):
|
||||
templatePath = "websites/1.txt"
|
||||
case len(parts) == 3 && parts[2] != "" && strings.HasPrefix(parts[2], "__next.") && strings.HasSuffix(parts[2], ".txt"):
|
||||
templatePath = "websites/1/" + parts[2]
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
|
||||
if _, err := fs.Stat(subFS, templatePath); err != nil {
|
||||
return "", false
|
||||
}
|
||||
return templatePath, true
|
||||
}
|
||||
|
||||
//nolint:unused // Used by frontend.go in embed_frontend builds; default lint runs without that tag.
|
||||
func nextExportDynamicTemplate(cleanPath string) (string, bool) {
|
||||
for _, prefix := range nextExportDynamicPrefixes {
|
||||
if path, ok := nextExportIDTemplate(cleanPath, prefix); ok {
|
||||
return path, true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
//nolint:unused // Used by frontend.go in embed_frontend builds; default lint runs without that tag.
|
||||
func nextExportIDTemplate(cleanPath, prefix string) (string, bool) {
|
||||
if cleanPath == prefix || !strings.HasPrefix(cleanPath, prefix+"/") {
|
||||
return "", false
|
||||
}
|
||||
rest := strings.TrimPrefix(cleanPath, prefix+"/")
|
||||
if rest == "" {
|
||||
return "", false
|
||||
}
|
||||
parts := strings.Split(rest, "/")
|
||||
switch {
|
||||
case len(parts) == 1 && !strings.Contains(parts[0], "."):
|
||||
return prefix + "/1.html", true
|
||||
case len(parts) == 1 && strings.HasSuffix(parts[0], ".txt"):
|
||||
return prefix + "/1.txt", true
|
||||
case len(parts) == 2 && parts[1] != "" && strings.HasPrefix(parts[1], "__next.") && strings.HasSuffix(parts[1], ".txt"):
|
||||
return prefix + "/1/" + parts[1], true
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,10 @@ func TestResolveNextExportDynamicFallbackUsesZoneTemplate(t *testing.T) {
|
||||
"websites/1.html": &fstest.MapFile{Data: []byte("zone detail")},
|
||||
"websites/1.txt": &fstest.MapFile{Data: []byte("zone flight")},
|
||||
"websites/1/__next.!KG1haW4p.websites.$d$zoneId.__PAGE__.txt": &fstest.MapFile{Data: []byte("zone segment")},
|
||||
"cloudflare/groups.html": &fstest.MapFile{Data: []byte("groups list redirect")},
|
||||
"cloudflare/groups/1.html": &fstest.MapFile{Data: []byte("group detail")},
|
||||
"cloudflare/groups/1.txt": &fstest.MapFile{Data: []byte("group flight")},
|
||||
"cloudflare/groups/1/__next.!KG1haW4p.cloudflare.groups.$d$id.__PAGE__.txt": &fstest.MapFile{Data: []byte("group segment")},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
@@ -28,15 +32,22 @@ func TestResolveNextExportDynamicFallbackUsesZoneTemplate(t *testing.T) {
|
||||
input: "websites/3/__next.!KG1haW4p.websites.$d$zoneId.__PAGE__.txt",
|
||||
want: "websites/1/__next.!KG1haW4p.websites.$d$zoneId.__PAGE__.txt",
|
||||
},
|
||||
{name: "cloudflare group html", input: "cloudflare/groups/2", want: "cloudflare/groups/1.html"},
|
||||
{name: "cloudflare group route payload", input: "cloudflare/groups/2.txt", want: "cloudflare/groups/1.txt"},
|
||||
{
|
||||
name: "cloudflare group segment payload",
|
||||
input: "cloudflare/groups/3/__next.!KG1haW4p.cloudflare.groups.$d$id.__PAGE__.txt",
|
||||
want: "cloudflare/groups/1/__next.!KG1haW4p.cloudflare.groups.$d$id.__PAGE__.txt",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, ok := resolveNextExportDynamicFallback(subFS, tt.input)
|
||||
if !ok {
|
||||
t.Fatal("expected dynamic websites route fallback")
|
||||
t.Fatalf("resolveNextExportDynamicFallback(%q) ok = false, want true", tt.input)
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Fatalf("expected %q fallback, got %q", tt.want, got)
|
||||
t.Fatalf("resolveNextExportDynamicFallback(%q) = %q, want %q", tt.input, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -53,6 +64,13 @@ func TestResolveNextExportDynamicFallbackRejectsNestedOrAssetPath(t *testing.T)
|
||||
"websites/3.js",
|
||||
"websites/3/",
|
||||
"websites/3/missing.txt",
|
||||
"cloudflare",
|
||||
"cloudflare/groups",
|
||||
"cloudflare/groups.html",
|
||||
"cloudflare/groups/2/settings",
|
||||
"cloudflare/groups/2.js",
|
||||
"cloudflare/groups/2/",
|
||||
"cloudflare/groups/2/missing.txt",
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt, func(t *testing.T) {
|
||||
@@ -69,6 +87,9 @@ func TestResolveNextExportDynamicFallbackRequiresGeneratedTemplate(t *testing.T)
|
||||
}
|
||||
|
||||
if got, ok := resolveNextExportDynamicFallback(subFS, "websites/3"); ok {
|
||||
t.Fatalf("expected no fallback without generated template, got %q", got)
|
||||
t.Fatalf("resolveNextExportDynamicFallback(%q) = %q, want no fallback", "websites/3", got)
|
||||
}
|
||||
if got, ok := resolveNextExportDynamicFallback(subFS, "cloudflare/groups/2"); ok {
|
||||
t.Fatalf("resolveNextExportDynamicFallback(%q) = %q, want no fallback", "cloudflare/groups/2", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user