Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"language": "en,en-gb",
"words": [
"apos",
"beforeunload",
"camelcase",
"tapable",
"sockjs",
Expand Down
1 change: 1 addition & 0 deletions client-src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ const overlay =
*/
const reloadApp = ({ hot, liveReload }, currentStatus) => {
if (currentStatus.isUnloading) {
log.info("Reload prevented.");
return;
}

Expand Down
39 changes: 39 additions & 0 deletions examples/client/beforeunload/hmr-fallback/README.md
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
Copy link
Member

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?

Copy link
Author

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.

Copy link
Member

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?

Copy link
Author

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?

Copy link
Member

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 page

Copy link
Author

@nakjun12 nakjun12 Aug 26, 2025

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?

Copy link
Member

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)

Copy link
Author

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.

79 changes: 79 additions & 0 deletions examples/client/beforeunload/hmr-fallback/app.js
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 examples/client/beforeunload/hmr-fallback/webpack.config.js
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,
},
});
39 changes: 39 additions & 0 deletions examples/client/beforeunload/live-reload/README.md
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
79 changes: 79 additions & 0 deletions examples/client/beforeunload/live-reload/app.js
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 examples/client/beforeunload/live-reload/webpack.config.js
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,
},
});