File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -66,11 +66,12 @@ export const loader = async ({ request }: LoaderFunctionArgs) => {
6666 headers . append ( "Set-Cookie" , await commitSession ( session ) ) ;
6767
6868 // Lazy-backfill the auth session's `issuedAt` for cookies issued before this
69- // feature shipped, and refresh the cookie's Max-Age to track the user's
70- // current effective session duration .
69+ // feature shipped. Returns null (and does not commit) once issuedAt is set,
70+ // so the cookie isn't re-written on every page load .
7171 if ( user ) {
7272 const authSession = await getUserSession ( request ) ;
73- headers . append ( "Set-Cookie" , await commitAuthenticatedSessionLazy ( authSession ) ) ;
73+ const lazyCookie = await commitAuthenticatedSessionLazy ( authSession ) ;
74+ if ( lazyCookie ) headers . append ( "Set-Cookie" , lazyCookie ) ;
7475 }
7576
7677 return typedjson (
Original file line number Diff line number Diff line change @@ -186,14 +186,17 @@ export async function commitAuthenticatedSession(
186186}
187187
188188/**
189- * Commits the session for an authenticated user, lazily backfilling
190- * `issuedAt` if missing. Use on every authenticated response that already
191- * commits the cookie (e.g. root.tsx).
189+ * Lazily backfills `issuedAt` on legacy auth sessions that predate the
190+ * sessionDuration feature. Returns the cookie string when a backfill happened
191+ * (caller must append it to the response's `Set-Cookie` headers), or `null`
192+ * when the session already had `issuedAt` set — avoiding an unnecessary
193+ * Set-Cookie on every authenticated page load and preventing the cookie's
194+ * 1-year Max-Age from rolling forward indefinitely.
192195 */
193196export async function commitAuthenticatedSessionLazy (
194197 session : Session ,
195198 now : number = Date . now ( )
196- ) : Promise < string > {
197- ensureSessionIssuedAt ( session , now ) ;
199+ ) : Promise < string | null > {
200+ if ( ! ensureSessionIssuedAt ( session , now ) ) return null ;
198201 return commitSession ( session , { maxAge : DEFAULT_SESSION_DURATION_SECONDS } ) ;
199202}
You can’t perform that action at this time.
0 commit comments