Skip to content

Commit

Permalink
feat: add support for redirect route rules
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Sep 30, 2022
1 parent 2049def commit 34424d3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/presets/netlify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export const netlify = defineNitroPreset({
async 'compiled' (nitro: Nitro) {
const redirectsPath = join(nitro.options.output.publicDir, '_redirects')
let contents = '/* /.netlify/functions/server 200'

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
contents = `${key.replace('/**', '/*')}\t${redirect.to}\t${redirect.statusCode || 307}` + '\n' + contents
}

if (existsSync(redirectsPath)) {
const currentRedirects = await fsp.readFile(redirectsPath, 'utf-8')
if (currentRedirects.match(/^\/\* /m)) {
Expand All @@ -28,6 +34,7 @@ export const netlify = defineNitroPreset({
nitro.logger.info('Adding Nitro fallback to `_redirects` to handle all unmatched routes.')
contents = currentRedirects + '\n' + contents
}

await fsp.writeFile(redirectsPath, contents)

const serverCJSPath = join(nitro.options.output.serverDir, 'server.js')
Expand Down
8 changes: 8 additions & 0 deletions src/presets/vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ function generateBuildConfig (nitro: Nitro) {
)
),
routes: [
...Object.entries(nitro.options.routes).filter(([_, value]) => value.redirect).map(([key, value]) => {
const redirect = typeof value.redirect === 'string' ? { to: value.redirect } : value.redirect
return {
src: key.replace('/**', '/.*'),
status: redirect.statusCode || 307,
headers: { Location: redirect.to }
}
}),
...nitro.options.publicAssets
.filter(asset => !asset.fallthrough)
.map(asset => asset.baseURL)
Expand Down
9 changes: 8 additions & 1 deletion src/runtime/app.ts
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 } from 'h3'
import { createFetch, Headers } from 'ohmyfetch'
import destr from 'destr'
import { createRouter as createMatcher } from 'radix3'
Expand Down Expand Up @@ -36,6 +36,13 @@ function createNitroApp (): NitroApp {

const routerOptions = createMatcher({ routes: config.nitro.routes })

h3App.use(eventHandler((event) => {
const routeOptions = routerOptions.lookup(event.req.url) || {}
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

Expand Down
2 changes: 1 addition & 1 deletion src/types/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export interface NitroConfig extends DeepPartial<NitroOptions> {

export interface NitroRouteOption {
swr?: boolean | number
redirect?: string
redirect?: string | { to: string, statusCode?: 307 | 308 }
}

export interface NitroRoutesOptions {
Expand Down

0 comments on commit 34424d3

Please sign in to comment.