Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.5.0 - Next 13 compatibility #257

Merged
merged 2 commits into from
Jan 9, 2025
Merged
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
8 changes: 7 additions & 1 deletion src/authMiddleware/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const handleMiddleware = async (req, options, onSuccess) => {

const isReturnToCurrentPage = options?.isReturnToCurrentPage;
const loginPage = options?.loginPage || "/api/auth/login";
const callbackPage = "/api/auth/kinde_callback";
const registerPage = "/api/auth/register";

if(loginPage == pathname || callbackPage == pathname || registerPage == pathname) {
return NextResponse.next();
}

let publicPaths = ["/_next", "/favicon.ico"];
if (options?.publicPaths !== undefined) {
Expand All @@ -28,7 +34,7 @@ const handleMiddleware = async (req, options, onSuccess) => {
? `${loginPage}?post_login_redirect_url=${pathname}`
: loginPage;

const isPublicPath = loginPage == pathname || publicPaths.some((p) => pathname.startsWith(p));
const isPublicPath = publicPaths.some((p) => pathname.startsWith(p));

// getAccessToken will validate the token
let kindeAccessToken = await getAccessToken(req);
Expand Down
4 changes: 3 additions & 1 deletion src/handlers/login.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { headers } from "next/headers";
import RouterClient from "../routerClients/RouterClient";
import { isPreFetch } from "../utils/isPreFetch";

Expand All @@ -6,7 +7,8 @@ import { isPreFetch } from "../utils/isPreFetch";
* @param {RouterClient} routerClient
*/
export const login = async (routerClient: RouterClient) => {
if (isPreFetch(routerClient.req)) {
const heads = await headers();
if (isPreFetch(heads)) {
return null;
}

Expand Down
4 changes: 3 additions & 1 deletion src/handlers/logout.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { headers } from "next/headers";
import { config } from "../config/index";
import RouterClient from "../routerClients/RouterClient";
import { isPreFetch } from "../utils/isPreFetch";
Expand All @@ -7,7 +8,8 @@ import { isPreFetch } from "../utils/isPreFetch";
* @param {RouterClient} routerClient
*/
export const logout = async (routerClient: RouterClient) => {
if (isPreFetch(routerClient.req)) {
const heads = await headers();
if (isPreFetch(heads)) {
return null
}

Expand Down
4 changes: 3 additions & 1 deletion src/handlers/register.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { headers } from "next/headers";
import RouterClient from "../routerClients/RouterClient";
import { isPreFetch } from "../utils/isPreFetch";

Expand All @@ -6,7 +7,8 @@ import { isPreFetch } from "../utils/isPreFetch";
* @param {RouterClient} routerClient
*/
export const register = async (routerClient: RouterClient) => {
if (isPreFetch(routerClient.req)) {
const heads = await headers();
if (isPreFetch(heads)) {
return null
}

Expand Down
36 changes: 13 additions & 23 deletions src/utils/isPreFetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,40 @@
// isPrefetch.test.ts
import { ReadonlyHeaders } from 'next/dist/server/web/spec-extension/adapters/headers';
import { isPreFetch } from './isPreFetch';
import { NextRequest } from 'next/server';
import { describe, expect, it } from 'vitest';

describe('isPreFetch', () => {
const mockNextRequest = (headers: Record<string, string>) => {
return {
headers: new Headers(headers)
} as NextRequest;
const mockHeaders = (headers: Record<string, string>) => {
return new Headers(headers) as ReadonlyHeaders
};

it('returns true when purpose header is prefetch', () => {
const req = mockNextRequest({ purpose: 'prefetch' });
expect(isPreFetch(req)).toBe(true);
const headers = mockHeaders({ purpose: 'prefetch' });
expect(isPreFetch(headers)).toBe(true);
});

it('returns true when x-purpose header is prefetch', () => {
const req = mockNextRequest({ 'x-purpose': 'prefetch' });
expect(isPreFetch(req)).toBe(true);
const headers = mockHeaders({ 'x-purpose': 'prefetch' });
expect(isPreFetch(headers)).toBe(true);
});

it('returns true when x-moz header is prefetch', () => {
const req = mockNextRequest({ 'x-moz': 'prefetch' });
expect(isPreFetch(req)).toBe(true);
const headers = mockHeaders({ 'x-moz': 'prefetch' });
expect(isPreFetch(headers)).toBe(true);
});

it('returns false when no prefetch headers are present', () => {
const req = mockNextRequest({});
expect(isPreFetch(req)).toBe(false);
const headers = mockHeaders({});
expect(isPreFetch(headers)).toBe(false);
});

it('returns false when headers have different values', () => {
const req = mockNextRequest({
const headers = mockHeaders({
purpose: 'navigation',
'x-purpose': 'fetch',
'x-moz': 'load'
});
expect(isPreFetch(req)).toBe(false);
});

it('handles undefined request gracefully', () => {
expect(isPreFetch(undefined as unknown as NextRequest)).toBe(false);
});

it('handles null headers gracefully', () => {
const req = { headers: null } as unknown as NextRequest;
expect(isPreFetch(req)).toBe(false);
expect(isPreFetch(headers)).toBe(false);
});
});
10 changes: 5 additions & 5 deletions src/utils/isPreFetch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { NextRequest } from "next/server";
import { ReadonlyHeaders } from "next/dist/server/web/spec-extension/adapters/headers";

export function isPreFetch(req: NextRequest): boolean {
const isPrefetch = req?.headers && (req?.headers.get('purpose') === 'prefetch' ||
req?.headers.get('x-purpose') === 'prefetch' ||
req?.headers.get('x-moz') === 'prefetch');
export function isPreFetch(headers: ReadonlyHeaders): boolean {
const isPrefetch = headers.get('purpose') === 'prefetch' ||
headers.get('x-purpose') === 'prefetch' ||
headers.get('x-moz') === 'prefetch';

return !!isPrefetch;
}