-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
getAuthenticatedUser
no longer updates / re-renders after React Router v6 upgrade
#591
Comments
getAuthenticatedUser
no longer updates / forces re-render after React Router v6 upgradegetAuthenticatedUser
no longer updates / re-renders after React Router v6 upgrade
If pulling We may also want to improve documentation so consumers have more direction in knowing when to use |
It may also be worth considering why the change in behavior for relying on |
After thoroughly investigating the issue, we have identified the following behavior:
function MyComponent() {
const authenticatedUser = getAuthenticatedUser();
console.log (authenticatedUser);
return (
<> ... </>;
);
}
Our conclusion is that the issue lies not with
By following these steps, the component will correctly re-render in response to changes in user metadata. |
Thanks for further investigating, @Syed-Ali-Abbas-Zaidi!
I'm still curious why upgrading to React Router v6 is what triggered this change in behavior with re-rendering (e.g., are there any other impacted functions?). Though, I agree we should be relying on For the purpose of this issue, we may want to consider whether there's any documentation improvements that could be made so consumers are more readily aware that they shouldn't rely on From the 2nd link above, about the
This documentation would lead consumers to believe the hydrated user metadata should always be returned by There is also a semi-related comment to this effect at the following link but consumers aren't often looking at the internals of how frontend-platform/src/initialize.js Lines 148 to 154 in 081a269
I feel it'd might be a good exercise to ensure the documentation around |
Description
Seemingly since this PR to upgrade
@edx/frontend-platform
to React Router v6, components seem to need to be explicitly subscribed to theAppContext
when retrieving the authenticated user object within a component as opposed to continuing to rely ongetAuthenticatedUser
.This change in behavior resulted in several critical user flows regressing/breaking on production for the
frontend-app-learner-portal-enterprise
MFE since this PR merged (e.g., infinite loading spinners because the MFE expectsgetAuthenticatedUser
to always represent the full metadata about the user, and it no longer always does). The React Router v6 upgrade was since reverted in this MFE as a result.Context
The
initialize
function (entry point for MFEs) allows consumers to optionallyhydrateAuthenticatedUser
which tells@edx/frontend-platform
to make an API call (http://localhost:18000/api/user/v1/accounts/<username>
)to retrieve additional metadata about the user (e.g., name, profile image, etc.).For MFEs, there's generally 2 mechanisms to extract the user's metadata from
@edx/frontend-platform
, both of which are patterns used by MFEs today:getAuthenticatedUser
The
getAuthenticatedUser
function (docs / source) ultimately calls theAxiosJwtAuthService.getAuthenticatedUser
function which simply returns whateverthis.authenticatedUser
it has available.AppContext
The
AppContext
context provider includes (presumably) the sameauthenticatedUser
object as should be returned bygetAuthenticatedUser()
. However, by subscribing to theAppContext
, any time theauthenticatedUser
changes, the component is guaranteed to re-render with the latestauthenticatedUser
data.Regression
The regression noticed in
frontend-app-learner-portal-enterprise
and also reproducible in frontend-platform example MFE is that after the React Router v6 upgrade in@edx/frontend-platform
, relying ongetAuthenticatedUser
is not guaranteed to have the correct metadata MFE developers might expect.When debugging, it was observed that when relying on
getAuthenticatedUser
before React Router v6 upgrade, the component first renders with the minimal set of user metadata but after a re-render of the component, it then contains the full metadata as expected.Now, after React Router v6 upgrade, there is only a single call to
getAuthenticatedUser
returning the minimal user metadata (i.e., missing the hydrated metadata from the API). This seems to be an unexpected change in behavior.The known workaround is to migrate components away from
getAuthenticatedUser()
in favor of pulling fromAppContext
instead to ensure the component is properly re-rendered in order to have the fully hydrated user metadata. That said, an ideal fix would ensuregetAuthenticatedUser()
always returns the same metadata as returned byAppContext
as most consumers would likely expect both mechanisms to have the same behavior, returning the same hydrated user metadata, when used within a component.Actual
The behavior after React Router 6 upgrade shows a single
console.log
of the minimal authenticated user metadata. It is missing the full hydrated user metadata including properties likeprofileImage
, etc. until something triggers a re-render of the component.Expected
The behavior before React Router v6 upgrade shows the
console.log
of the minimal authenticated user metadata first, then an automatic component re-render which then forces anotherconsole.log
including the full authenticated user metadata instead.The text was updated successfully, but these errors were encountered: