Skip to content

Commit

Permalink
feat(blog): create middleware for rewrite post slug on runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusfg7 committed Sep 15, 2024
1 parent 8a1ab87 commit 3f684a1
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { NextRequest, MiddlewareConfig, NextResponse } from 'next/server'
import { slug } from './lib/slug'

export function middleware(request: NextRequest) {
if (request.nextUrl.pathname.startsWith('/blog/post')) {
const splittedPath = request.nextUrl.pathname
.split('/')
.filter(s => s !== '')

if (splittedPath.length === 3) {
return NextResponse.rewrite(
new URL(`/blog/post/${slug(splittedPath[2])}`, request.url)
)
}
}

return NextResponse.next()
}

export const config: MiddlewareConfig = {
matcher: [
'/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)'
]
}

0 comments on commit 3f684a1

Please sign in to comment.