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

Simplify <Link> Prefetch Deduping #9987

Merged
merged 4 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/next/client/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ let observer: IntersectionObserver
const listeners = new Map()
const IntersectionObserver =
typeof window !== 'undefined' ? (window as any).IntersectionObserver : null
const prefetched: { [href: string]: boolean } = {}

function getObserver() {
// Return shared instance of IntersectionObserver if already created
Expand Down Expand Up @@ -133,10 +134,7 @@ class Link extends Component<LinkProps> {
}

handleRef(ref: Element) {
const isPrefetched = (Router.router as any).pageLoader.prefetched[
this.getHref()
]

const isPrefetched = prefetched[this.getHref()]
if (this.p && IntersectionObserver && ref && ref.tagName) {
this.cleanUpListeners()

Expand Down Expand Up @@ -206,7 +204,9 @@ class Link extends Component<LinkProps> {
prefetch() {
if (!this.p || typeof window === 'undefined') return
// Prefetch the JSON page if asked (only in the client)
Router.prefetch(this.getHref())
const href = this.getHref()
Router.prefetch(href)
prefetched[href] = true
}

render() {
Expand Down
2 changes: 0 additions & 2 deletions packages/next/client/page-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export default class PageLoader {
this.assetPrefix = assetPrefix

this.pageCache = {}
this.prefetched = {}
this.pageRegisterEvents = mitt()
this.loadingRoutes = {}
if (process.env.__NEXT_GRANULAR_CHUNKS) {
Expand Down Expand Up @@ -221,7 +220,6 @@ export default class PageLoader {
url = route
} else {
route = normalizeRoute(route)
this.prefetched[route] = true

let scriptRoute = `${route === '/' ? '/index' : route}.js`
if (process.env.__NEXT_MODERN_BUILD && hasNoModule) {
Expand Down
7 changes: 3 additions & 4 deletions packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,15 +608,14 @@ export default class Router implements BaseRouter {
}
return
}
// @ts-ignore pathname is always defined
const route = toRoute(pathname)

// Prefetch is not supported in development mode because it would trigger on-demand-entries
if (process.env.NODE_ENV !== 'production') {
// mark it as prefetched for debugging in dev
this.pageLoader.prefetched[route] = true
return
}

// @ts-ignore pathname is always defined
const route = toRoute(pathname)
this.pageLoader.prefetch(route).then(resolve, reject)
})
}
Expand Down
2 changes: 2 additions & 0 deletions test/integration/preload-viewport/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ describe('Prefetching Links in viewport', () => {
})

it('should not add an another observer for a prefetched page', async () => {
// info: both `/` and `/de-duped` ref the `/first` page, which we don't
// want to be re-fetched.
Timer marked this conversation as resolved.
Show resolved Hide resolved
const browser = await webdriver(appPort, '/')
await waitFor(2 * 1000)
await browser.eval(`(function() {
Expand Down