Skip to content

Commit 971132e

Browse files
layouts: generate Netlify redirects from page aliases frontmatter
Update layouts/index.redirects to iterate all site pages and emit server-side Netlify redirect lines for each alias declared in page frontmatter. This allows content authors and localization teams to manage redirects directly via the aliases field without editing the central static/_redirects.base file. Implementation details: - Collects all RegularPages permalinks for conflict detection - Resolves relative aliases using path.Dir of the page's RelPermalink - Emits a warnf and skips any alias conflicting with a real page URL - Localized aliases are fully supported; they appear in production builds where all language segments are rendered
1 parent 3e847e5 commit 971132e

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

layouts/index.redirects

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,30 @@
1010
# Dynamic latest API redirect - automatically points to current latest version
1111
/docs/reference/generated/kubernetes-api/latest/ /docs/reference/generated/kubernetes-api/{{ $latest }}/ 307
1212
/docs/reference/generated/kubernetes-api/latest/* /docs/reference/generated/kubernetes-api/{{ $latest }}/:splat 307
13+
{{- end }}
14+
15+
# Redirects generated from page aliases frontmatter
16+
{{ $allPagePermalinks := slice -}}
17+
{{- range .Site.RegularPages -}}
18+
{{- $allPagePermalinks = $allPagePermalinks | append .RelPermalink -}}
1319
{{- end -}}
20+
{{- range .Site.Pages -}}
21+
{{- $page := . -}}
22+
{{- range $page.Aliases -}}
23+
{{- $alias := . -}}
24+
{{- if not (hasPrefix $alias "/") -}}
25+
{{- $parentDir := path.Dir (strings.TrimSuffix "/" $page.RelPermalink) -}}
26+
{{- $alias = printf "%s/%s/" $parentDir (strings.TrimSuffix "/" $alias) -}}
27+
{{- end -}}
28+
{{- /* Guard: warn if alias conflicts with a real page */ -}}
29+
{{- if in $allPagePermalinks $alias -}}
30+
{{- warnf "alias %q on page %q conflicts with an existing page permalink — skipping" $alias $page.RelPermalink -}}
31+
{{- else }}
32+
{{ $alias }} {{ $page.RelPermalink }} 301
33+
{{- end -}}
34+
{{- end -}}
35+
{{- end }}
1436

1537
# Include all existing static redirects
16-
{{- $staticRedirects := readFile "static/_redirects.base" -}}
17-
{{ $staticRedirects }}
38+
{{ $staticRedirects := readFile "static/_redirects.base" -}}
39+
{{- $staticRedirects }}

0 commit comments

Comments
 (0)