|
|
import { nextTestSetup } from 'e2e-utils' |
|
|
import { retry, waitFor } from 'next-test-utils' |
|
|
|
|
|
describe('app dir - prefetching (custom staleTime)', () => { |
|
|
const { next, isNextDev } = nextTestSetup({ |
|
|
files: __dirname, |
|
|
skipDeployment: true, |
|
|
nextConfig: { |
|
|
experimental: { |
|
|
staleTimes: { |
|
|
static: 10, |
|
|
dynamic: 5, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}) |
|
|
|
|
|
if (isNextDev) { |
|
|
it('should skip next dev for now', () => {}) |
|
|
return |
|
|
} |
|
|
|
|
|
it('should not fetch again when a static page was prefetched when navigating to it twice', async () => { |
|
|
const browser = await next.browser('/404') |
|
|
let requests: string[] = [] |
|
|
|
|
|
browser.on('request', (req) => { |
|
|
requests.push(new URL(req.url()).pathname) |
|
|
}) |
|
|
await browser.eval('location.href = "/"') |
|
|
|
|
|
await retry(async () => { |
|
|
expect( |
|
|
requests.filter((request) => request === '/static-page') |
|
|
).toHaveLength(1) |
|
|
}) |
|
|
|
|
|
await browser |
|
|
.elementByCss('#to-static-page') |
|
|
.click() |
|
|
.waitForElementByCss('#static-page') |
|
|
|
|
|
await browser |
|
|
.elementByCss('#to-home') |
|
|
|
|
|
.click() |
|
|
|
|
|
.waitForElementByCss('#to-static-page') |
|
|
|
|
|
.click() |
|
|
|
|
|
.waitForElementByCss('#static-page') |
|
|
|
|
|
await retry(async () => { |
|
|
expect( |
|
|
requests.filter((request) => request === '/static-page') |
|
|
).toHaveLength(1) |
|
|
}) |
|
|
}) |
|
|
|
|
|
it('should fetch again when a static page was prefetched when navigating to it after the stale time has passed', async () => { |
|
|
const browser = await next.browser('/404') |
|
|
let requests: string[] = [] |
|
|
|
|
|
browser.on('request', (req) => { |
|
|
requests.push(new URL(req.url()).pathname) |
|
|
}) |
|
|
await browser.eval('location.href = "/"') |
|
|
|
|
|
await retry(async () => { |
|
|
expect( |
|
|
requests.filter((request) => request === '/static-page') |
|
|
).toHaveLength(1) |
|
|
}) |
|
|
|
|
|
await browser |
|
|
.elementByCss('#to-static-page') |
|
|
.click() |
|
|
.waitForElementByCss('#static-page') |
|
|
|
|
|
const linkToStaticPage = await browser |
|
|
.elementByCss('#to-home') |
|
|
|
|
|
.click() |
|
|
|
|
|
.waitForElementByCss('#to-static-page') |
|
|
|
|
|
|
|
|
await waitFor(10000) |
|
|
|
|
|
await linkToStaticPage.click() |
|
|
|
|
|
await browser.waitForElementByCss('#static-page') |
|
|
|
|
|
await retry(async () => { |
|
|
expect( |
|
|
requests.filter((request) => request === '/static-page') |
|
|
).toHaveLength(2) |
|
|
}) |
|
|
}) |
|
|
|
|
|
it('should not re-fetch cached data when navigating back to a route group', async () => { |
|
|
const browser = await next.browser('/prefetch-auto-route-groups') |
|
|
|
|
|
expect(await browser.elementById('count').text()).toBe('1') |
|
|
|
|
|
|
|
|
await browser |
|
|
.elementByCss("[href='/prefetch-auto-route-groups/sub/foo']") |
|
|
.click() |
|
|
|
|
|
|
|
|
await browser.elementByCss("[href='/prefetch-auto-route-groups']").click() |
|
|
|
|
|
|
|
|
expect(await browser.elementById('count').text()).toBe('1') |
|
|
|
|
|
|
|
|
await browser |
|
|
.elementByCss("[href='/prefetch-auto-route-groups/sub/bar']") |
|
|
.click() |
|
|
|
|
|
|
|
|
await browser.elementByCss("[href='/prefetch-auto-route-groups']").click() |
|
|
|
|
|
|
|
|
expect(await browser.elementById('count').text()).toBe('1') |
|
|
|
|
|
await browser.refresh() |
|
|
|
|
|
|
|
|
expect(await browser.elementById('count').text()).toBe('4') |
|
|
}) |
|
|
|
|
|
it('should fetch again when the initially visited static page is visited after the stale time has passed', async () => { |
|
|
const browser = await next.browser('/404') |
|
|
let requests: string[] = [] |
|
|
|
|
|
browser.on('request', (req) => { |
|
|
const path = new URL(req.url()).pathname |
|
|
const headers = req.headers() |
|
|
|
|
|
if (headers['rsc']) { |
|
|
requests.push(path) |
|
|
} |
|
|
}) |
|
|
|
|
|
await browser.eval('location.href = "/static-page-no-prefetch"') |
|
|
|
|
|
await browser |
|
|
.elementByCss('#to-home') |
|
|
.click() |
|
|
.waitForElementByCss('#to-static-page') |
|
|
|
|
|
|
|
|
await waitFor(10000) |
|
|
|
|
|
await browser.elementByCss('#to-static-page-no-prefetch').click() |
|
|
|
|
|
|
|
|
await browser.waitForElementByCss('#static-page-no-prefetch') |
|
|
|
|
|
await retry(async () => { |
|
|
expect( |
|
|
requests.filter((request) => request === '/static-page-no-prefetch') |
|
|
).toHaveLength(1) |
|
|
}) |
|
|
}) |
|
|
|
|
|
it('should renew the stale time after refetching expired RSC data', async () => { |
|
|
const browser = await next.browser('/404') |
|
|
let requests: string[] = [] |
|
|
|
|
|
browser.on('request', (req) => { |
|
|
requests.push(new URL(req.url()).pathname) |
|
|
}) |
|
|
|
|
|
|
|
|
await browser.eval('location.href = "/"') |
|
|
|
|
|
await retry(async () => { |
|
|
expect( |
|
|
requests.filter((request) => request === '/static-page') |
|
|
).toHaveLength(1) |
|
|
}) |
|
|
|
|
|
|
|
|
await browser |
|
|
.elementByCss('#to-static-page') |
|
|
.click() |
|
|
.waitForElementByCss('#static-page') |
|
|
|
|
|
|
|
|
await browser |
|
|
.elementByCss('#to-home') |
|
|
.click() |
|
|
.waitForElementByCss('#to-static-page') |
|
|
|
|
|
|
|
|
await waitFor(10000) |
|
|
|
|
|
|
|
|
await browser |
|
|
.elementByCss('#to-static-page') |
|
|
.click() |
|
|
.waitForElementByCss('#static-page') |
|
|
|
|
|
|
|
|
await retry(async () => { |
|
|
expect( |
|
|
requests.filter((request) => request === '/static-page') |
|
|
).toHaveLength(2) |
|
|
}) |
|
|
|
|
|
|
|
|
await browser |
|
|
.elementByCss('#to-home') |
|
|
.click() |
|
|
.waitForElementByCss('#to-static-page') |
|
|
|
|
|
|
|
|
await waitFor(5000) |
|
|
|
|
|
|
|
|
await browser |
|
|
.elementByCss('#to-static-page') |
|
|
.click() |
|
|
.waitForElementByCss('#static-page') |
|
|
|
|
|
|
|
|
|
|
|
await retry(async () => { |
|
|
expect( |
|
|
requests.filter((request) => request === '/static-page') |
|
|
).toHaveLength(2) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
|