Skip to content

Commit 588bb45

Browse files
authored
Move warnOnce imports into statements guarded by process.env.NODE_ENV === 'development' (vercel#94781)
The context is this message from @icyJoseph: ``` I wonder if you've also look a little at DCE? This module, https://nextjs.org/_next/static/immutable/chunks/2uxebeysomvt2.js ships every time... ``` https://nextjs.org/_next/static/immutable/chunks/2uxebeysomvt2.js corresponds to this `warnOnce` helper: https://github.com/vercel/next.js/blob/643e34a59183f90ae9d2db9a64673781316ce786/packages/next/src/shared/lib/utils/warn-once.ts#L1 That doesn't do anything because Turbopack appears to be attempting to remove unused imports before doing dead-code removal. If the imports are moved though into the `(process.env.NODE_ENV !== 'production')` guard, this module won't get shipped into production. The patch to do this is pretty small so this PR does that, I'm going to look into whether its possible to reorder it so unused imports are looked at after dead code is removed.
1 parent 8385211 commit 588bb45

6 files changed

Lines changed: 12 additions & 7 deletions

File tree

packages/next/src/client/app-dir/link.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { AppRouterContext } from '../../shared/lib/app-router-context.shared-run
77
import { useMergedRef } from '../use-merged-ref'
88
import { isAbsoluteUrl } from '../../shared/lib/utils'
99
import { addBasePath } from '../add-base-path'
10-
import { warnOnce } from '../../shared/lib/utils/warn-once'
1110
import { ScrollBehavior } from '../components/router-reducer/router-reducer-types'
1211
import type { PENDING_LINK_STATUS } from '../components/links'
1312
import {
@@ -513,6 +512,8 @@ export default function LinkComponent(
513512
const formattedHref = formatStringOrUrl(resolvedHref)
514513

515514
if (process.env.NODE_ENV !== 'production') {
515+
const { warnOnce } =
516+
require('../../shared/lib/utils/warn-once') as typeof import('../../shared/lib/utils/warn-once')
516517
if (props.locale) {
517518
warnOnce(
518519
'The `locale` prop is not supported in `next/link` while using the `app` router. Read more about app router internalization: https://nextjs.org/docs/app/building-your-application/routing/internationalization'

packages/next/src/client/components/http-access-fallback/error-boundary.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
getAccessFallbackErrorTypeByStatus,
2020
isHTTPAccessFallbackError,
2121
} from './http-access-fallback'
22-
import { warnOnce } from '../../../shared/lib/utils/warn-once'
2322
import { MissingSlotContext } from '../../../shared/lib/app-router-context.shared-runtime'
2423

2524
interface HTTPAccessFallbackBoundaryProps {
@@ -62,6 +61,8 @@ class HTTPAccessFallbackErrorBoundary extends React.Component<
6261
// A missing children slot is the typical not-found case, so no need to warn
6362
!this.props.missingSlots.has('children')
6463
) {
64+
const { warnOnce } =
65+
require('../../../shared/lib/utils/warn-once') as typeof import('../../../shared/lib/utils/warn-once')
6566
let warningMessage =
6667
'No default component was found for a parallel route rendered on this page. Falling back to nearest NotFound boundary.\n' +
6768
'Learn more: https://nextjs.org/docs/app/building-your-application/routing/parallel-routes#defaultjs\n\n'

packages/next/src/client/image-component.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import type {
2626
} from '../shared/lib/image-config'
2727
import { imageConfigDefault } from '../shared/lib/image-config'
2828
import { ImageConfigContext } from '../shared/lib/image-config-context.shared-runtime'
29-
import { warnOnce } from '../shared/lib/utils/warn-once'
3029
import { RouterContext } from '../shared/lib/router-context.shared-runtime'
3130

3231
// This is replaced by webpack alias
@@ -116,6 +115,8 @@ function handleLoading(
116115
onLoadingCompleteRef.current(img)
117116
}
118117
if (process.env.NODE_ENV !== 'production') {
118+
const { warnOnce } =
119+
require('../shared/lib/utils/warn-once') as typeof import('../shared/lib/utils/warn-once')
119120
const origSrc = new URL(src, 'http://n').searchParams.get('url') || src
120121
if (img.getAttribute('data-nimg') === 'fill') {
121122
if (!unoptimized && (!sizesInput || sizesInput === '100vw')) {

packages/next/src/shared/lib/get-img-props.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { warnOnce } from './utils/warn-once'
21
import { getAssetToken, getDeploymentId } from './deployment-id'
32
import { getImageBlurSvg } from './image-blur-svg'
43
import { imageConfigDefault } from './image-config'
@@ -462,6 +461,8 @@ export function getImgProps(
462461
const qualityInt = getInt(quality)
463462

464463
if (process.env.NODE_ENV !== 'production') {
464+
const { warnOnce } =
465+
require('./utils/warn-once') as typeof import('./utils/warn-once')
465466
if (config.output === 'export' && isDefaultLoader && !unoptimized) {
466467
throw new Error(
467468
`Image Optimization using the default loader is not compatible with \`{ output: 'export' }\`.

packages/next/src/shared/lib/head.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import React, { useContext, type JSX } from 'react'
44
import Effect from './side-effect'
55
import { HeadManagerContext } from './head-manager-context.shared-runtime'
6-
import { warnOnce } from './utils/warn-once'
76

87
export function defaultHead(): JSX.Element[] {
98
const head = [
@@ -129,6 +128,8 @@ function reduceComponents(
129128
.map((c: React.ReactElement<any>, i: number) => {
130129
const key = c.key || i
131130
if (process.env.NODE_ENV === 'development') {
131+
const { warnOnce } =
132+
require('./utils/warn-once') as typeof import('./utils/warn-once')
132133
// omit JSON-LD structured data snippets from the warning
133134
if (c.type === 'script' && c.props['type'] !== 'application/ld+json') {
134135
const srcMessage = c.props['src']

packages/next/src/shared/lib/router/utils/disable-smooth-scroll.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { warnOnce } from '../../utils/warn-once'
2-
31
/**
42
* Run function with `scroll-behavior: auto` applied to `<html/>`.
53
* This css change will be reverted after the function finishes.
@@ -24,6 +22,8 @@ export function disableSmoothScrollDuringRouteTransition(
2422
process.env.NODE_ENV === 'development' &&
2523
getComputedStyle(htmlElement).scrollBehavior === 'smooth'
2624
) {
25+
const { warnOnce } =
26+
require('../../utils/warn-once') as typeof import('../../utils/warn-once')
2727
warnOnce(
2828
'Detected `scroll-behavior: smooth` on the `<html>` element. To disable smooth scrolling during route transitions, ' +
2929
'add `data-scroll-behavior="smooth"` to your <html> element. ' +

0 commit comments

Comments
 (0)