-
Notifications
You must be signed in to change notification settings - Fork 500
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
Changes from 8 commits
34424d3
da12900
0dd265a
a31f544
7aff11a
bf6a218
26cc9fc
2941dfc
5dc9468
4c72c5b
207c1c2
1d97b74
4cf4a83
afc39f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,34 @@ function generateBuildConfig (nitro: Nitro) { | |
) | ||
), | ||
routes: [ | ||
...Object.entries(nitro.options.routes).filter(([_, value]) => value.redirect || value.headers || value.cors).map(([key, value]) => { | ||
let route = { | ||
src: key.replace('/**', '/.*') | ||
} | ||
if (value.redirect) { | ||
const redirect = typeof value.redirect === 'string' ? { to: value.redirect } : value.redirect | ||
route = defu(route, { | ||
status: redirect.statusCode || 307, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Defaults to be moved into nitro |
||
headers: { Location: redirect.to } | ||
}) | ||
} | ||
if (value.cors) { | ||
route = defu(route, { | ||
headers: { | ||
'access-control-allow-origin': '*', | ||
'access-control-allowed-methods': '*', | ||
'access-control-allow-headers': '*', | ||
'access-control-max-age': '0' | ||
} | ||
}) | ||
} | ||
if (value.headers) { | ||
route = defu(route, { | ||
headers: value.headers | ||
}) | ||
} | ||
return route | ||
}), | ||
...nitro.options.publicAssets | ||
.filter(asset => !asset.fallthrough) | ||
.map(asset => asset.baseURL) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { App as H3App, createApp, createRouter, lazyEventHandler, Router } from 'h3' | ||
import { App as H3App, createApp, createRouter, eventHandler, lazyEventHandler, Router, sendRedirect, setHeaders } from 'h3' | ||
import { createFetch, Headers } from 'ohmyfetch' | ||
import destr from 'destr' | ||
import { createRouter as createMatcher } from 'radix3' | ||
|
@@ -36,6 +36,26 @@ function createNitroApp (): NitroApp { | |
|
||
const routerOptions = createMatcher({ routes: config.nitro.routes }) | ||
|
||
h3App.use(eventHandler((event) => { | ||
const routeOptions = routerOptions.lookup(event.req.url) || {} | ||
pi0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Share applicable route rules across handlers | ||
event.context.routeOptions = routeOptions | ||
pi0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (routeOptions.cors) { | ||
setHeaders(event, { | ||
'access-control-allow-origin': '*', | ||
'access-control-allowed-methods': '*', | ||
'access-control-allow-headers': '*', | ||
'access-control-max-age': '0' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
}) | ||
} | ||
if (routeOptions.headers) { | ||
setHeaders(event, routeOptions.headers) | ||
} | ||
if (routeOptions.redirect) { | ||
return sendRedirect(event, routeOptions.redirect.to || routeOptions.redirect, routeOptions.redirect.statusCode || 307) | ||
} | ||
})) | ||
|
||
for (const h of handlers) { | ||
let handler = h.lazy ? lazyEventHandler(h.handler) : h.handler | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(related to cors refactor)