From eb0d031bf8db10befd2f4023d86abc52f6c0cf05 Mon Sep 17 00:00:00 2001 From: Nandan Bhat Date: Thu, 23 Oct 2025 22:41:02 +0530 Subject: [PATCH] draft: working code --- src/server/auth-client.ts | 14 ++++++++++++++ src/utils/request.ts | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/server/auth-client.ts b/src/server/auth-client.ts index 4a999d04..15e989f4 100644 --- a/src/server/auth-client.ts +++ b/src/server/auth-client.ts @@ -60,6 +60,7 @@ import { normalizeWithBasePath, removeTrailingSlash } from "../utils/pathUtils.js"; +import { isPrefetch } from "../utils/request.js"; import { ensureDefaultScope, getScopeForAudience @@ -374,6 +375,19 @@ export class AuthClient { const sanitizedPathname = removeTrailingSlash(pathname); const method = req.method; + if ( + method === "GET" && + isPrefetch(req) && + Object.values(this.routes).includes(sanitizedPathname) + ) { + return new NextResponse(null, { + status: 204, + headers: { + "cache-control": "no-store" + } + }); + } + if (method === "GET" && sanitizedPathname === this.routes.login) { return this.handleLogin(req); } else if (method === "GET" && sanitizedPathname === this.routes.logout) { diff --git a/src/utils/request.ts b/src/utils/request.ts index 9461fcf3..b5fbbd35 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -16,3 +16,16 @@ export const isRequest = (req: Req): boolean => { typeof (req as Request).bodyUsed === "boolean" ); }; + +export function isPrefetch(req: NextRequest): boolean { + const h = req.headers; + return ( + h.get("next-router-prefetch") === "1" || + h.has("next-router-segment-prefetch") || + h.get("rsc") === "1" || + req.nextUrl.searchParams.has("_rsc") || + (h.get("sec-fetch-mode") === "cors" && + h.get("sec-fetch-dest") === "empty" && + h.has("next-url")) + ); +} \ No newline at end of file