Skip to content

fix(chromium): apply offline mode to pages created in offline context - #42177

Open
KARTIK SAHU (yash2277-ctrl) wants to merge 1 commit into
microsoft:mainfrom
yash2277-ctrl:fix/navigator-online-offline-context
Open

fix(chromium): apply offline mode to pages created in offline context#42177
KARTIK SAHU (yash2277-ctrl) wants to merge 1 commit into
microsoft:mainfrom
yash2277-ctrl:fix/navigator-online-offline-context

Conversation

@yash2277-ctrl

Copy link
Copy Markdown

Summary

This PR fixes a bug where pages created in an already-offline BrowserContext incorrectly report navigator.onLine === true instead of false.

Fixes

Closes #42174

Problem

When creating a page on a BrowserContext that already has offline mode enabled:

  • context.setOffline(true) is called first
  • Then context.newPage() is called
  • The new page reports navigator.onLine === true (WRONG)
  • Expected: navigator.onLine === false

Toggling offline on existing pages works correctly.

Root Cause

During page initialization in FrameSession._initialize(), the offline state from the parent context was never applied to the new page. While other context options like geolocation, user agent, and emulated media were properly applied during initialization, updateOffline() was missing from the initialization chain.

Solution

Added this._crPage.updateOffline() to the initialization promise chain in _initialize(), ensuring new pages receive their parent context's offline state during setup.

Changes

  • Modified: packages/playwright-core/src/server/chromium/crPage.ts
  • Added updateOffline() call during page initialization
  • Only affects Chromium (Firefox/WebKit have different implementations)

Impact

  • Pages created in offline contexts now correctly report navigator.onLine === false
  • No breaking changes
  • No impact on existing page toggling behavior
  • Minimal code change (1 line)

Testing

Tested with the repro from #42174:
`javascript
const { chromium } = require('@playwright/test');

(async () => {
const browser = await chromium.launch();
const context = await browser.newContext();
await context.setOffline(true);
const page = await context.newPage();
const result = await page.evaluate(() => navigator.onLine);
console.log(result); // Now correctly returns false
await browser.close();
})();
`

Type of Change

Fixes microsoft#42174

When a page was created on a BrowserContext that already had offline mode
enabled via setOffline(true), navigator.onLine incorrectly reported true
instead of false.

The root cause was that updateOffline() was not being called during page
initialization in _initialize(). While updateOffline() was properly called
when toggling offline mode on existing pages, newly created pages never
received the initial offline state from their parent context.

This fix adds updateOffline() to the initialization promise chain,
ensuring that pages born into an offline context correctly reflect the
offline state from the start, making navigator.onLine === false as expected.

The fix only affects Chromium, as the offline state is applied during
page initialization alongside other context options like geolocation,
user agent, and emulated media.

Testing:
- Pages created after setOffline(true) now report navigator.onLine === false
- Toggling offline on existing pages continues to work
- No impact on pages created in online contexts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chromium: page born into setOffline(true) context keeps navigator.onLine === true (1.62.0)

1 participant