Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/server/auth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
normalizeWithBasePath,
removeTrailingSlash
} from "../utils/pathUtils.js";
import { isPrefetch } from "../utils/request.js";
import {
ensureDefaultScope,
getScopeForAudience
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,16 @@
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"))
);
}

Check failure on line 31 in src/utils/request.ts

View workflow job for this annotation

GitHub Actions / Lint Code

Insert `⏎`
Loading