fix(react): getSession should update session on current tab #11470
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
☕️ Reasoning
Calling
getSession
should trigger refetch of session by broadcasting a message. However, since the broadcast channel was changed to a single instance in #10762, the broadcast channel instance that sends the message will not receive it and will not update the SessionProvider context on the current tab because this is howBroadcastChannel
is supposed to work as specified in the specs:This commit creates a new
BroadcastChannel
instance just for posting a message. This message is received by all otherBroadcastChannel
instances that haveEventListener
, including the instance on the current tab. This message triggers__NEXTAUTH._getSession
and updates the session on all tabs.It does not break #10762, because the new instance is only used to post a message and no new EventListener is created, therefore no unnecessary
/session
requests are made.🧢 Checklist
Could possibly fix
#11075 #9504 #11075