Commit 06876f1b authored by Natalie Marleny's avatar Natalie Marleny Committed by Joe Haddad

fix: Warn when user specifies prefetch option as `true` (#8233)

* Warn when user specifies prefetch option as `true`

* Update index.test.js
parent 6b516f6c
# `prefetch={true}` is deprecated
#### Why This Error Occurred
See https://nextjs.org/blog/next-9#prefetching-in-viewport-links
<Link> will automatically prefetch pages in the background as they appear in the view. If certain pages are rarely visited you can manually set prefetch to false, here's how:
Next.js 9 will automatically prefetch <Link> components as they appear in-viewport.
This feature improves the responsiveness of your application by making navigations to new pages quicker.
Next.js uses an Intersection Observer to prefetch the assets necessary in the background.
These requests have low-priority and yield to fetch() or XHR requests. Next.js will avoid automatically prefetching if the user has data-saver enabled.
#### Possible Ways to Fix It
The prefetch attribute is no longer needed, when set to true, example: `prefetch={true}`, remove the property.
Prefetching can be disabled with `prefetch={false}`.
......@@ -106,8 +106,17 @@ const listenToIntersections = (el: any, cb: any) => {
class Link extends Component<LinkProps> {
static propTypes?: any
static defaultProps: Partial<LinkProps> = {
prefetch: true,
p: boolean
constructor(props: LinkProps) {
super(props)
if (process.env.NODE_ENV !== 'production') {
if (props.prefetch) {
console.warn(
'Next.js auto-prefetches automatically based on viewport. The prefetch attribute is no longer needed. More: https://err.sh/zeit/next.js/prefetch-true-deprecated'
)
}
}
this.p = props.prefetch !== false
}
cleanUpListeners = () => {}
......@@ -117,7 +126,7 @@ class Link extends Component<LinkProps> {
}
handleRef(ref: Element) {
if (this.props.prefetch && IntersectionObserver && ref && ref.tagName) {
if (this.p && IntersectionObserver && ref && ref.tagName) {
this.cleanUpListeners()
this.cleanUpListeners = listenToIntersections(ref, () => {
this.prefetch()
......@@ -181,7 +190,7 @@ class Link extends Component<LinkProps> {
}
prefetch() {
if (!this.props.prefetch || typeof window === 'undefined') return
if (!this.p || typeof window === 'undefined') return
// Prefetch the JSON page if asked (only in the client)
const { pathname } = window.location
......
import Link from 'next/link'
export default function IndexPage () {
return (
<div>
<Link href='/about' prefetch>
<a>To About Page</a>
</Link>
</div>
)
}
import Link from 'next/link'
export default function NoPrefetchPage () {
return (
<div>
<Link href='/about'>
<a>No prefetch</a>
</Link>
</div>
)
}
import Link from 'next/link'
export default function PrefetchFalsePage () {
return (
<div>
<Link href='/about' prefetch={false}>
<a>Prefetch set to false</a>
</Link>
</div>
)
}
/* eslint-env jest */
import webdriver from 'next-webdriver'
export default context => {
describe('Development Logs', () => {
it('should warn when prefetch is true', async () => {
let browser
try {
browser = await webdriver(context.appPort, '/development-logs')
const browserLogs = await browser.log('browser')
let foundLog = false
browserLogs.forEach(log => {
if (log.message.includes('Next.js auto-prefetches automatically')) {
foundLog = true
}
})
expect(foundLog).toBe(true)
} finally {
if (browser) {
await browser.close()
}
}
})
it('should not warn when prefetch is false', async () => {
let browser
try {
browser = await webdriver(
context.appPort,
'/development-logs/link-with-prefetch-false'
)
const browserLogs = await browser.log('browser')
let found = false
browserLogs.forEach(log => {
if (log.message.includes('Next.js auto-prefetches automatically')) {
found = true
}
})
expect(found).toBe(false)
} finally {
if (browser) {
await browser.close()
}
}
})
it('should not warn when prefetch is not specified', async () => {
let browser
try {
browser = await webdriver(
context.appPort,
'/development-logs/link-with-no-prefetch'
)
const browserLogs = await browser.log('browser')
let found = false
browserLogs.forEach(log => {
if (log.message.includes('Next.js auto-prefetches automatically')) {
found = true
}
})
expect(found).toBe(false)
} finally {
if (browser) {
await browser.close()
}
}
})
})
}
import Link from 'next/link'
export default function IndexPage () {
return (
<div>
<Link href='/about' prefetch>
<a>To About Page</a>
</Link>
</div>
)
}
......@@ -475,7 +475,6 @@ describe('Production Usage', () => {
foundLog = true
}
})
expect(foundLog).toBe(true)
// When we go to the 404 page, it'll do a hard reload.
......@@ -555,6 +554,26 @@ describe('Production Usage', () => {
expect(text).toBe('Not AMP')
})
it('should warn when prefetch is true', async () => {
if (global.browserName !== 'chrome') return
let browser
try {
browser = await webdriver(appPort, '/development-logs')
const browserLogs = await browser.log('browser')
let found = false
browserLogs.forEach(log => {
if (log.message.includes('Next.js auto-prefetches automatically')) {
found = true
}
})
expect(found).toBe(false)
} finally {
if (browser) {
await browser.close()
}
}
})
dynamicImportTests(context, (p, q) => renderViaHTTP(context.appPort, p, q))
processEnv(context)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment