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
eb23134e
Unverified
Commit
eb23134e
authored
Jul 22, 2024
by
Sebastian Silbermann
Committed by
GitHub
Jul 22, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Wait for pending Webpack Hot Updates before evaluating JS from RSC responses (#67673)
parent
cad3a510
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
150 additions
and
5 deletions
+150
-5
hot-reloader-client.tsx
.../components/react-dev-overlay/app/hot-reloader-client.tsx
+31
-4
fetch-server-response.ts
...client/components/router-reducer/fetch-server-response.ts
+9
-0
page.tsx
...undler-runtime-changes/new-runtime-functionality/page.tsx
+8
-0
page.tsx
.../development/app-hmr/app/bundler-runtime-changes/page.tsx
+9
-0
favicon.ico
test/development/app-hmr/app/favicon.ico
+0
-0
hmr.test.ts
test/development/app-hmr/hmr.test.ts
+69
-1
tsconfig.json
test/development/app-hmr/tsconfig.json
+24
-0
No files found.
packages/next/src/client/components/react-dev-overlay/app/hot-reloader-client.tsx
View file @
eb23134e
...
@@ -47,17 +47,35 @@ let __nextDevClientId = Math.round(Math.random() * 100 + Date.now())
...
@@ -47,17 +47,35 @@ let __nextDevClientId = Math.round(Math.random() * 100 + Date.now())
let
reloading
=
false
let
reloading
=
false
let
startLatency
:
number
|
null
=
null
let
startLatency
:
number
|
null
=
null
function
onBeforeFastRefresh
(
dispatcher
:
Dispatcher
,
hasUpdates
:
boolean
)
{
let
pendingHotUpdateWebpack
=
Promise
.
resolve
()
let
resolvePendingHotUpdateWebpack
:
()
=>
void
=
()
=>
{}
function
setPendingHotUpdateWebpack
()
{
pendingHotUpdateWebpack
=
new
Promise
((
resolve
)
=>
{
resolvePendingHotUpdateWebpack
=
()
=>
{
resolve
()
}
})
}
export
function
waitForWebpackRuntimeHotUpdate
()
{
return
pendingHotUpdateWebpack
}
function
handleBeforeHotUpdateWebpack
(
dispatcher
:
Dispatcher
,
hasUpdates
:
boolean
)
{
if
(
hasUpdates
)
{
if
(
hasUpdates
)
{
dispatcher
.
onBeforeRefresh
()
dispatcher
.
onBeforeRefresh
()
}
}
}
}
function
onFastRefresh
(
function
handleSuccessfulHotUpdateWebpack
(
dispatcher
:
Dispatcher
,
dispatcher
:
Dispatcher
,
sendMessage
:
(
message
:
string
)
=>
void
,
sendMessage
:
(
message
:
string
)
=>
void
,
updatedModules
:
ReadonlyArray
<
string
>
updatedModules
:
ReadonlyArray
<
string
>
)
{
)
{
resolvePendingHotUpdateWebpack
()
dispatcher
.
onBuildOk
()
dispatcher
.
onBuildOk
()
reportHmrLatency
(
sendMessage
,
updatedModules
)
reportHmrLatency
(
sendMessage
,
updatedModules
)
...
@@ -159,6 +177,7 @@ function tryApplyUpdates(
...
@@ -159,6 +177,7 @@ function tryApplyUpdates(
dispatcher
:
Dispatcher
dispatcher
:
Dispatcher
)
{
)
{
if
(
!
isUpdateAvailable
()
||
!
canApplyUpdates
())
{
if
(
!
isUpdateAvailable
()
||
!
canApplyUpdates
())
{
resolvePendingHotUpdateWebpack
()
dispatcher
.
onBuildOk
()
dispatcher
.
onBuildOk
()
reportHmrLatency
(
sendMessage
,
[])
reportHmrLatency
(
sendMessage
,
[])
return
return
...
@@ -281,12 +300,16 @@ function processMessage(
...
@@ -281,12 +300,16 @@ function processMessage(
}
else
{
}
else
{
tryApplyUpdates
(
tryApplyUpdates
(
function
onBeforeHotUpdate
(
hasUpdates
:
boolean
)
{
function
onBeforeHotUpdate
(
hasUpdates
:
boolean
)
{
onBeforeFastRefresh
(
dispatcher
,
hasUpdates
)
handleBeforeHotUpdateWebpack
(
dispatcher
,
hasUpdates
)
},
},
function
onSuccessfulHotUpdate
(
webpackUpdatedModules
:
string
[])
{
function
onSuccessfulHotUpdate
(
webpackUpdatedModules
:
string
[])
{
// Only dismiss it when we're sure it's a hot update.
// Only dismiss it when we're sure it's a hot update.
// Otherwise it would flicker right before the reload.
// Otherwise it would flicker right before the reload.
onFastRefresh
(
dispatcher
,
sendMessage
,
webpackUpdatedModules
)
handleSuccessfulHotUpdateWebpack
(
dispatcher
,
sendMessage
,
webpackUpdatedModules
)
},
},
sendMessage
,
sendMessage
,
dispatcher
dispatcher
...
@@ -320,6 +343,9 @@ function processMessage(
...
@@ -320,6 +343,9 @@ function processMessage(
}
}
case
HMR_ACTIONS_SENT_TO_BROWSER
.
BUILDING
:
{
case
HMR_ACTIONS_SENT_TO_BROWSER
.
BUILDING
:
{
startLatency
=
Date
.
now
()
startLatency
=
Date
.
now
()
if
(
!
process
.
env
.
TURBOPACK
)
{
setPendingHotUpdateWebpack
()
}
console
.
log
(
'[Fast Refresh] rebuilding'
)
console
.
log
(
'[Fast Refresh] rebuilding'
)
break
break
}
}
...
@@ -426,6 +452,7 @@ function processMessage(
...
@@ -426,6 +452,7 @@ function processMessage(
reloading
=
true
reloading
=
true
return
window
.
location
.
reload
()
return
window
.
location
.
reload
()
}
}
resolvePendingHotUpdateWebpack
()
startTransition
(()
=>
{
startTransition
(()
=>
{
router
.
hmrRefresh
()
router
.
hmrRefresh
()
dispatcher
.
onRefresh
()
dispatcher
.
onRefresh
()
...
...
packages/next/src/client/components/router-reducer/fetch-server-response.ts
View file @
eb23134e
...
@@ -29,6 +29,7 @@ import {
...
@@ -29,6 +29,7 @@ import {
import
{
callServer
}
from
'../../app-call-server'
import
{
callServer
}
from
'../../app-call-server'
import
{
PrefetchKind
}
from
'./router-reducer-types'
import
{
PrefetchKind
}
from
'./router-reducer-types'
import
{
hexHash
}
from
'../../../shared/lib/hash'
import
{
hexHash
}
from
'../../../shared/lib/hash'
import
{
waitForWebpackRuntimeHotUpdate
}
from
'../react-dev-overlay/app/hot-reloader-client'
export
interface
FetchServerResponseOptions
{
export
interface
FetchServerResponseOptions
{
readonly
flightRouterState
:
FlightRouterState
readonly
flightRouterState
:
FlightRouterState
...
@@ -180,6 +181,14 @@ export async function fetchServerResponse(
...
@@ -180,6 +181,14 @@ export async function fetchServerResponse(
return
doMpaNavigation
(
responseUrl
.
toString
())
return
doMpaNavigation
(
responseUrl
.
toString
())
}
}
// We may navigate to a page that requires a different Webpack runtime.
// In prod, every page will have the same Webpack runtime.
// In dev, the Webpack runtime is minimal for each page.
// We need to ensure the Webpack runtime is updated before executing client-side JS of the new page.
if
(
process
.
env
.
NODE_ENV
!==
'production'
&&
!
process
.
env
.
TURBOPACK
)
{
await
waitForWebpackRuntimeHotUpdate
()
}
// Handle the `fetch` readable stream that can be unwrapped by `React.use`.
// Handle the `fetch` readable stream that can be unwrapped by `React.use`.
const
response
:
NavigationFlightResponse
=
await
createFromFetch
(
const
response
:
NavigationFlightResponse
=
await
createFromFetch
(
Promise
.
resolve
(
res
),
Promise
.
resolve
(
res
),
...
...
test/development/app-hmr/app/bundler-runtime-changes/new-runtime-functionality/page.tsx
0 → 100644
View file @
eb23134e
'use client'
// requires
import
*
as
React
from
'react'
export
default
function
RuntimeChangesNewRuntimeFunctionalityPage
()
{
React
.
useEffect
(()
=>
{},
[])
return
<
div
data
-
testid=
"new-runtime-functionality-page"
/>
}
test/development/app-hmr/app/bundler-runtime-changes/page.tsx
0 → 100644
View file @
eb23134e
import
Link
from
'next/link'
export
default
function
RuntimeChangesPage
()
{
return
(
<
Link
href=
"/bundler-runtime-changes/new-runtime-functionality"
>
Click me
</
Link
>
)
}
test/development/app-hmr/app/favicon.ico
0 → 100644
View file @
eb23134e
14.7 KB
test/development/app-hmr/hmr.test.ts
View file @
eb23134e
...
@@ -99,7 +99,7 @@ describe(`app-dir-hmr`, () => {
...
@@ -99,7 +99,7 @@ describe(`app-dir-hmr`, () => {
'window.__TEST_NO_RELOAD === undefined'
'window.__TEST_NO_RELOAD === undefined'
)
)
// Used to be flaky but presumably no longer is.
// Used to be flaky but presumably no longer is.
// If this flakes again, please add the received value as a comm
ne
t.
// If this flakes again, please add the received value as a comm
en
t.
expect
({
envValue
,
mpa
}).
toEqual
({
expect
({
envValue
,
mpa
}).
toEqual
({
envValue
:
'ipad'
,
envValue
:
'ipad'
,
mpa
:
false
,
mpa
:
false
,
...
@@ -245,5 +245,73 @@ describe(`app-dir-hmr`, () => {
...
@@ -245,5 +245,73 @@ describe(`app-dir-hmr`, () => {
it
(
'should have no unexpected action error for hmr'
,
async
()
=>
{
it
(
'should have no unexpected action error for hmr'
,
async
()
=>
{
expect
(
next
.
cliOutput
).
not
.
toContain
(
'Unexpected action'
)
expect
(
next
.
cliOutput
).
not
.
toContain
(
'Unexpected action'
)
})
})
it
(
'can navigate cleanly to a page that requires a change in the Webpack runtime'
,
async
()
=>
{
// This isn't a very accurate test since the Webpack runtime is somewhat an implementation detail.
// To ensure this is still valid, check the `*/webpack.*.hot-update.js` network response content when the navigation is triggered.
// If there is new functionality added, the test is still valid.
// If not, the test doesn't cover anything new.
// TODO: Enforce console.error assertions or MPA navigation assertions in all tests instead.
const
browser
=
await
next
.
browser
(
'/bundler-runtime-changes'
)
await
browser
.
eval
(
'window.__TEST_NO_RELOAD = true'
)
await
browser
.
elementByCss
(
'a'
)
.
click
()
.
waitForElementByCss
(
'[data-testid="new-runtime-functionality-page"]'
)
const
logs
=
await
browser
.
log
()
// TODO: Should assert on all logs but these are cluttered with logs from our test utils (e.g. playwright tracing or webdriver)
if
(
process
.
env
.
TURBOPACK
)
{
// FIXME: logging "rebuilding" multiple times instead of closing it of with "done in"
// Should just not branch here and have the same logs as Webpack.
expect
(
logs
).
toEqual
(
expect
.
arrayContaining
([
{
message
:
'[Fast Refresh] rebuilding'
,
source
:
'log'
,
},
{
message
:
'[Fast Refresh] rebuilding'
,
source
:
'log'
,
},
{
message
:
'[Fast Refresh] rebuilding'
,
source
:
'log'
,
},
])
)
expect
(
logs
).
not
.
toEqual
(
expect
.
arrayContaining
([
{
message
:
expect
.
stringContaining
(
'[Fast Refresh] done in'
),
source
:
'log'
,
},
])
)
}
else
{
expect
(
logs
).
toEqual
(
expect
.
arrayContaining
([
{
message
:
'[Fast Refresh] rebuilding'
,
source
:
'log'
,
},
{
message
:
expect
.
stringContaining
(
'[Fast Refresh] done in'
),
source
:
'log'
,
},
])
)
expect
(
logs
).
not
.
toEqual
(
expect
.
arrayContaining
([
expect
.
objectContaining
({
source
:
'error'
,
}),
])
)
}
// No MPA navigation triggered
expect
(
await
browser
.
eval
(
'window.__TEST_NO_RELOAD'
)).
toEqual
(
true
)
})
})
})
})
})
test/development/app-hmr/tsconfig.json
0 → 100644
View file @
eb23134e
{
"compilerOptions"
:
{
"target"
:
"ES2017"
,
"lib"
:
[
"dom"
,
"dom.iterable"
,
"esnext"
],
"allowJs"
:
true
,
"skipLibCheck"
:
true
,
"strict"
:
false
,
"noEmit"
:
true
,
"incremental"
:
true
,
"module"
:
"esnext"
,
"esModuleInterop"
:
true
,
"moduleResolution"
:
"node"
,
"resolveJsonModule"
:
true
,
"isolatedModules"
:
true
,
"jsx"
:
"preserve"
,
"plugins"
:
[
{
"name"
:
"next"
}
]
},
"include"
:
[
"next-env.d.ts"
,
".next/types/**/*.ts"
,
"**/*.ts"
,
"**/*.tsx"
],
"exclude"
:
[
"node_modules"
]
}
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