client: don't run middleware if the request isn't a page
This commit is contained in:
parent
f927fae9ed
commit
ce01eba9c0
1 changed files with 31 additions and 29 deletions
|
@ -1,17 +1,17 @@
|
|||
import { NextRequest, NextResponse } from 'next/server'
|
||||
|
||||
// const PUBLIC_FILE = /.(.*)$/
|
||||
const PUBLIC_FILE = /.(.*)$/
|
||||
|
||||
export function middleware(req: NextRequest) {
|
||||
const pathname = req.nextUrl.pathname
|
||||
const signedIn = req.cookies['drift-token']
|
||||
const getURL = (pageName: string) => new URL(`/${pageName}`, req.url).href
|
||||
// const isPageRequest =
|
||||
// !PUBLIC_FILE.test(req.nextUrl.pathname) &&
|
||||
// !req.nextUrl.pathname.startsWith('/api') &&
|
||||
// // header added when next/link pre-fetches a route
|
||||
// !req.headers.get('x-middleware-preflight')
|
||||
|
||||
const isPageRequest =
|
||||
!PUBLIC_FILE.test(req.nextUrl.pathname) &&
|
||||
!req.nextUrl.pathname.startsWith('/api') &&
|
||||
// header added when next/link pre-fetches a route
|
||||
!req.headers.get('x-middleware-preflight')
|
||||
if (isPageRequest) {
|
||||
if (pathname === '/signout') {
|
||||
// If you're signed in we remove the cookie and redirect to the home page
|
||||
// If you're not signed in we redirect to the home page
|
||||
|
@ -37,5 +37,7 @@ export function middleware(req: NextRequest) {
|
|||
return NextResponse.redirect(getURL(''))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue