From a41dbd758ba2a3b2cac23d88b377baaf5f1a1db6 Mon Sep 17 00:00:00 2001 From: Zack Tanner Date: Tue, 31 Oct 2023 10:24:43 -0700 Subject: [PATCH] pr comments --- .../src/shared/lib/router/action-queue.ts | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/packages/next/src/shared/lib/router/action-queue.ts b/packages/next/src/shared/lib/router/action-queue.ts index df6c09d78eb22..c3ff9f7ab54bc 100644 --- a/packages/next/src/shared/lib/router/action-queue.ts +++ b/packages/next/src/shared/lib/router/action-queue.ts @@ -76,7 +76,8 @@ async function runAction({ function handleResult(nextState: AppRouterState) { // if we discarded this action, the state should also be discarded if (action.discarded) { - if (actionQueue.needsRefresh) { + // if a refresh is needed, we only want to trigger it once the action queue is empty + if (actionQueue.needsRefresh && actionQueue.pending === null) { actionQueue.needsRefresh = false actionQueue.dispatch( { @@ -140,26 +141,24 @@ function dispatchAction( setState(deferredPromise) }) - if (payload.type === ACTION_NAVIGATE && actionQueue.pending !== null) { - // Navigations take priority over any pending actions. - // Mark the pending action as discarded (so the state is never applied) and start the navigation action immediately. - actionQueue.pending.discarded = true - - // if the pending action was a server action, mark the queue as needing a refresh once events are processed - actionQueue.needsRefresh = - actionQueue.pending.payload.type === ACTION_SERVER_ACTION - + // Check if the queue is empty + if (actionQueue.pending === null) { + // The queue is empty, so add the action and start it immediately runAction({ actionQueue, action: newAction, setState, }) - return - } + } else if (payload.type === ACTION_NAVIGATE) { + // Navigations take priority over any pending actions. + // Mark the pending action as discarded (so the state is never applied) and start the navigation action immediately. + actionQueue.pending.discarded = true + + // if the pending action was a server action, mark the queue as needing a refresh once events are processed + if (actionQueue.pending.payload.type === ACTION_SERVER_ACTION) { + actionQueue.needsRefresh = true + } - // Check if the queue is empty - if (actionQueue.pending === null) { - // The queue is empty, so add the action and start it immediately runAction({ actionQueue, action: newAction,