thuliteio / doks

Everything you need to build a stellar documentation website. Fast, accessible, and easy to use.
https://getdoks.org
MIT License
2.1k stars 361 forks source link

Document dynamic generation of Netlify `_redirects` file #1170

Open nk9 opened 2 years ago

nk9 commented 2 years ago

I have URLs in my site which look like this:

/episode/15/the-boy-and-the-whale

I would like users who navigate to /episode/15/ to be redirected to the full URL with post slug. While this can be done with alias: in the front matter, it's cleaner to dynamically generate the Netlify _redirects file.

I have done this on another site with the TND Redirects Netlify module. But it looks like Doks may have its own way to do this already:

config.toml:

home = ["HTML", "RSS", "REDIRECTS", "HEADERS"]

# add output format for netlify _redirects
[outputFormats.REDIRECTS]
mediaType = "text/netlify"
baseName = "_redirects"
isPlainText = true
notAlternative = true

I don't see any information in the docs about how to use this output format, though. So I'd like to know if this is already possible, or if I should try to integrate TND Redirects Netlify instead.

h-enk commented 2 years ago

Doks can handle this (similar to the TND approach). Not documented that well though — yet 😉

Redirect rules can be appended to /layouts/index.redirects, which is then built to /public/_redirects (as you can see configured in your excerpt of config.toml above).

The default /layouts/index.redirects:

{{- range $p := .Site.Pages -}}
{{- range .Aliases }}
{{ . }} {{ $p.RelPermalink -}}
{{- end }}
{{- end -}}

# /docs/1.0/prologue/   /docs/1.0/prologue/introduction/
# /docs/1.0/help/       /docs/1.0/help/how-to-update/
# /docs/1.0/            /docs/1.0/prologue/introduction/
# /docs/                /docs/1.0/prologue/introduction/
#
# /docs/0.1/*           https://v0-1-0--doks-versioning-poc.netlify.app/docs/0.1/:splat  200
# /docs/0.2/*           https://v0-2-0--doks-versioning-poc.netlify.app/docs/0.2/:splat  200

See also the Netlify docs: Redirects and rewrites.

Hugo Aliases are usually handled by <meta http-equiv="refresh" ...> tags. These have been disabled in ./config/_default/config.toml with disableAliases = true, and instead are handled by Netlify's Redirects and rewrites. This is handled automatically and you should continue to add aliases as described in the Hugo documentation.

To be added to the Doks docs!

nk9 commented 2 years ago

Oh, excellent, that's just what I was hoping for. Thank you!

h-enk commented 10 months ago

Handled by Netlify's Redirects and rewrites in netlify.toml

nk9 commented 10 months ago

Thanks, Henk. However, I still don't see anything in the docs about this. And it actually looks like /layouts/index.redirects is no longer there at HEAD on main. And outputFormat.REDIRECTS has been removed from config/_default/hugo.toml.

Using outputFormats.REDIRECTS and the index.redirects file is a dynamic solution, feasible for large numbers of redirects, in a way that just manually updating the netlify.toml isn't. Is a built-in, dynamic solution no longer supported?

h-enk commented 10 months ago

You're right! Would surely be interesting to re-add/base upon Regis' Redirects Hugo Module with Netlify. I'm thinking of making this a Hyas integration