medusa 2.11.1
20
16
macOS
No response
Why is Medusa constantly (!) polling for a user (always returns 401). This adds on average around 250ms of latency as we analyzed on our backend. In most (!) cases customers are not logged in. I would like to change the logic in our repo that we only fetch the customer if there is a _medusa_jwt_token present in the cookies => meaning a customer has been logged in or registered. Otherwise I would guard the fetchCustomer() call in the storefront: If there is no token present do not attempt to fetch a customer.
Does this make sense or could this introduce serious issues or flaws? Thanks!
For example in the frontend starter we do this::
export const retrieveCustomer = async (): Promise<StoreCustomer | null> => {
const headers = {
...(await getAuthHeaders()),
}
const next = {
...(await getCacheOptions("customers")),
}
return await sdk.client
.fetch<{ customer: StoreCustomer }>(`/store/customers/me`, {
method: "GET",
headers,
next,
})
.then(({ customer }) => customer)
.catch(() => null)
}
And before we fetch the auth headers:
export const getAuthHeaders = async (): Promise<
{ authorization: string } | {}
> => {
const token = (await nextCookies()).get("_medusa_jwt")?.value
if (token) {
return { authorization: `Bearer ${token}` }
}
return {}
}
Why would we even make the call if no token is in the cookie storage? Seems super unnecessary? I see there is some caching logic but especially for large websites like ours, crawlers like google would have no cache id and send a request every single time?!
Does this make sense?
Before we adjust this I wanted to hear the communities opinion and if there is a reason for this maybe.
Thanks
Only fetch the customer when a token is present
Medusa fetches the customer on every page load
nA