From ce01eba9c09456a29968e287dc54617f77d01736 Mon Sep 17 00:00:00 2001 From: Max Leiter Date: Fri, 1 Apr 2022 21:17:33 -0700 Subject: [PATCH] client: don't run middleware if the request isn't a page --- client/pages/_middleware.tsx | 60 +++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/client/pages/_middleware.tsx b/client/pages/_middleware.tsx index dbcbe8e8..0de2bd35 100644 --- a/client/pages/_middleware.tsx +++ b/client/pages/_middleware.tsx @@ -1,41 +1,43 @@ 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 + if (signedIn) { + const resp = NextResponse.redirect(getURL('')); + resp.clearCookie('drift-token'); + resp.clearCookie('drift-userid'); - 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 - if (signedIn) { - const resp = NextResponse.redirect(getURL('')); - resp.clearCookie('drift-token'); - resp.clearCookie('drift-userid'); - - return resp - } - } else if (pathname === '/') { - if (signedIn) { - return NextResponse.redirect(getURL('new')) - } - // If you're not signed in we redirect the new post page to the home page - } else if (pathname === '/new') { - if (!signedIn) { - return NextResponse.redirect(getURL('signin')) - } - // If you're signed in we redirect the sign in page to the home page (which is the new page) - } else if (pathname === '/signin' || pathname === '/signup') { - if (signedIn) { - return NextResponse.redirect(getURL('')) + return resp + } + } else if (pathname === '/') { + if (signedIn) { + return NextResponse.redirect(getURL('new')) + } + // If you're not signed in we redirect the new post page to the home page + } else if (pathname === '/new') { + if (!signedIn) { + return NextResponse.redirect(getURL('signin')) + } + // If you're signed in we redirect the sign in page to the home page (which is the new page) + } else if (pathname === '/signin' || pathname === '/signup') { + if (signedIn) { + return NextResponse.redirect(getURL('')) + } } } + return NextResponse.next() }