kit icon indicating copy to clipboard operation
kit copied to clipboard

Load function should re-run if it was prefetched with an error

Open alialaa opened this issue 3 years ago • 0 comments

Describe the problem

I am having a situation where I am calling an API in my load functions and storing an access token and a refresh token in http cookies. When an endpoint returns 401 I redirect to a refresh token endpoint, update the cookies and redirect back to the original request. Anyways the problem happens when multiple requests are sent at the same time. Because the refresh token can only be used once. If multiple requests are sent with an expired access token, some of them will return 401 because the refresh token will no longer be valid. I can get around this in load functions by calling 1 endpoint first and the followed by others:

const res1 = await fetch(...);
const [res2, res3] = await fetch();

But what I can't control is when having links with prefetch enabled and a user hovers over multiple of them at the same time. If the token is expired, some of the load functions will fail (401). But the token will actually be refreshed in another request. So now when I open a prefetched link that failed, I get the 401 error although the token was refreshed. Does it make sense that a load function re-runs if an error was thrown during prefetching?

Describe the proposed solution

If an error was thrown in a load function during prefetching, it should re-run wen visiting the actual page instead of showing the error.

Alternatives considered

No response

Importance

nice to have

Additional Information

No response

alialaa avatar Sep 09 '22 18:09 alialaa