Skip to content

Frontend: Auth: Move redirect function out of render cycle to avoid repeated redirects #1378

@rvveber

Description

@rvveber

Auth.tsx redirects are happening in component render cycle.
In combination with the serviceworker intercepting requests,
can lead to situations where the component is rerendering and making repeated requests fast.

We should move the redirection to a guarded useEffect.

Reproduce:

  1. Have service worker activated
  2. Delete the docs_sessionid cookie after already authenticated and reload.
  3. Should see multiple cancelled requests
    (for us in opendesk it ends in a loop for 25 or so requests per second)

Old issue description (before the issues cause was clearer)
The redirect to the `authenticate|callback|logout` endpoints should not be intercepted by the service worker. They need to be top-level redirects (window.location=...) for the browser to set the `docs_sessionid` cookie correctly.

They are not like regular api endpoints which just return cachable data, these endpoints can redirect and the browser must follow the redirect to properly set the session cookie.

Intercepting them at the service-worker for offline use, leads to the service-worker pre-checking if data is available with an XHR/fetch (browser will not follow) and causing the actual top-level redirect to be cancelled. Causes redirect loops.
Image

This is probably causd by the catch‑all API route matching /authenticate/:

registerRoute(
({ url }) => isApiUrl(url.href),
new NetworkFirst({
cacheName: getCacheNameVersion('api'),
plugins: [
new CacheableResponsePlugin({ statuses: [0, 200] }),
new ExpirationPlugin({
maxAgeSeconds: 24 * 60 * 60 * DAYS_EXP,
}),
new ApiPlugin({
type: 'synch',
syncManager,
}),
new OfflinePlugin(),
],
}),
'GET',
);

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingtriage

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions