-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: add logging for blocked updates when beforeunload is cancelled #5574
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
Open
nakjun12
wants to merge
6
commits into
webpack:main
Choose a base branch
from
nakjun12:feat-add-logging-for-blocked-updates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+106
−0
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6b913a4
feat: add beforeunload word
nakjun12 1071acb
feat: add beforeunload examples to reproduce isUnloading bug
nakjun12 08fb89d
feat: add reload prevention log message
nakjun12 15fd8ff
fix: clarify HMR blocking messages with actionable guidance
nakjun12 ae063b2
refactor: consolidate beforeunload examples
nakjun12 7dc1131
Merge branch 'master' into feat-add-logging-for-blocked-updates
nakjun12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
"language": "en,en-gb", | ||
"words": [ | ||
"apos", | ||
"beforeunload", | ||
"camelcase", | ||
"tapable", | ||
"sockjs", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# client.beforeunload Example | ||
|
||
This example reproduces a bug where the `isUnloading` flag gets stuck after canceling the "Leave site?" dialog, blocking HMR/Live Reload. | ||
|
||
## Bug Description | ||
|
||
When a user cancels the native confirmation dialog triggered by a `beforeunload` event listener, webpack-dev-server's internal `isUnloading` state remains `true`. This causes all subsequent HMR and live reloads to be ignored until manual refresh. | ||
|
||
## Configuration | ||
|
||
```js | ||
module.exports = { | ||
devServer: { | ||
hot: true, | ||
liveReload: false, // Intentionally configured to cause HMR fallback | ||
}, | ||
}; | ||
``` | ||
|
||
## How to Reproduce the Bug | ||
|
||
### Prerequisites | ||
|
||
- Enable "Slow 3G" in browser DevTools Network tab (to make the issue more visible) | ||
- Open browser console to see webpack-dev-server logs | ||
|
||
### Steps | ||
|
||
1. Run `npx webpack serve` and open `http://localhost:8080/` | ||
2. Click **"Add Beforeunload Event"** button | ||
3. Click **"Reload Page"** button (or press F5/Ctrl+R) | ||
4. When "Leave site?" dialog appears, click **"Cancel"** | ||
5. Edit `app.js` file (make any change to trigger rebuild) | ||
6. **Bug**: Page does not update despite file changes | ||
|
||
### Expected vs Actual Behavior | ||
|
||
**Expected**: After canceling dialog, file changes should still trigger page updates | ||
**Actual**: Page updates are completely blocked until manual refresh | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
"use strict"; | ||
|
||
const target = document.querySelector("#target"); | ||
|
||
// Beforeunload event handler | ||
function beforeunloadHandler(event) { | ||
console.log("[webpack-dev-server] beforeunload event triggered"); | ||
event.preventDefault(); | ||
event.returnValue = ""; | ||
return ""; | ||
} | ||
|
||
let isEventRegistered = false; | ||
|
||
// Create add event button | ||
const addEventButton = document.createElement("button"); | ||
addEventButton.textContent = "Add Beforeunload Event"; | ||
addEventButton.style.cssText = | ||
"padding: 10px 20px; margin: 10px; font-size: 16px; cursor: pointer; background-color: #28a745; color: white; border: none; border-radius: 4px;"; | ||
addEventButton.addEventListener("click", function () { | ||
if (!isEventRegistered) { | ||
window.addEventListener("beforeunload", beforeunloadHandler); | ||
isEventRegistered = true; | ||
updateStatus(); | ||
console.log("[webpack-dev-server] beforeunload event added"); | ||
} | ||
}); | ||
|
||
// Create remove event button | ||
const removeEventButton = document.createElement("button"); | ||
removeEventButton.textContent = "Remove Beforeunload Event"; | ||
removeEventButton.style.cssText = | ||
"padding: 10px 20px; margin: 10px; font-size: 16px; cursor: pointer; background-color: #dc3545; color: white; border: none; border-radius: 4px;"; | ||
removeEventButton.addEventListener("click", function () { | ||
if (isEventRegistered) { | ||
window.removeEventListener("beforeunload", beforeunloadHandler); | ||
isEventRegistered = false; | ||
updateStatus(); | ||
console.log("[webpack-dev-server] beforeunload event removed"); | ||
} | ||
}); | ||
|
||
// Create reload button | ||
const reloadButton = document.createElement("button"); | ||
reloadButton.textContent = "Reload Page"; | ||
reloadButton.style.cssText = | ||
"padding: 10px 20px; margin: 10px; font-size: 16px; cursor: pointer; background-color: #007bff; color: white; border: none; border-radius: 4px;"; | ||
reloadButton.addEventListener("click", function () { | ||
console.log("[webpack-dev-server] page reload triggered"); | ||
window.location.reload(); | ||
}); | ||
|
||
// Create status display | ||
const statusDisplay = document.createElement("div"); | ||
statusDisplay.style.cssText = | ||
"margin: 10px; padding: 10px; border: 2px solid #ccc; border-radius: 4px; font-weight: bold;"; | ||
|
||
function updateStatus() { | ||
statusDisplay.textContent = isEventRegistered | ||
? "Status: Beforeunload event is ACTIVE - Page exit will be blocked" | ||
: "Status: Beforeunload event is INACTIVE - Page exit will not be blocked"; | ||
statusDisplay.style.backgroundColor = isEventRegistered | ||
? "#d4edda" | ||
: "#f8d7da"; | ||
statusDisplay.style.borderColor = isEventRegistered ? "#28a745" : "#dc3545"; | ||
} | ||
|
||
target.classList.add("pass"); | ||
target.innerHTML = "Beforeunload Event Controller"; | ||
target.appendChild(document.createElement("br")); | ||
target.appendChild(document.createElement("br")); | ||
target.appendChild(statusDisplay); | ||
target.appendChild(document.createElement("br")); | ||
target.appendChild(addEventButton); | ||
target.appendChild(removeEventButton); | ||
target.appendChild(reloadButton); | ||
|
||
// Initialize status | ||
updateStatus(); |
14 changes: 14 additions & 0 deletions
14
examples/client/beforeunload/hmr-fallback/webpack.config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"use strict"; | ||
|
||
// our setup function adds behind-the-scenes bits to the config that all of our | ||
// examples need | ||
const { setup } = require("../../../util"); | ||
|
||
module.exports = setup({ | ||
context: __dirname, | ||
entry: "./app.js", | ||
devServer: { | ||
hot: true, | ||
liveReload: false, | ||
}, | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# client.beforeunload Example | ||
|
||
This example reproduces a bug where the `isUnloading` flag gets stuck after canceling the "Leave site?" dialog, blocking Live Reload updates. | ||
|
||
## Bug Description | ||
|
||
When a user cancels the native confirmation dialog triggered by a `beforeunload` event listener, webpack-dev-server's internal `isUnloading` state remains `true`. This causes all subsequent Live Reload updates to be ignored until manual refresh. | ||
|
||
## Configuration | ||
|
||
```js | ||
module.exports = { | ||
devServer: { | ||
hot: false, | ||
liveReload: true, // Test Live Reload scenario | ||
}, | ||
}; | ||
``` | ||
|
||
## How to Reproduce the Bug | ||
|
||
### Prerequisites | ||
|
||
- Enable "Slow 3G" in browser DevTools Network tab (to make the issue more visible) | ||
- Open browser console to see webpack-dev-server logs | ||
|
||
### Steps | ||
|
||
1. Run `npx webpack serve` and open `http://localhost:8080/` | ||
2. Click **"Add Beforeunload Event"** button | ||
3. Click **"Reload Page"** button (or press F5/Ctrl+R) | ||
4. When "Leave site?" dialog appears, click **"Cancel"** | ||
5. Edit `app.js` file (make any change to trigger rebuild) | ||
6. **Bug**: Page does not update despite file changes | ||
|
||
### Expected vs Actual Behavior | ||
|
||
**Expected**: After canceling dialog, file changes should still trigger Live Reload updates | ||
**Actual**: Live Reload updates are completely blocked until manual refresh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
"use strict"; | ||
|
||
const target = document.querySelector("#target"); | ||
|
||
// Beforeunload event handler | ||
function beforeunloadHandler(event) { | ||
console.log("[webpack-dev-server] beforeunload event triggered"); | ||
event.preventDefault(); | ||
event.returnValue = ""; | ||
return ""; | ||
} | ||
|
||
let isEventRegistered = false; | ||
|
||
// Create add event button | ||
const addEventButton = document.createElement("button"); | ||
addEventButton.textContent = "Add Beforeunload Event"; | ||
addEventButton.style.cssText = | ||
"padding: 10px 20px; margin: 10px; font-size: 16px; cursor: pointer; background-color: #28a745; color: white; border: none; border-radius: 4px;"; | ||
addEventButton.addEventListener("click", function () { | ||
if (!isEventRegistered) { | ||
window.addEventListener("beforeunload", beforeunloadHandler); | ||
isEventRegistered = true; | ||
updateStatus(); | ||
console.log("[webpack-dev-server] beforeunload event added"); | ||
} | ||
}); | ||
|
||
// Create remove event button | ||
const removeEventButton = document.createElement("button"); | ||
removeEventButton.textContent = "Remove Beforeunload Event"; | ||
removeEventButton.style.cssText = | ||
"padding: 10px 20px; margin: 10px; font-size: 16px; cursor: pointer; background-color: #dc3545; color: white; border: none; border-radius: 4px;"; | ||
removeEventButton.addEventListener("click", function () { | ||
if (isEventRegistered) { | ||
window.removeEventListener("beforeunload", beforeunloadHandler); | ||
isEventRegistered = false; | ||
updateStatus(); | ||
console.log("[webpack-dev-server] beforeunload event removed"); | ||
} | ||
}); | ||
|
||
// Create reload button | ||
const reloadButton = document.createElement("button"); | ||
reloadButton.textContent = "Reload Page"; | ||
reloadButton.style.cssText = | ||
"padding: 10px 20px; margin: 10px; font-size: 16px; cursor: pointer; background-color: #007bff; color: white; border: none; border-radius: 4px;"; | ||
reloadButton.addEventListener("click", function () { | ||
console.log("[webpack-dev-server] page reload triggered"); | ||
window.location.reload(); | ||
}); | ||
|
||
// Create status display | ||
const statusDisplay = document.createElement("div"); | ||
statusDisplay.style.cssText = | ||
"margin: 10px; padding: 10px; border: 2px solid #ccc; border-radius: 4px; font-weight: bold;"; | ||
|
||
function updateStatus() { | ||
statusDisplay.textContent = isEventRegistered | ||
? "Status: Beforeunload event is ACTIVE - Page exit will be blocked" | ||
: "Status: Beforeunload event is INACTIVE - Page exit will not be blocked"; | ||
statusDisplay.style.backgroundColor = isEventRegistered | ||
? "#d4edda" | ||
: "#f8d7da"; | ||
statusDisplay.style.borderColor = isEventRegistered ? "#28a745" : "#dc3545"; | ||
} | ||
|
||
target.classList.add("pass"); | ||
target.innerHTML = "Beforeunload Event Controller"; | ||
target.appendChild(document.createElement("br")); | ||
target.appendChild(document.createElement("br")); | ||
target.appendChild(statusDisplay); | ||
target.appendChild(document.createElement("br")); | ||
target.appendChild(addEventButton); | ||
target.appendChild(removeEventButton); | ||
target.appendChild(reloadButton); | ||
|
||
// Initialize status | ||
updateStatus(); |
14 changes: 14 additions & 0 deletions
14
examples/client/beforeunload/live-reload/webpack.config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"use strict"; | ||
|
||
// our setup function adds behind-the-scenes bits to the config that all of our | ||
// examples need | ||
const { setup } = require("../../../util"); | ||
|
||
module.exports = setup({ | ||
context: __dirname, | ||
entry: "./app.js", | ||
devServer: { | ||
hot: false, | ||
liveReload: true, | ||
}, | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we need this example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially added this example while working on the improvements, but I kept it because it clearly demonstrates the problem I was experiencing and helps with understanding the issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to fix the problem, not just logging?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I tried various approaches but concluded that logging would be sufficient for now.
Would you have any suggestions for a better approach?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately I don't think it can be fixed here, if you have logic
beforeunload
you need to do it manually refresh the pageUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand.
I think logging alone would help developers understand why HMR stops working.
I'll remove the examples. Would that be okay?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make only one example and provide lines how to fix it (i.e. reloading) - adding comment in code and in readme (in example)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the example as you suggested.
I changed the log level to warn and the message to recommend page refresh.