Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/five-numbers-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/backend": patch
---

Fixed an issue where TanStack React Start middleware fails to properly handle requests.
1 change: 0 additions & 1 deletion integration/templates/tanstack-react-start/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"@tanstack/react-start": "1.132.51",
"react": "18.3.1",
"react-dom": "18.3.1",
"srvx": "0.8.15",
"tailwind-merge": "^2.5.4"
},
"devDependencies": {
Expand Down
35 changes: 35 additions & 0 deletions packages/backend/src/tokens/__tests__/clerkRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,39 @@ describe('createClerkRequest', () => {
expect(json.cookies).toBe('{}');
});
});

describe('duck typing detection (instanceof workaround)', () => {
it('should create a new ClerkRequest from a regular Request', () => {
const regularRequest = new Request('http://localhost:3000');
const clerkRequest = createClerkRequest(regularRequest);

expect(clerkRequest).not.toBe(regularRequest);
expect(clerkRequest.clerkUrl).toBeDefined();
expect(clerkRequest.cookies).toBeDefined();
});

it('should return an existing ClerkRequest instance unchanged', () => {
const firstClerkRequest = createClerkRequest(new Request('http://localhost:3000'));
const secondClerkRequest = createClerkRequest(firstClerkRequest);

expect(secondClerkRequest).toBe(firstClerkRequest);
});

it('should work correctly with bundler-scoped Request classes', () => {
// Simulate bundler creating a scoped Request class (like Request$1)
class RequestScoped extends Request {
constructor(input: RequestInfo | URL, init?: RequestInit) {
super(input, init);
}
}

const scopedRequest = new RequestScoped('http://localhost:3000');
const clerkRequest = createClerkRequest(scopedRequest);

// Should create a new ClerkRequest even though scopedRequest is a different Request class
expect(clerkRequest).not.toBe(scopedRequest);
expect(clerkRequest.clerkUrl).toBeDefined();
expect(clerkRequest.cookies).toBeDefined();
});
});
});
7 changes: 6 additions & 1 deletion packages/backend/src/tokens/clerkRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ class ClerkRequest extends Request {
}

export const createClerkRequest = (...args: ConstructorParameters<typeof ClerkRequest>): ClerkRequest => {
return args[0] instanceof ClerkRequest ? args[0] : new ClerkRequest(...args);
// Use duck typing instead of instanceof to avoid issues with polyfilled Request classes
// (e.g., in TanStack Start or other environments with multiple Request class instances)
// ClerkRequest has unique properties 'clerkUrl' and 'cookies' that distinguish it from Request
const isClerkRequest = args[0] && typeof args[0] === 'object' && 'clerkUrl' in args[0] && 'cookies' in args[0];

return isClerkRequest ? (args[0] as ClerkRequest) : new ClerkRequest(...args);
};

export type { ClerkRequest };