Skip to content

Commit

Permalink
/media route rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
rtrembecky committed Dec 21, 2024
1 parent dbb2c12 commit 2419c7f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {NextRequest, NextResponse} from 'next/server'

import {apiMiddleware} from './middleware/apiMiddleware'
import {backendRewriteMiddleware} from './middleware/backendRewriteMiddleware'

export function middleware(req: NextRequest) {
const url = req.nextUrl

if (url.pathname.startsWith('/api')) {
return apiMiddleware(req)
return backendRewriteMiddleware({req, trailingSlash: true})
}
if (url.pathname.startsWith('/media')) {
return backendRewriteMiddleware({req, trailingSlash: false})
}

// https://nextjs.org/docs/app/building-your-application/routing/middleware#advanced-middleware-flags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import {NextRequest, NextResponse} from 'next/server'
import {addApiTrailingSlash} from '@/utils/addApiTrailingSlash'
import {getBackendServerUrl} from '@/utils/urlBase'

export const apiMiddleware = (req: NextRequest) => {
export const backendRewriteMiddleware = ({req, trailingSlash}: {req: NextRequest; trailingSlash: boolean}) => {
const {method, nextUrl} = req
const {pathname, search, href} = nextUrl

let newPathname = pathname

newPathname = addApiTrailingSlash(newPathname)
if (trailingSlash) newPathname = addApiTrailingSlash(newPathname)

const newUrl = new URL(`${newPathname}${search}`, getBackendServerUrl())

Expand Down

0 comments on commit 2419c7f

Please sign in to comment.