File size: 22,863 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 |
import { nextTestSetup } from 'e2e-utils'
import { check, waitFor, retry } from 'next-test-utils'
import { NEXT_RSC_UNION_QUERY } from 'next/dist/client/components/app-router-headers'
import { computeCacheBustingSearchParam } from 'next/dist/shared/lib/router/utils/cache-busting-search-param'
const browserConfigWithFixedTime = {
beforePageLoad: (page) => {
page.addInitScript(() => {
const startTime = new Date()
const fixedTime = new Date('2023-04-17T00:00:00Z')
// Override the Date constructor
// @ts-ignore
// eslint-disable-next-line no-native-reassign
Date = class extends Date {
constructor() {
super()
// @ts-ignore
return new startTime.constructor(fixedTime)
}
static now() {
return fixedTime.getTime()
}
}
})
},
}
const itHeaded = process.env.HEADLESS ? it.skip : it
describe('app dir - prefetching', () => {
const { next, isNextDev, isNextDeploy } = nextTestSetup({
files: __dirname,
})
// TODO: re-enable for dev after https://vercel.slack.com/archives/C035J346QQL/p1663822388387959 is resolved (Sep 22nd 2022)
if (isNextDev) {
it('should skip next dev for now', () => {})
return
}
it('NEXT_RSC_UNION_QUERY query name is _rsc', async () => {
expect(NEXT_RSC_UNION_QUERY).toBe('_rsc')
})
it('should show layout eagerly when prefetched with loading one level down', async () => {
const browser = await next.browser('/', browserConfigWithFixedTime)
// Ensure the page is prefetched
await waitFor(1000)
const before = Date.now()
await browser
.elementByCss('#to-dashboard')
.click()
.waitForElementByCss('#dashboard-layout')
const after = Date.now()
const timeToComplete = after - before
expect(timeToComplete).toBeLessThan(1000)
expect(await browser.elementByCss('#dashboard-layout').text()).toBe(
'Dashboard Hello World'
)
await browser.waitForElementByCss('#dashboard-page')
expect(await browser.waitForElementByCss('#dashboard-page').text()).toBe(
'Welcome to the dashboard'
)
})
it('should not have prefetch error for static path', async () => {
const browser = await next.browser('/')
await browser.eval('window.nd.router.prefetch("/dashboard/123")')
await waitFor(3000)
await browser.eval('window.nd.router.push("/dashboard/123")')
expect(next.cliOutput).not.toContain('ReferenceError')
expect(next.cliOutput).not.toContain('is not defined')
})
it('should not have prefetch error when reloading before prefetch request is finished', async () => {
const browser = await next.browser('/')
await browser.eval('window.next.router.prefetch("/dashboard/123")')
await browser.refresh()
const logs = await browser.log()
expect(logs).not.toMatchObject(
expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining('Failed to fetch RSC payload'),
}),
])
)
})
itHeaded('should not suppress prefetches after navigating back', async () => {
// Force headed mode, as bfcache is not available in headless mode.
const browser = await next.browser('/', { headless: false })
// Trigger a hard navigation.
await browser.elementById('to-static-page-hard').click()
// Go back, utilizing the bfcache.
await browser.elementById('go-back').click()
let requests: string[] = []
browser.on('request', (req) => {
requests.push(new URL(req.url()).pathname)
})
await browser.eval('window.next.router.prefetch("/dashboard/123")')
await browser.waitForIdleNetwork()
expect(requests).toInclude('/dashboard/123')
})
it('should not fetch again when a static page was prefetched', async () => {
const browser = await next.browser('/404', browserConfigWithFixedTime)
let requests: string[] = []
browser.on('request', (req) => {
requests.push(new URL(req.url()).pathname)
})
await browser.eval('location.href = "/"')
await browser.eval(
'window.nd.router.prefetch("/static-page", {kind: "auto"})'
)
await check(() => {
return requests.some(
(req) =>
req.includes('static-page') && !req.includes(NEXT_RSC_UNION_QUERY)
)
? 'success'
: JSON.stringify(requests)
}, 'success')
await browser
.elementByCss('#to-static-page')
.click()
.waitForElementByCss('#static-page')
expect(
requests.filter((request) => request === '/static-page').length
).toBe(1)
// return to the home page
await browser.elementByCss('#to-home').click()
await browser.waitForElementByCss('#to-static-page')
// there shouldn't be any additional prefetches
expect(
requests.filter((request) => request === '/static-page').length
).toBe(1)
// navigate to the static page again
await browser.elementByCss('#to-static-page').click()
await browser.waitForElementByCss('#static-page')
// there still should only be the initial request to the static page
expect(
requests.filter((request) => request === '/static-page').length
).toBe(1)
})
it('should calculate `_rsc` query based on `Next-Url`', async () => {
const browser = await next.browser('/404', browserConfigWithFixedTime)
let staticPageRequests: string[] = []
browser.on('request', (req) => {
const url = new URL(req.url())
if (url.toString().includes(`/static-page?${NEXT_RSC_UNION_QUERY}=`)) {
staticPageRequests.push(`${url.pathname}${url.search}`)
}
})
await browser.eval('location.href = "/"')
await browser.eval(
`window.nd.router.prefetch("/static-page", {kind: "auto"})`
)
await check(() => {
return staticPageRequests.length === 1
? 'success'
: JSON.stringify(staticPageRequests)
}, 'success')
// Unable to clear router cache so mpa navigation
await browser.eval('location.href = "/dashboard"')
await browser.eval(
`window.nd.router.prefetch("/static-page", {kind: "auto"})`
)
await check(() => {
return staticPageRequests.length === 2
? 'success'
: JSON.stringify(staticPageRequests)
}, 'success')
expect(staticPageRequests[0]).toMatch('/static-page?_rsc=')
expect(staticPageRequests[1]).toMatch('/static-page?_rsc=')
// `_rsc` does not match because it depends on the `Next-Url`
expect(staticPageRequests[0]).not.toBe(staticPageRequests[1])
})
it('should not prefetch for a bot user agent', 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 = "/?useragent=${encodeURIComponent(
'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/W.X.Y.Z Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
)}"`
)
await browser.elementByCss('#to-static-page').moveTo()
// check five times to ensure prefetch didn't occur
for (let i = 0; i < 5; i++) {
await waitFor(500)
expect(
requests.filter(
(request) =>
request === '/static-page' || request.includes(NEXT_RSC_UNION_QUERY)
).length
).toBe(0)
}
})
it('should navigate when prefetch is false', async () => {
const browser = await next.browser('/prefetch-false/initial')
await browser
.elementByCss('#to-prefetch-false-result')
.click()
.waitForElementByCss('#prefetch-false-page-result')
expect(
await browser.elementByCss('#prefetch-false-page-result').text()
).toBe('Result page')
})
it('should not need to prefetch the layout if the prefetch is initiated at the same segment', async () => {
const stateTree = encodeURIComponent(
JSON.stringify([
'',
{
children: [
'prefetch-auto',
{
children: [
['slug', 'justputit', 'd'],
{ children: ['__PAGE__', {}] },
],
},
],
},
null,
null,
true,
])
)
const response = await next.fetch(`/prefetch-auto/justputit?_rsc=dcqtr`, {
headers: {
RSC: '1',
'Next-Router-Prefetch': '1',
'Next-Router-State-Tree': stateTree,
'Next-Url': '/prefetch-auto/justputit',
},
})
const prefetchResponse = await response.text()
expect(prefetchResponse).not.toContain('Page Data!')
expect(prefetchResponse).not.toContain('Layout Data!')
expect(prefetchResponse).not.toContain('Loading Prefetch Auto')
})
it('should only prefetch the loading state and not the component tree when prefetching at the same segment', async () => {
const stateTree = encodeURIComponent(
JSON.stringify([
'',
{
children: [
'prefetch-auto',
{
children: [
['slug', 'vercel', 'd'],
{ children: ['__PAGE__', {}] },
],
},
],
},
null,
null,
true,
])
)
const headers = {
RSC: '1',
'Next-Router-Prefetch': '1',
'Next-Router-State-Tree': stateTree,
'Next-Url': '/prefetch-auto/vercel',
}
const url = new URL('/prefetch-auto/justputit', 'http://localhost')
const cacheBustingParam = computeCacheBustingSearchParam(
headers['Next-Router-Prefetch'],
undefined,
headers['Next-Router-State-Tree'],
headers['Next-Url']
)
if (cacheBustingParam) {
url.searchParams.set('_rsc', cacheBustingParam)
}
const response = await next.fetch(url.toString(), { headers })
const prefetchResponse = await response.text()
expect(prefetchResponse).not.toContain('Page Data!')
expect(prefetchResponse).toContain('Loading Prefetch Auto')
})
it('should not re-render error component when triggering a prefetch action', async () => {
const browser = await next.browser('/with-error')
const initialRandom = await browser
.elementByCss('button')
.click()
.waitForElementByCss('#random-number')
.text()
await browser.eval('window.next.router.prefetch("/")')
// confirm the error component was not re-rendered
expect(await browser.elementById('random-number').text()).toBe(
initialRandom
)
})
it('should immediately render the loading state for a dynamic segment when fetched from higher up in the tree', async () => {
const browser = await next.browser('/')
const loadingText = await browser
.elementById('to-dynamic-page')
.click()
.waitForElementByCss('#loading-text')
.text()
expect(loadingText).toBe('Loading Prefetch Auto')
await browser.waitForElementByCss('#prefetch-auto-page-data')
})
it('should not unintentionally modify the requested prefetch by escaping the uri encoded query params', async () => {
const rscRequests = []
const browser = await next.browser('/uri-encoded-prefetch', {
beforePageLoad(page) {
page.on('request', async (req) => {
const url = new URL(req.url())
if (url.searchParams.has('_rsc')) {
rscRequests.push(url.pathname + url.search)
}
})
},
})
// sanity check: the link should be present
expect(await browser.elementById('prefetch-via-link')).toBeDefined()
await browser.waitForIdleNetwork()
// The space encoding of the prefetch request should be the same as the href, and should not be replaced with a +
await retry(async () => {
expect(
rscRequests.filter((req) => req.includes('/?param=with%20space'))
).toHaveLength(1)
})
// Click the link
await browser.elementById('prefetch-via-link').click()
// Assert that we're on the homepage
expect(await browser.hasElementByCssSelector('#to-dashboard')).toBe(true)
await browser.waitForIdleNetwork()
// No new requests should be made since it is correctly prefetched
await retry(async () => {
expect(
rscRequests.filter((req) => req.includes('/?param=with%20space'))
).toHaveLength(1)
})
})
describe('prefetch cache seeding', () => {
it('should not re-fetch the initial static page if the same page is prefetched with prefetch={true}', async () => {
const rscRequests = []
const browser = await next.browser('/static-page', {
beforePageLoad(page) {
page.on('request', async (req) => {
const url = new URL(req.url())
if (url.pathname === '/static-page' || url.pathname === '/') {
const headers = await req.allHeaders()
if (headers['rsc']) {
rscRequests.push(url.pathname)
}
}
})
},
})
expect(
await browser.hasElementByCssSelector('[href="/static-page"]')
).toBe(true)
// sanity check: we should see a prefetch request to the root page
await retry(async () => {
expect(rscRequests.filter((req) => req === '/').length).toBe(1)
})
// We shouldn't see any requests to the static page since the prefetch cache was seeded as part of the SSR render
await retry(async () => {
expect(rscRequests.filter((req) => req === '/static-page').length).toBe(
0
)
})
// navigate to index
await browser.elementByCss('[href="/"]').click()
// we should be on the index page
await browser.waitForElementByCss('#to-dashboard')
// navigate to the static page
await browser.elementByCss('[href="/static-page"]').click()
// we should be on the static page
await browser.waitForElementByCss('#static-page')
await browser.waitForIdleNetwork()
// We still shouldn't see any requests since it respects the static staletime (default 5m)
await retry(async () => {
expect(rscRequests.filter((req) => req === '/static-page').length).toBe(
0
)
})
})
it('should not re-fetch the initial dynamic page if the same page is prefetched with prefetch={true}', async () => {
const rscRequests = []
const browser = await next.browser('/dynamic-page', {
beforePageLoad(page) {
page.on('request', async (req) => {
const url = new URL(req.url())
if (url.pathname === '/dynamic-page' || url.pathname === '/') {
const headers = await req.allHeaders()
if (headers['rsc']) {
rscRequests.push(url.pathname)
}
}
})
},
})
expect(
await browser.hasElementByCssSelector('[href="/dynamic-page"]')
).toBe(true)
// sanity check: we should see a prefetch request to the root page
await retry(async () => {
expect(rscRequests.filter((req) => req === '/').length).toBe(1)
})
// We shouldn't see any requests to the dynamic page since the prefetch cache was seeded as part of the SSR render
await retry(async () => {
expect(
rscRequests.filter((req) => req === '/dynamic-page').length
).toBe(0)
})
// navigate to index
await browser.elementByCss('[href="/"]').click()
// we should be on the index page
await browser.waitForElementByCss('#to-dashboard')
// navigate to the dynamic page
await browser.elementByCss('[href="/dynamic-page"]').click()
// we should be on the dynamic page
await browser.waitForElementByCss('#dynamic-page')
await browser.waitForIdleNetwork()
// We should see a request for the dynamic page since it respects the dynamic staletime (default 0)
await retry(async () => {
expect(
rscRequests.filter((req) => req === '/dynamic-page').length
).toBe(1)
})
})
})
// These tests are skipped when deployed as they rely on runtime logs
if (!isNextDeploy) {
describe('dynamic rendering', () => {
describe.each(['/force-dynamic', '/revalidate-0'])('%s', (basePath) => {
it('should not re-render layout when navigating between sub-pages', async () => {
const logStartIndex = next.cliOutput.length
const browser = await next.browser(`${basePath}/test-page`)
let initialRandomNumber = await browser
.elementById('random-number')
.text()
await browser
.elementByCss(`[href="${basePath}/test-page/sub-page"]`)
.click()
await check(() => browser.hasElementByCssSelector('#sub-page'), true)
const newRandomNumber = await browser
.elementById('random-number')
.text()
expect(initialRandomNumber).toBe(newRandomNumber)
await check(() => {
const logOccurrences =
next.cliOutput.slice(logStartIndex).split('re-fetching in layout')
.length - 1
return logOccurrences
}, 1)
})
it('should update search params following a link click', async () => {
const browser = await next.browser(`${basePath}/search-params`)
await check(
() => browser.elementById('search-params-data').text(),
/{}/
)
await browser.elementByCss('[href="?foo=true"]').click()
await check(
() => browser.elementById('search-params-data').text(),
/{"foo":"true"}/
)
await browser
.elementByCss(`[href="${basePath}/search-params"]`)
.click()
await check(
() => browser.elementById('search-params-data').text(),
/{}/
)
await browser.elementByCss('[href="?foo=true"]').click()
await check(
() => browser.elementById('search-params-data').text(),
/{"foo":"true"}/
)
})
})
})
}
describe('invalid URLs', () => {
it('should not throw when an invalid URL is passed to Link', async () => {
const browser = await next.browser('/invalid-url/from-link')
await check(() => browser.hasElementByCssSelector('h1'), true)
expect(await browser.elementByCss('h1').text()).toEqual('Hello, world!')
})
it('should throw when an invalid URL is passed to router.prefetch', async () => {
const browser = await next.browser('/invalid-url/from-router-prefetch')
await check(() => browser.hasElementByCssSelector('h1'), true)
expect(await browser.elementByCss('h1').text()).toEqual(
'A prefetch threw an error'
)
})
})
describe('fetch priority', () => {
it('should prefetch links in viewport with low priority', async () => {
const requests: { priority: string; url: string }[] = []
const browser = await next.browser('/', {
beforePageLoad(page) {
page.on('request', async (req) => {
const url = new URL(req.url())
const headers = await req.allHeaders()
if (headers['rsc']) {
requests.push({
priority: headers['next-test-fetch-priority'],
url: url.pathname,
})
}
})
},
})
await browser.waitForIdleNetwork()
await retry(async () => {
expect(requests.length).toBeGreaterThan(0)
expect(requests.every((req) => req.priority === 'low')).toBe(true)
})
})
it('should prefetch with high priority when navigating to a page without a prefetch entry', async () => {
const requests: { priority: string; url: string }[] = []
const browser = await next.browser('/prefetch-false/initial', {
beforePageLoad(page) {
page.on('request', async (req) => {
const url = new URL(req.url())
const headers = await req.allHeaders()
if (headers['rsc']) {
requests.push({
priority: headers['next-test-fetch-priority'],
url: url.pathname,
})
}
})
},
})
await browser.waitForIdleNetwork()
expect(requests.length).toBe(0)
await browser.elementByCss('#to-prefetch-false-result').click()
await retry(async () => {
expect(requests.length).toBe(1)
expect(requests[0].priority).toBe('high')
})
})
it('should have an auto priority for all other fetch operations', async () => {
const requests: { priority: string; url: string }[] = []
const browser = await next.browser('/', {
beforePageLoad(page) {
page.on('request', async (req) => {
const url = new URL(req.url())
const headers = await req.allHeaders()
if (headers['rsc']) {
requests.push({
priority: headers['next-test-fetch-priority'],
url: url.pathname,
})
}
})
},
})
await browser.elementByCss('#to-dashboard').click()
await browser.waitForIdleNetwork()
await retry(async () => {
const dashboardRequests = requests.filter(
(req) => req.url === '/dashboard'
)
expect(dashboardRequests.length).toBe(2)
expect(dashboardRequests[0].priority).toBe('low') // the first request is the prefetch
expect(dashboardRequests[1].priority).toBe('auto') // the second request is the lazy fetch to fill in missing data
})
})
it('should respect multiple prefetch types to the same URL', async () => {
let interceptRequests = false
const browser = await next.browser('/prefetch-race', {
beforePageLoad(page) {
page.route('**/force-dynamic/**', async (route) => {
if (!interceptRequests) {
return route.continue()
}
const request = route.request()
const headers = await request.allHeaders()
if (headers['rsc'] === '1') {
// intentionally stall the request,
// as after the initial page load, there shouldn't be any additional fetches
// since the data should already be available.
} else {
await route.continue()
}
})
},
})
await browser.waitForIdleNetwork()
interceptRequests = true
await browser.elementByCss('[href="/force-dynamic/test-page"]').click()
await browser.waitForElementByCss('#test-page')
})
})
})
|