Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support redirect, headers, and cors route rules #538

Merged
merged 14 commits into from Oct 11, 2022
Merged

Conversation

danielroe
Copy link
Member

@danielroe danielroe commented Sep 30, 2022

πŸ”— Linked issue

resolves #535, resolves #539, resolves #95

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This implements redirect route rules (including platform-specific support for vercel + netlify cdns).

It also implements headers and cors route rules. Apologies @pi0, I spotted too late you had self-assigned the cors route rule. 😬 Not my intention; happy to revert it - or feel free to amend PR.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@danielroe danielroe added the enhancement New feature or request label Sep 30, 2022
@danielroe danielroe requested a review from pi0 September 30, 2022 13:12
@danielroe danielroe self-assigned this Sep 30, 2022
@codecov
Copy link

codecov bot commented Sep 30, 2022

Codecov Report

Merging #538 (afc39f5) into main (4e0ee44) will increase coverage by 0.70%.
The diff coverage is 71.73%.

@@            Coverage Diff             @@
##             main     #538      +/-   ##
==========================================
+ Coverage   65.34%   66.04%   +0.70%     
==========================================
  Files          55       55              
  Lines        3970     4049      +79     
  Branches      418      437      +19     
==========================================
+ Hits         2594     2674      +80     
+ Misses       1372     1371       -1     
  Partials        4        4              
Impacted Files Coverage Ξ”
src/presets/netlify.ts 54.41% <57.37%> (+30.27%) ⬆️
src/presets/vercel.ts 68.18% <100.00%> (+5.68%) ⬆️
src/types/nitro.ts 100.00% <100.00%> (ΓΈ)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@danielroe danielroe changed the title feat: add support for redirect route rules feat: add support for redirect, headers, and cors route rules Oct 2, 2022
@danielroe danielroe changed the title feat: add support for redirect, headers, and cors route rules feat: support redirect, headers, and cors route rules Oct 11, 2022
@danielroe danielroe merged commit 1026edb into main Oct 11, 2022
@danielroe danielroe deleted the feat/redirects branch October 11, 2022 11:46
src/runtime/app.ts Show resolved Hide resolved
'access-control-allow-origin': '*',
'access-control-allowed-methods': '*',
'access-control-allow-headers': '*',
'access-control-max-age': '0'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might refactor this to h3 later and support cors options (rather than a boolean). Alternatively this could be compiled into headers for better cross platform support

src/runtime/app.ts Show resolved Hide resolved
for (const [key, value] of Object.entries(nitro.options.routes).filter(([_, value]) => value.redirect)) {
const redirect = typeof value.redirect === 'string' ? { to: value.redirect } : value.redirect
// TODO: update to 307 when netlify support 307/308
contents = `${key.replace('/**', '/*')}\t${redirect.to}\t${redirect.statusCode || 301}\n` + contents
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defaults (301 fallback) shall be done directly in nitro. For fixing 307/308 not supported, we need to check for them.

'access-control-allow-origin': '*',
'access-control-allowed-methods': '*',
'access-control-allow-headers': '*',
'access-control-max-age': '0'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(related to cors refactor)

if (value.redirect) {
const redirect = typeof value.redirect === 'string' ? { to: value.redirect } : value.redirect
route = defu(route, {
status: redirect.statusCode || 307,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defaults to be moved into nitro

@pi0 pi0 mentioned this pull request Oct 12, 2022
@pi0
Copy link
Member

pi0 commented Oct 12, 2022

Thanks for this amazing PR @danielroe <3 I've added some notes, they need to be addressed before next nitropack release ~> #570 (selfassigned)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to enable cors for /public Built-in redirects support cors support for route rules
3 participants