-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Remove inaccurate console warning for POP navigations #10030
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"react-router": patch | ||
"@remix-run/router": patch | ||
--- | ||
|
||
Remove inaccurate console warning for POP navigations |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -754,10 +754,6 @@ export function createRouter(init: RouterInit): Router { | |
// cancel active deferreds for eliminated routes. | ||
let activeDeferreds = new Map<string, DeferredData>(); | ||
|
||
// We ony support a single active blocker at the moment since we don't have | ||
// any compelling use cases for multi-blocker yet | ||
let activeBlocker: string | null = null; | ||
|
||
// Store blocker functions in a separate Map outside of router state since | ||
// we don't need to update UI state if they change | ||
let blockerFunctions = new Map<string, BlockerFunction>(); | ||
|
@@ -782,7 +778,7 @@ export function createRouter(init: RouterInit): Router { | |
} | ||
|
||
warning( | ||
activeBlocker != null && delta === null, | ||
blockerFunctions.size === 0 || delta != null, | ||
"You are trying to use a blocker on a POP navigation to a location " + | ||
"that was not created by @remix-run/router. This will fail silently in " + | ||
"production. This can happen if you are navigating outside the router " + | ||
|
@@ -2123,12 +2119,6 @@ export function createRouter(init: RouterInit): Router { | |
|
||
if (blockerFunctions.get(key) !== fn) { | ||
blockerFunctions.set(key, fn); | ||
if (activeBlocker == null) { | ||
// This is now the active blocker | ||
activeBlocker = key; | ||
} else if (key !== activeBlocker) { | ||
warning(false, "A router only supports one blocker at a time"); | ||
} | ||
Comment on lines
-2126
to
-2131
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While testing, I realized that this
Causing us to think we did not have a blocker. But that would get resolved as soon as we entered any form data causing the getBlocker to run again on the Even changing to an incrementing key and tracking that wasn't sufficient since the effects cleanup cleans up the second render:
Which also resulted in |
||
} | ||
|
||
return blocker; | ||
|
@@ -2137,9 +2127,6 @@ export function createRouter(init: RouterInit): Router { | |
function deleteBlocker(key: string) { | ||
state.blockers.delete(key); | ||
blockerFunctions.delete(key); | ||
if (activeBlocker === key) { | ||
activeBlocker = null; | ||
} | ||
} | ||
|
||
// Utility function to update blockers, ensuring valid state transitions | ||
|
@@ -2170,18 +2157,19 @@ export function createRouter(init: RouterInit): Router { | |
nextLocation: Location; | ||
historyAction: HistoryAction; | ||
}): string | undefined { | ||
if (activeBlocker == null) { | ||
if (blockerFunctions.size === 0) { | ||
return; | ||
} | ||
|
||
// We only allow a single blocker at the moment. This will need to be | ||
// updated if we enhance to support multiple blockers in the future | ||
let blockerFunction = blockerFunctions.get(activeBlocker); | ||
invariant( | ||
blockerFunction, | ||
"Could not find a function for the active blocker" | ||
); | ||
let blocker = state.blockers.get(activeBlocker); | ||
// We ony support a single active blocker at the moment since we don't have | ||
// any compelling use cases for multi-blocker yet | ||
if (blockerFunctions.size > 1) { | ||
warning(false, "A router only supports one blocker at a time"); | ||
} | ||
|
||
let entries = Array.from(blockerFunctions.entries()); | ||
let [blockerKey, blockerFunction] = entries[entries.length - 1]; | ||
let blocker = state.blockers.get(blockerKey); | ||
Comment on lines
+2170
to
+2172
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Active blocker is just the last (top) entry in the Map since entries are ordered by insertion time in a Map |
||
|
||
if (blocker && blocker.state === "proceeding") { | ||
// If the blocker is currently proceeding, we don't need to re-check | ||
|
@@ -2192,7 +2180,7 @@ export function createRouter(init: RouterInit): Router { | |
// At this point, we know we're unblocked/blocked so we need to check the | ||
// user-provided blocker function | ||
if (blockerFunction({ currentLocation, nextLocation, historyAction })) { | ||
return activeBlocker; | ||
return blockerKey; | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the incorrect boolean condition causing the warning, it should have been
activeBlocker == null || delta != null
since we only want to warn if we have a blocker and delta isnull
.