Skip to content
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

misc: change strategy for link prefetch test #45234

Merged
merged 4 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/next/src/client/use-intersection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ export function useIntersection<T extends Element>({
const isDisabled: boolean = disabled || !hasIntersectionObserver

const [visible, setVisible] = useState(false)
// const [element, setElement] = useState<T | null>(null)
const elementRef = useRef<T | null>(null)
const setElement = useCallback((element: T | null) => {
elementRef.current = element
Expand Down
68 changes: 48 additions & 20 deletions test/integration/preload-viewport/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-env jest */

import {
check,
findPort,
killApp,
nextBuild,
Expand Down Expand Up @@ -94,17 +95,21 @@ describe('Prefetching Links in viewport', () => {
let browser
try {
browser = await webdriver(appPort, '/')
const links = await browser.elementsByCss('link[rel=prefetch]')
let found = false

for (const link of links) {
const href = await link.getAttribute('href')
if (href.includes('first')) {
found = true
break
await check(async () => {
const links = await browser.elementsByCss('link[rel=prefetch]')
let found = false

for (const link of links) {
const href = await link.getAttribute('href')
if (href.includes('first')) {
found = true
break
}
}
}
expect(found).toBe(true)
expect(found).toBe(true)
return 'success'
}, 'success')
} finally {
if (browser) await browser.close()
}
Expand Down Expand Up @@ -146,17 +151,21 @@ describe('Prefetching Links in viewport', () => {
let browser
try {
browser = await webdriver(appPort, '/rewrite-prefetch')
const links = await browser.elementsByCss('link[rel=prefetch]')
let found = false

for (const link of links) {
const href = await link.getAttribute('href')
if (href.includes('%5Bslug%5D')) {
found = true
break
await check(async () => {
const links = await browser.elementsByCss('link[rel=prefetch]')
let found = false

for (const link of links) {
const href = await link.getAttribute('href')
if (href.includes('%5Bslug%5D')) {
found = true
break
}
}
}
expect(found).toBe(true)
expect(found).toBe(true)
return 'success'
}, 'success')

const hrefs = await browser.eval(`Object.keys(window.next.router.sdc)`)
expect(hrefs.map((href) => new URL(href).pathname)).toEqual([
Expand Down Expand Up @@ -433,10 +442,26 @@ describe('Prefetching Links in viewport', () => {
expect(hrefs.some((e) => e.includes(`%5Bhello%5D-`))).toBe(true)
})

it('should not add an another observer for a prefetched page', async () => {
it('should not re-prefetch for an already prefetched page', async () => {
// info: both `/` and `/de-duped` ref the `/first` page, which we don't
// want to be re-fetched/re-observed.
const browser = await webdriver(appPort, '/')

await check(async () => {
const links = await browser.elementsByCss('link[rel=prefetch]')
let found = false

for (const link of links) {
const href = await link.getAttribute('href')
if (href.includes('first')) {
found = true
break
}
}
expect(found).toBe(true)
return 'success'
}, 'success')

await browser.eval(`(function() {
window.calledPrefetch = false
window.next.router.prefetch = function() {
Expand All @@ -445,7 +470,10 @@ describe('Prefetching Links in viewport', () => {
}
window.next.router.push('/de-duped')
})()`)
await waitFor(2 * 1000)
await check(
() => browser.eval('document.documentElement.innerHTML'),
/to \/first/
)
const calledPrefetch = await browser.eval(`window.calledPrefetch`)
expect(calledPrefetch).toBe(false)
})
Expand Down