Skip to content

fix: allow null viewport to disable viewport emulation#107

Merged
Cloak-HQ merged 2 commits intoCloakHQ:mainfrom
kitiho:fix/allow-null-viewport
Apr 7, 2026
Merged

fix: allow null viewport to disable viewport emulation#107
Cloak-HQ merged 2 commits intoCloakHQ:mainfrom
kitiho:fix/allow-null-viewport

Conversation

@kitiho
Copy link
Copy Markdown
Contributor

@kitiho kitiho commented Apr 3, 2026

Problem

Passing viewport: null to launchPersistentContext() has no effect — the browser always launches with DEFAULT_VIEWPORT (1920×947) because the ?? operator treats null the same as undefined.

Root cause

viewport: options.viewport ?? DEFAULT_VIEWPORT

?? coalesces both null and undefined, so an explicit null (which in Playwright means "disable viewport emulation and let the OS window control the size") is silently replaced by DEFAULT_VIEWPORT.

Fix

Use a strict undefined check so null is passed through to Playwright:

viewport: options.viewport === undefined ? DEFAULT_VIEWPORT : options.viewport,

Also update the viewport type in LaunchContextOptions to accept null:

viewport?: { width: number; height: number } | null;

Why it matters

Desktop apps (e.g. Electron) need the browser content to resize with the window. With viewport: null, Playwright/Chromium uses the actual window dimensions and responds to resize events. Without this fix there is no way to opt out of the fixed 1920×947 default.

@kitiho kitiho force-pushed the fix/allow-null-viewport branch from 2af5d87 to a9da154 Compare April 3, 2026 05:48
@Cloak-HQ
Copy link
Copy Markdown
Contributor

Cloak-HQ commented Apr 3, 2026

Thanks for the clean PR, good catch on the ?? vs null behavior.

We confirmed the bug — viewport: null is a valid Playwright API value (disables viewport emulation), and ?? silently swallows it.

One thing: launchPersistentContext at line 171 has the same pattern:

viewport: options.viewport ?? DEFAULT_VIEWPORT,

Happy to merge this as-is and fix it ourselves, or if you'd like to add it to this PR that works too. Up to you.

Same bug as in newContext: `??` swallows `null`, preventing users from
disabling viewport emulation via `viewport: null`. Use explicit
`=== undefined` check instead.
@kitiho
Copy link
Copy Markdown
Contributor Author

kitiho commented Apr 7, 2026

Thanks for pointing that out! I've applied the same fix to launchPersistentContext as well — changed ?? to an explicit === undefined check so viewport: null is correctly passed through there too.

@Cloak-HQ Cloak-HQ merged commit 8eb2e4b into CloakHQ:main Apr 7, 2026
Cloak-HQ added a commit that referenced this pull request Apr 7, 2026
viewport=None now disables viewport emulation via Playwright's
no_viewport=True, matching the JS wrapper's viewport: null behavior.
Uses a sentinel to distinguish "not provided" from explicit None.

Co-authored-by: kitiho <51785099+kitiho@users.noreply.github.com>
@Cloak-HQ
Copy link
Copy Markdown
Contributor

Cloak-HQ commented Apr 7, 2026

Thanks for the fix, @kitiho! Merged and shipped.

We also applied the same fix to the Python wrapper (browser.py) in 1060772 — Python's Playwright API uses no_viewport=True instead of viewport=None, so we translate accordingly. You're credited as co-author on that commit.

Added you to the Contributors section in README as well.

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.

2 participants