From 1b312ef4bdc6b495961eb09f877d661c38414556 Mon Sep 17 00:00:00 2001 From: Jimmy Lai Date: Thu, 10 Aug 2023 18:23:53 +0200 Subject: [PATCH 1/3] update test --- .../app-dir/app-client-cache/client-cache.test.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/e2e/app-dir/app-client-cache/client-cache.test.ts b/test/e2e/app-dir/app-client-cache/client-cache.test.ts index 4c2e451bbe37d..83e39e5f4514a 100644 --- a/test/e2e/app-dir/app-client-cache/client-cache.test.ts +++ b/test/e2e/app-dir/app-client-cache/client-cache.test.ts @@ -315,6 +315,18 @@ createNextDescribe( .text() expect(newNumber).not.toBe(randomNumber) + + await browser.eval(fastForwardTo, 30 * 1000) + + await browser.elementByCss('[href="/"]').click() + + const newNumber2 = await browser + .elementByCss('[href="/1"]') + .click() + .waitForElementByCss('#random-number') + .text() + + expect(newNumber2).not.toBe(newNumber) }) it('should refetch below the fold after 30 seconds', async () => { const randomLoadingNumber = await browser From a9dd3a25db7e2ab6d652c67a5cc9202cc471c0a1 Mon Sep 17 00:00:00 2001 From: Jimmy Lai Date: Thu, 10 Aug 2023 18:37:33 +0200 Subject: [PATCH 2/3] switch heuristics --- .../router-reducer/get-prefetch-cache-entry-status.ts | 2 +- test/e2e/app-dir/app-client-cache/client-cache.test.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/next/src/client/components/router-reducer/get-prefetch-cache-entry-status.ts b/packages/next/src/client/components/router-reducer/get-prefetch-cache-entry-status.ts index f2a6cedb134b0..ea6b2a3f32d5b 100644 --- a/packages/next/src/client/components/router-reducer/get-prefetch-cache-entry-status.ts +++ b/packages/next/src/client/components/router-reducer/get-prefetch-cache-entry-status.ts @@ -16,7 +16,7 @@ export function getPrefetchEntryCacheStatus({ lastUsedTime, }: PrefetchCacheEntry): PrefetchCacheEntryStatus { // if the cache entry was prefetched or read less than 30s ago, then we want to re-use it - if (Date.now() < (lastUsedTime ?? prefetchTime) + THIRTY_SECONDS) { + if (Date.now() < prefetchTime + THIRTY_SECONDS) { return lastUsedTime ? PrefetchCacheEntryStatus.reusable : PrefetchCacheEntryStatus.fresh diff --git a/test/e2e/app-dir/app-client-cache/client-cache.test.ts b/test/e2e/app-dir/app-client-cache/client-cache.test.ts index 83e39e5f4514a..8d369220885d8 100644 --- a/test/e2e/app-dir/app-client-cache/client-cache.test.ts +++ b/test/e2e/app-dir/app-client-cache/client-cache.test.ts @@ -304,7 +304,7 @@ createNextDescribe( expect(number).toBe(randomNumber) - await browser.eval(fastForwardTo, 30 * 1000) + await browser.eval(fastForwardTo, 5 * 1000) await browser.elementByCss('[href="/"]').click() @@ -314,7 +314,7 @@ createNextDescribe( .waitForElementByCss('#random-number') .text() - expect(newNumber).not.toBe(randomNumber) + expect(newNumber).toBe(randomNumber) await browser.eval(fastForwardTo, 30 * 1000) From eb203b5ebf80f7e9123cd108b91cd6ed7628ac26 Mon Sep 17 00:00:00 2001 From: Jimmy Lai Date: Fri, 11 Aug 2023 12:52:44 +0200 Subject: [PATCH 3/3] switch heuristics agian --- .../router-reducer/get-prefetch-cache-entry-status.ts | 2 +- .../components/router-reducer/reducers/navigate-reducer.ts | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/next/src/client/components/router-reducer/get-prefetch-cache-entry-status.ts b/packages/next/src/client/components/router-reducer/get-prefetch-cache-entry-status.ts index ea6b2a3f32d5b..f2a6cedb134b0 100644 --- a/packages/next/src/client/components/router-reducer/get-prefetch-cache-entry-status.ts +++ b/packages/next/src/client/components/router-reducer/get-prefetch-cache-entry-status.ts @@ -16,7 +16,7 @@ export function getPrefetchEntryCacheStatus({ lastUsedTime, }: PrefetchCacheEntry): PrefetchCacheEntryStatus { // if the cache entry was prefetched or read less than 30s ago, then we want to re-use it - if (Date.now() < prefetchTime + THIRTY_SECONDS) { + if (Date.now() < (lastUsedTime ?? prefetchTime) + THIRTY_SECONDS) { return lastUsedTime ? PrefetchCacheEntryStatus.reusable : PrefetchCacheEntryStatus.fresh diff --git a/packages/next/src/client/components/router-reducer/reducers/navigate-reducer.ts b/packages/next/src/client/components/router-reducer/reducers/navigate-reducer.ts index 35f99de505ef8..43232b9b45c4d 100644 --- a/packages/next/src/client/components/router-reducer/reducers/navigate-reducer.ts +++ b/packages/next/src/client/components/router-reducer/reducers/navigate-reducer.ts @@ -251,8 +251,11 @@ export function navigateReducer( // Unwrap cache data with `use` to suspend here (in the reducer) until the fetch resolves. const [flightData, canonicalUrlOverride] = readRecordValue(data!) - // important: we should only mark the cache node as dirty after we unsuspend from the call above - prefetchValues.lastUsedTime = Date.now() + // we only want to mark this once + if (!prefetchValues.lastUsedTime) { + // important: we should only mark the cache node as dirty after we unsuspend from the call above + prefetchValues.lastUsedTime = Date.now() + } // Handle case when navigating to page in `pages` from `app` if (typeof flightData === 'string') {