unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
5.85k stars 493 forks source link

[routeRules] Support `redirect: '<path> <statusCode>'` alias #1707

Open atinux opened 1 year ago

atinux commented 1 year ago

It would be nice to support:

'/get-started': { redirect: '/get-started/installation 301' },
// Similar to
'/get-started': { redirect: { to: '/get-started/installation', statusCode: 301 } },

I believe it could be done here: https://github.com/unjs/nitro/blob/11a241244a166b0733b924d16ce5a32e1be89005/src/options.ts#L419

pi0 commented 1 year ago

I think using 302 (non permanent) might be a better choice for default. It is also default for h3 util. (or 307)

We can think of introducing redirect: { code: 301 } route rule to apply 301 to all redirects by default when set for /**

manniL commented 1 year ago

@pi0 I think the idea is having '<ROUTE> <CODE>' as shortcute here. No defaults used

pi0 commented 1 year ago

Ah, I see! That would be dangerous considering URLs can actually include whitespace (which is valid and needs to be encoded finally). An alternative would be [route, code] wdyt?

'/get-started': { redirect: ['/get-started/installation', 301] },
atinux commented 1 year ago

I am ok with this too, about the whitespace in URL, it is not a best practice and in that case, CloudFlare and Netlify redirects file should fail as well.

But I guess for TypeScript intellisense it will be better with [route, code] 👍