|
1 | 1 | import type { AuthObject, ClerkClient } from '@clerk/backend'; |
2 | 2 | import type { AuthenticateRequestOptions, ClerkRequest, RedirectFun, RequestState } from '@clerk/backend/internal'; |
3 | 3 | import { AuthStatus, constants, createClerkRequest, createRedirect } from '@clerk/backend/internal'; |
4 | | -import { isDevelopmentFromPublishableKey, isDevelopmentFromSecretKey } from '@clerk/shared/keys'; |
| 4 | +import { isDevelopmentFromSecretKey } from '@clerk/shared/keys'; |
| 5 | +import { handleNetlifyCacheInDevInstance } from '@clerk/shared/netlifyCacheHandler'; |
5 | 6 | import { isHttpOrHttps } from '@clerk/shared/proxy'; |
6 | 7 | import { handleValueOrFn } from '@clerk/shared/utils'; |
7 | 8 | import type { APIContext } from 'astro'; |
8 | 9 |
|
9 | 10 | import { authAsyncStorage } from '#async-local-storage'; |
10 | 11 |
|
11 | | -import { NETLIFY_CACHE_BUST_PARAM } from '../internal'; |
12 | 12 | import { buildClerkHotloadScript } from './build-clerk-hotload-script'; |
13 | 13 | import { clerkClient } from './clerk-client'; |
14 | 14 | import { createCurrentUser } from './current-user'; |
@@ -74,7 +74,11 @@ export const clerkMiddleware: ClerkMiddleware = (...args: unknown[]): any => { |
74 | 74 |
|
75 | 75 | const locationHeader = requestState.headers.get(constants.Headers.Location); |
76 | 76 | if (locationHeader) { |
77 | | - handleNetlifyCacheInDevInstance(locationHeader, requestState); |
| 77 | + handleNetlifyCacheInDevInstance({ |
| 78 | + locationHeader, |
| 79 | + requestStateHeaders: requestState.headers, |
| 80 | + publishableKey: requestState.publishableKey, |
| 81 | + }); |
78 | 82 |
|
79 | 83 | const res = new Response(null, { status: 307, headers: requestState.headers }); |
80 | 84 | return decorateResponseWithObservabilityHeaders(res, requestState); |
@@ -234,25 +238,6 @@ Check if signInUrl is missing from your configuration or if it is not an absolut |
234 | 238 | PUBLIC_CLERK_SIGN_IN_URL='SOME_URL' |
235 | 239 | PUBLIC_CLERK_IS_SATELLITE='true'`; |
236 | 240 |
|
237 | | -/** |
238 | | - * Prevents infinite redirects in Netlify's functions |
239 | | - * by adding a cache bust parameter to the original redirect URL. This ensures Netlify |
240 | | - * doesn't serve a cached response during the authentication flow. |
241 | | - */ |
242 | | -function handleNetlifyCacheInDevInstance(locationHeader: string, requestState: RequestState) { |
243 | | - // Only run on Netlify environment and Clerk development instance |
244 | | - // eslint-disable-next-line turbo/no-undeclared-env-vars |
245 | | - if (import.meta.env.NETLIFY && isDevelopmentFromPublishableKey(requestState.publishableKey)) { |
246 | | - const hasHandshakeQueryParam = locationHeader.includes('__clerk_handshake'); |
247 | | - // If location header is the original URL before the handshake redirects, add cache bust param |
248 | | - if (!hasHandshakeQueryParam) { |
249 | | - const url = new URL(locationHeader); |
250 | | - url.searchParams.append(NETLIFY_CACHE_BUST_PARAM, Date.now().toString()); |
251 | | - requestState.headers.set('Location', url.toString()); |
252 | | - } |
253 | | - } |
254 | | -} |
255 | | - |
256 | 241 | function decorateAstroLocal(clerkRequest: ClerkRequest, context: APIContext, requestState: RequestState) { |
257 | 242 | const { reason, message, status, token } = requestState; |
258 | 243 | context.locals.authToken = token; |
|
0 commit comments