honour aliases as permanent redirects
An alias is a promise that an old URL keeps working, so it answers 301 to the canonical one rather than serving the same content twice (ADR-0008). Frontmatter takes a scalar or a list and tolerates surrounding slashes, because authors write both. Ambiguity is dropped, not resolved: an alias naming a real bundle, or claimed by two bundles, is logged and ignored so the real bundle keeps its URL. Aliases compose with language prefixes for free, since the resolver splits the language before the key is looked up. The redirect still fires only for an alias that exists, so a nonexistent path cannot be probed by 301 — the property prompt 3 established. Evidence: /pages/bio/ and /about/ both 301 to /pages/about/, /bn/pages/bio/ 301s to /bn/pages/about/, and /pages/nothing/ is 404.
This commit is contained in:
@@ -215,3 +215,57 @@ func TestURLPrefixesOnlyNonDefaultLanguages(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAliasesAreParsedAndIndexed(t *testing.T) {
|
||||
fsys := fstest.MapFS{
|
||||
"content/posts/new-name.md": {Data: []byte("---\ntitle: New\naliases: [/posts/old-name/, posts/older]\n---\n")},
|
||||
"content/pages/single.md": {Data: []byte("---\ntitle: Single\naliases: pages/one\n---\n")},
|
||||
}
|
||||
bundles, err := Scan(fsys)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
site := NewSite(bundles)
|
||||
for alias, want := range map[string]string{
|
||||
"posts/old-name": "posts/new-name",
|
||||
"posts/older": "posts/new-name",
|
||||
"pages/one": "pages/single",
|
||||
} {
|
||||
got, ok := site.Alias(alias)
|
||||
if !ok || got != want {
|
||||
t.Errorf("Alias(%q) = %q %v, want %q", alias, got, ok, want)
|
||||
}
|
||||
}
|
||||
for _, b := range bundles {
|
||||
if _, leaked := b.Extra["aliases"]; leaked {
|
||||
t.Error("aliases should be lifted out of Extra, not duplicated")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAmbiguousAliasesAreDropped(t *testing.T) {
|
||||
fsys := fstest.MapFS{
|
||||
"content/pages/real.md": {Data: []byte("---\ntitle: Real\n---\n")},
|
||||
"content/pages/a.md": {Data: []byte("---\ntitle: A\naliases: [pages/real, pages/shared]\n---\n")},
|
||||
"content/pages/b.md": {Data: []byte("---\ntitle: B\naliases: [pages/shared]\n---\n")},
|
||||
}
|
||||
site := NewSite(mustScan(t, fsys))
|
||||
if _, ok := site.Alias("pages/real"); ok {
|
||||
t.Error("an alias naming a real bundle must be dropped, not shadow it")
|
||||
}
|
||||
if _, ok := site.Alias("pages/shared"); ok {
|
||||
t.Error("an alias claimed by two bundles must be dropped, not picked arbitrarily")
|
||||
}
|
||||
if _, _, ok := site.Lookup("pages/real", "en"); !ok {
|
||||
t.Error("the real bundle must keep its URL")
|
||||
}
|
||||
}
|
||||
|
||||
func mustScan(t *testing.T, fsys fstest.MapFS) []Bundle {
|
||||
t.Helper()
|
||||
b, err := Scan(fsys)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user