From ea58d94e9dd22ce7850d490d0254e716eb8207f5 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Thu, 8 Dec 2022 22:35:51 +0100 Subject: [PATCH] Move prefetch bailout to start of the prefetch function for `pages` (#43731) Found that prefetching bails later than it has to in development for `pages`. This moves it to be the first thing that is checked in prefetch() ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) --- packages/next/shared/lib/router/router.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/next/shared/lib/router/router.ts b/packages/next/shared/lib/router/router.ts index 6ffb6e0b619522c..c9c06916706fce6 100644 --- a/packages/next/shared/lib/router/router.ts +++ b/packages/next/shared/lib/router/router.ts @@ -2267,6 +2267,11 @@ export default class Router implements BaseRouter { asPath: string = url, options: PrefetchOptions = {} ): Promise { + // Prefetch is not supported in development mode because it would trigger on-demand-entries + if (process.env.NODE_ENV !== 'production') { + return + } + if (typeof window !== 'undefined' && isBot(window.navigator.userAgent)) { // No prefetches for bots that render the link since they are typically navigating // links via the equivalent of a hard navigation and hence never utilize these @@ -2361,11 +2366,6 @@ export default class Router implements BaseRouter { } } - // Prefetch is not supported in development mode because it would trigger on-demand-entries - if (process.env.NODE_ENV !== 'production') { - return - } - const data = process.env.__NEXT_MIDDLEWARE_PREFETCH === 'strict' ? ({} as any)