Gavin Hofer
Gavin Hofer
TTCTheo's Typesafe Cult
Created by Gavin Hofer on 11/30/2024 in #questions
Clerk handshake causes slow initial load for NextJS standalone deployment
Solved: Implemented the initial redirect in a cloudfront viewer request function by doing the following: 1. Check the request uri path to make sure it's an initial request to a dynamic page. Don't do the redirect for static assets (e.g., next_), api routes, if query params from clerk exist, or if the __clerk_redirect_count cookie exists. 2. Parse the __session cookie. 3. If the cookie is expired (exp), then return a 307 to redirect to https://{session.iss}/v1/client/handshake?redirect_url={encoded_request_url}&suffixed_cookies=true. Set the cache-control header to no-store. Note: this approach seems to only work on the Clerk production instance, not development (in dev I always end up signed out after this flow). The diagrams I have above are for development - in production that final redirect does not happen.
3 replies