Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
7de4c072
Unverified
Commit
7de4c072
authored
Mar 14, 2025
by
Jude Gao
Committed by
GitHub
Mar 14, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(feat) support client-side instrumentation (#76916)
parent
afa3c94b
Hide whitespace changes
Inline
Side-by-side
Showing
26 changed files
with
214 additions
and
0 deletions
+214
-0
next_import_map.rs
crates/next-core/src/next_import_map.rs
+21
-0
create-compiler-aliases.ts
packages/next/src/build/create-compiler-aliases.ts
+13
-0
define-env-plugin.ts
packages/next/src/build/webpack/plugins/define-env-plugin.ts
+3
-0
app-index.tsx
packages/next/src/client/app-index.tsx
+1
-0
app-next-dev.ts
packages/next/src/client/app-next-dev.ts
+1
-0
app-next-turbopack.ts
packages/next/src/client/app-next-turbopack.ts
+1
-0
app-next.ts
packages/next/src/client/app-next.ts
+1
-0
index.tsx
packages/next/src/client/index.tsx
+2
-0
next-turbopack.ts
packages/next/src/client/next-turbopack.ts
+2
-0
next.ts
packages/next/src/client/next.ts
+2
-0
page-bootstrap.ts
packages/next/src/client/page-bootstrap.ts
+1
-0
require-instrumentation-client.ts
packages/next/src/lib/require-instrumentation-client.ts
+10
-0
config-schema.ts
packages/next/src/server/config-schema.ts
+1
-0
config-shared.ts
packages/next/src/server/config-shared.ts
+9
-0
layout.tsx
...e2e/instrumentation-client-hook/app-router/app/layout.tsx
+9
-0
page.tsx
test/e2e/instrumentation-client-hook/app-router/app/page.tsx
+5
-0
instrumentation-client.ts
...entation-client-hook/app-router/instrumentation-client.ts
+1
-0
next.config.js
...e2e/instrumentation-client-hook/app-router/next.config.js
+5
-0
next.config.js
...e/instrumentation-client-hook/app-with-src/next.config.js
+5
-0
layout.tsx
...strumentation-client-hook/app-with-src/src/app/layout.tsx
+9
-0
page.tsx
...instrumentation-client-hook/app-with-src/src/app/page.tsx
+5
-0
instrumentation-client.ts
...on-client-hook/app-with-src/src/instrumentation-client.ts
+1
-0
instrumentation-client-hook.test.ts
...mentation-client-hook/instrumentation-client-hook.test.ts
+95
-0
instrumentation-client.ts
...tation-client-hook/pages-router/instrumentation-client.ts
+1
-0
next.config.js
...e/instrumentation-client-hook/pages-router/next.config.js
+5
-0
index.tsx
.../instrumentation-client-hook/pages-router/pages/index.tsx
+5
-0
No files found.
crates/next-core/src/next_import_map.rs
View file @
7de4c072
...
@@ -244,6 +244,7 @@ pub async fn get_next_client_import_map(
...
@@ -244,6 +244,7 @@ pub async fn get_next_client_import_map(
}
}
insert_turbopack_dev_alias
(
&
mut
import_map
)
.await
?
;
insert_turbopack_dev_alias
(
&
mut
import_map
)
.await
?
;
insert_instrumentation_client_alias
(
&
mut
import_map
,
project_path
)
.await
?
;
Ok
(
import_map
.cell
())
Ok
(
import_map
.cell
())
}
}
...
@@ -1143,6 +1144,26 @@ async fn insert_turbopack_dev_alias(import_map: &mut ImportMap) -> Result<()> {
...
@@ -1143,6 +1144,26 @@ async fn insert_turbopack_dev_alias(import_map: &mut ImportMap) -> Result<()> {
Ok
(())
Ok
(())
}
}
/// Handles instrumentation-client.ts bundling logic
async
fn
insert_instrumentation_client_alias
(
import_map
:
&
mut
ImportMap
,
project_path
:
ResolvedVc
<
FileSystemPath
>
,
)
->
Result
<
()
>
{
insert_alias_to_alternatives
(
import_map
,
"private-next-instrumentation-client"
,
vec!
[
request_to_import_mapping
(
project_path
,
"./src/instrumentation-client"
),
request_to_import_mapping
(
project_path
,
"./src/instrumentation-client.ts"
),
request_to_import_mapping
(
project_path
,
"./instrumentation-client"
),
request_to_import_mapping
(
project_path
,
"./instrumentation-client.ts"
),
ImportMapping
::
Ignore
.resolved_cell
(),
],
);
Ok
(())
}
/// Creates a direct import mapping to the result of resolving a request
/// Creates a direct import mapping to the result of resolving a request
/// in a context.
/// in a context.
fn
request_to_import_mapping
(
fn
request_to_import_mapping
(
...
...
packages/next/src/build/create-compiler-aliases.ts
View file @
7de4c072
...
@@ -139,6 +139,19 @@ export function createWebpackAliases({
...
@@ -139,6 +139,19 @@ export function createWebpackAliases({
...(
pagesDir
?
{
[
PAGES_DIR_ALIAS
]:
pagesDir
}
:
{}),
...(
pagesDir
?
{
[
PAGES_DIR_ALIAS
]:
pagesDir
}
:
{}),
...(
appDir
?
{
[
APP_DIR_ALIAS
]:
appDir
}
:
{}),
...(
appDir
?
{
[
APP_DIR_ALIAS
]:
appDir
}
:
{}),
[
ROOT_DIR_ALIAS
]:
dir
,
[
ROOT_DIR_ALIAS
]:
dir
,
...(
isClient
?
{
'private-next-instrumentation-client'
:
[
path
.
join
(
dir
,
'src'
,
'instrumentation-client'
),
path
.
join
(
dir
,
'instrumentation-client'
),
'private-next-empty-module'
,
],
// disable typechecker, webpack5 allows aliases to be set to false to create a no-op module
'private-next-empty-module'
:
false
as
any
,
}
:
{}),
[
DOT_NEXT_ALIAS
]:
distDir
,
[
DOT_NEXT_ALIAS
]:
distDir
,
...(
isClient
||
isEdgeServer
?
getOptimizedModuleAliases
()
:
{}),
...(
isClient
||
isEdgeServer
?
getOptimizedModuleAliases
()
:
{}),
...(
reactProductionProfiling
?
getReactProfilingInProduction
()
:
{}),
...(
reactProductionProfiling
?
getReactProfilingInProduction
()
:
{}),
...
...
packages/next/src/build/webpack/plugins/define-env-plugin.ts
View file @
7de4c072
...
@@ -240,6 +240,9 @@ export function getDefineEnv({
...
@@ -240,6 +240,9 @@ export function getDefineEnv({
'process.env.__NEXT_TELEMETRY_DISABLED'
:
Boolean
(
'process.env.__NEXT_TELEMETRY_DISABLED'
:
Boolean
(
process
.
env
.
NEXT_TELEMETRY_DISABLED
process
.
env
.
NEXT_TELEMETRY_DISABLED
),
),
'process.env.__NEXT_EXPERIMENTAL_CLIENT_INSTRUMENTATION_HOOK'
:
Boolean
(
config
.
experimental
.
clientInstrumentationHook
),
...(
isNodeOrEdgeCompilation
...(
isNodeOrEdgeCompilation
?
{
?
{
// Fix bad-actors in the npm ecosystem (e.g. `node-formidable`)
// Fix bad-actors in the npm ecosystem (e.g. `node-formidable`)
...
...
packages/next/src/client/app-index.tsx
View file @
7de4c072
...
@@ -225,6 +225,7 @@ function Root({ children }: React.PropsWithChildren<{}>) {
...
@@ -225,6 +225,7 @@ function Root({ children }: React.PropsWithChildren<{}>) {
// eslint-disable-next-line react-hooks/rules-of-hooks
// eslint-disable-next-line react-hooks/rules-of-hooks
React
.
useEffect
(()
=>
{
React
.
useEffect
(()
=>
{
window
.
__NEXT_HYDRATED
=
true
window
.
__NEXT_HYDRATED
=
true
window
.
__NEXT_HYDRATED_AT
=
performance
.
now
()
window
.
__NEXT_HYDRATED_CB
?.()
window
.
__NEXT_HYDRATED_CB
?.()
},
[])
},
[])
}
}
...
...
packages/next/src/client/app-next-dev.ts
View file @
7de4c072
// TODO-APP: hydration warning
// TODO-APP: hydration warning
import
'./app-webpack'
import
'./app-webpack'
import
'../lib/require-instrumentation-client'
import
{
appBootstrap
}
from
'./app-bootstrap'
import
{
appBootstrap
}
from
'./app-bootstrap'
import
{
initializeDevBuildIndicatorForAppRouter
}
from
'./dev/dev-build-indicator/initialize-for-app-router'
import
{
initializeDevBuildIndicatorForAppRouter
}
from
'./dev/dev-build-indicator/initialize-for-app-router'
...
...
packages/next/src/client/app-next-turbopack.ts
View file @
7de4c072
// TODO-APP: hydration warning
// TODO-APP: hydration warning
import
'../lib/require-instrumentation-client'
import
{
appBootstrap
}
from
'./app-bootstrap'
import
{
appBootstrap
}
from
'./app-bootstrap'
window
.
next
.
version
+=
'-turbo'
window
.
next
.
version
+=
'-turbo'
...
...
packages/next/src/client/app-next.ts
View file @
7de4c072
// This import must go first because it needs to patch webpack chunk loading
// This import must go first because it needs to patch webpack chunk loading
// before React patches chunk loading.
// before React patches chunk loading.
import
'./app-webpack'
import
'./app-webpack'
import
'../lib/require-instrumentation-client'
import
{
appBootstrap
}
from
'./app-bootstrap'
import
{
appBootstrap
}
from
'./app-bootstrap'
appBootstrap
(()
=>
{
appBootstrap
(()
=>
{
...
...
packages/next/src/client/index.tsx
View file @
7de4c072
...
@@ -55,6 +55,7 @@ declare global {
...
@@ -55,6 +55,7 @@ declare global {
interface
Window
{
interface
Window
{
/* test fns */
/* test fns */
__NEXT_HYDRATED
?:
boolean
__NEXT_HYDRATED
?:
boolean
__NEXT_HYDRATED_AT
?:
number
__NEXT_HYDRATED_CB
?:
()
=>
void
__NEXT_HYDRATED_CB
?:
()
=>
void
/* prod */
/* prod */
...
@@ -612,6 +613,7 @@ function Root({
...
@@ -612,6 +613,7 @@ function Root({
// eslint-disable-next-line react-hooks/rules-of-hooks
// eslint-disable-next-line react-hooks/rules-of-hooks
React
.
useEffect
(()
=>
{
React
.
useEffect
(()
=>
{
window
.
__NEXT_HYDRATED
=
true
window
.
__NEXT_HYDRATED
=
true
window
.
__NEXT_HYDRATED_AT
=
performance
.
now
()
if
(
window
.
__NEXT_HYDRATED_CB
)
{
if
(
window
.
__NEXT_HYDRATED_CB
)
{
window
.
__NEXT_HYDRATED_CB
()
window
.
__NEXT_HYDRATED_CB
()
...
...
packages/next/src/client/next-turbopack.ts
View file @
7de4c072
// A client-side entry point for Turbopack builds. Includes logic to load chunks,
// A client-side entry point for Turbopack builds. Includes logic to load chunks,
// but does not include development-time features like hot module reloading.
// but does not include development-time features like hot module reloading.
import
'../lib/require-instrumentation-client'
// TODO: Remove use of `any` type.
// TODO: Remove use of `any` type.
import
{
initialize
,
version
,
router
,
emitter
,
hydrate
}
from
'./'
import
{
initialize
,
version
,
router
,
emitter
,
hydrate
}
from
'./'
// TODO: This seems necessary, but is a module in the `dev` directory.
// TODO: This seems necessary, but is a module in the `dev` directory.
...
...
packages/next/src/client/next.ts
View file @
7de4c072
import
'./webpack'
import
'./webpack'
import
'../lib/require-instrumentation-client'
import
{
initialize
,
hydrate
,
version
,
router
,
emitter
}
from
'./'
import
{
initialize
,
hydrate
,
version
,
router
,
emitter
}
from
'./'
declare
global
{
declare
global
{
...
...
packages/next/src/client/page-bootstrap.ts
View file @
7de4c072
import
'../lib/require-instrumentation-client'
import
{
hydrate
,
router
}
from
'./'
import
{
hydrate
,
router
}
from
'./'
import
initOnDemandEntries
from
'./dev/on-demand-entries-client'
import
initOnDemandEntries
from
'./dev/on-demand-entries-client'
import
{
devBuildIndicator
}
from
'./dev/dev-build-indicator/internal/dev-build-indicator'
import
{
devBuildIndicator
}
from
'./dev/dev-build-indicator/internal/dev-build-indicator'
...
...
packages/next/src/lib/require-instrumentation-client.ts
0 → 100644
View file @
7de4c072
/**
* This module imports the client instrumentation hook from the project root.
*
* The `private-next-instrumentation-client` module is automatically aliased to
* the `instrumentation-client.ts` file in the project root by webpack or turbopack.
*/
if
(
process
.
env
.
__NEXT_EXPERIMENTAL_CLIENT_INSTRUMENTATION_HOOK
)
{
require
(
'private-next-instrumentation-client'
)
}
packages/next/src/server/config-schema.ts
View file @
7de4c072
...
@@ -455,6 +455,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
...
@@ -455,6 +455,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
buildTimeThresholdMs
:
z
.
number
().
int
(),
buildTimeThresholdMs
:
z
.
number
().
int
(),
})
})
.
optional
(),
.
optional
(),
clientInstrumentationHook
:
z
.
boolean
().
optional
(),
})
})
.
optional
(),
.
optional
(),
exportPathMap
:
z
exportPathMap
:
z
...
...
packages/next/src/server/config-shared.ts
View file @
7de4c072
...
@@ -601,6 +601,15 @@ export interface ExperimentalConfig {
...
@@ -601,6 +601,15 @@ export interface ExperimentalConfig {
*/
*/
buildTimeThresholdMs
:
number
buildTimeThresholdMs
:
number
}
}
/**
* Enables the client instrumentation hook.
* Loads the instrumentation-client.ts file from the project root
* and executes it on the client side before hydration.
*
* Note: Use with caution as this can negatively impact page loading performance.
*/
clientInstrumentationHook
?:
boolean
}
}
export
type
ExportPathMap
=
{
export
type
ExportPathMap
=
{
...
...
test/e2e/instrumentation-client-hook/app-router/app/layout.tsx
0 → 100644
View file @
7de4c072
import
React
from
'react'
export
default
function
RootLayout
({
children
})
{
return
(
<
html
>
<
body
>
{
children
}
</
body
>
</
html
>
)
}
test/e2e/instrumentation-client-hook/app-router/app/page.tsx
0 → 100644
View file @
7de4c072
import
React
from
'react'
export
default
function
Page
()
{
return
<
h1
>
App
</
h1
>
}
test/e2e/instrumentation-client-hook/app-router/instrumentation-client.ts
0 → 100644
View file @
7de4c072
;(
window
as
any
).
__INSTRUMENTATION_CLIENT_EXECUTED_AT
=
performance
.
now
()
test/e2e/instrumentation-client-hook/app-router/next.config.js
0 → 100644
View file @
7de4c072
module
.
exports
=
{
experimental
:
{
clientInstrumentationHook
:
true
,
},
}
test/e2e/instrumentation-client-hook/app-with-src/next.config.js
0 → 100644
View file @
7de4c072
module
.
exports
=
{
experimental
:
{
clientInstrumentationHook
:
true
,
},
}
test/e2e/instrumentation-client-hook/app-with-src/src/app/layout.tsx
0 → 100644
View file @
7de4c072
import
React
from
'react'
export
default
function
RootLayout
({
children
})
{
return
(
<
html
>
<
body
>
{
children
}
</
body
>
</
html
>
)
}
test/e2e/instrumentation-client-hook/app-with-src/src/app/page.tsx
0 → 100644
View file @
7de4c072
import
React
from
'react'
export
default
function
Page
()
{
return
<
h1
>
App with src folder
</
h1
>
}
test/e2e/instrumentation-client-hook/app-with-src/src/instrumentation-client.ts
0 → 100644
View file @
7de4c072
;(
window
as
any
).
__INSTRUMENTATION_CLIENT_EXECUTED_AT
=
performance
.
now
()
test/e2e/instrumentation-client-hook/instrumentation-client-hook.test.ts
0 → 100644
View file @
7de4c072
import
{
nextTestSetup
}
from
'e2e-utils'
import
{
retry
}
from
'next-test-utils'
import
path
from
'path'
describe
(
'Instrumentation Client Hook'
,
()
=>
{
const
testCases
=
[
{
name
:
'With src folder'
,
appDir
:
'app-with-src'
,
},
{
name
:
'App Router'
,
appDir
:
'app-router'
,
},
{
name
:
'Pages Router'
,
appDir
:
'pages-router'
,
},
]
testCases
.
forEach
(({
name
,
appDir
})
=>
{
describe
(
name
,
()
=>
{
const
{
next
}
=
nextTestSetup
({
files
:
path
.
join
(
__dirname
,
appDir
),
})
it
(
`should execute instrumentation-client from
${
name
.
toLowerCase
()}
before hydration`
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/'
)
const
instrumentationTime
=
await
browser
.
eval
(
`window.__INSTRUMENTATION_CLIENT_EXECUTED_AT`
)
const
hydrationTime
=
await
browser
.
eval
(
`window.__NEXT_HYDRATED_AT`
)
expect
(
instrumentationTime
).
toBeDefined
()
expect
(
hydrationTime
).
toBeDefined
()
expect
(
instrumentationTime
).
toBeLessThan
(
hydrationTime
)
})
})
})
describe
(
'HMR in development mode'
,
()
=>
{
const
{
next
,
isNextDev
}
=
nextTestSetup
({
files
:
path
.
join
(
__dirname
,
'app-router'
),
})
if
(
isNextDev
)
{
it
(
'should reload instrumentation-client when modified'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/'
)
const
initialTime
=
await
browser
.
eval
(
`window.__INSTRUMENTATION_CLIENT_EXECUTED_AT`
)
expect
(
initialTime
).
toBeDefined
()
// Modify the instrumentation-client.ts file
const
instrumentationPath
=
'instrumentation-client.ts'
const
originalContent
=
await
next
.
readFile
(
instrumentationPath
)
await
next
.
patchFile
(
instrumentationPath
,
`
window.__INSTRUMENTATION_CLIENT_EXECUTED_AT = Date.now();
window.__INSTRUMENTATION_CLIENT_UPDATED = true;
`
)
await
retry
(
async
()
=>
{
// Check if the updated instrumentation client was executed
const
updatedFlag
=
await
browser
.
eval
(
`window.__INSTRUMENTATION_CLIENT_UPDATED`
)
expect
(
updatedFlag
).
toBe
(
true
)
// Verify new execution time
const
newTime
=
await
browser
.
eval
(
`window.__INSTRUMENTATION_CLIENT_EXECUTED_AT`
)
expect
(
newTime
).
toBeDefined
()
expect
(
newTime
).
toBeGreaterThan
(
initialTime
)
})
// Restore the original file
await
next
.
patchFile
(
instrumentationPath
,
originalContent
)
})
}
else
{
// Add a dummy test when not in dev mode
it
(
'skips tests in non-dev mode'
,
()
=>
{
console
.
log
(
'Skipping instrumentation-client-hook tests in non-dev mode'
)
})
}
})
})
test/e2e/instrumentation-client-hook/pages-router/instrumentation-client.ts
0 → 100644
View file @
7de4c072
;(
window
as
any
).
__INSTRUMENTATION_CLIENT_EXECUTED_AT
=
performance
.
now
()
test/e2e/instrumentation-client-hook/pages-router/next.config.js
0 → 100644
View file @
7de4c072
module
.
exports
=
{
experimental
:
{
clientInstrumentationHook
:
true
,
},
}
test/e2e/instrumentation-client-hook/pages-router/pages/index.tsx
0 → 100644
View file @
7de4c072
import
React
from
'react'
export
default
function
Page
()
{
return
<
h1
>
Pages Router
</
h1
>
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment