Skip to content

feat: Add responsive widths config endpoint and CORS iframe processing to core#2121

Merged
prklm10 merged 17 commits intomasterfrom
PPLT-4949-add-cors-iframe-handling-and-multi-dom-width-config
Mar 10, 2026
Merged

feat: Add responsive widths config endpoint and CORS iframe processing to core#2121
prklm10 merged 17 commits intomasterfrom
PPLT-4949-add-cors-iframe-handling-and-multi-dom-width-config

Conversation

@bhokaremoin
Copy link
Copy Markdown
Contributor

@bhokaremoin bhokaremoin commented Feb 26, 2026

Overview

This PR introduces two major enhancements to the Percy CLI core package:

  1. A new /percy/widths-config endpoint for responsive snapshot capture with mobile device height support
  2. CORS iframe processing logic moved from SDK-specific implementations to core for reusability across all SDKs

Changes

1. Responsive Widths Configuration Endpoint

New API Endpoint: GET /percy/widths-config

  • Returns computed widths list for responsive snapshot capture
  • Accepts optional widths query parameter (e.g., ?widths=375,1280)
  • Merges three width sources intelligently:
    • Mobile device widths: Includes height information from deviceDetails
    • User-passed widths: From query parameters (without heights)
    • Config widths: Fallback from Percy configuration (without heights)
  • Returns deduplicated, sorted array of width objects
  • Mobile widths are prioritized to preserve height information when duplicates exist

Example Response:

{
  "success": true,
  "widths": [
    { "width": 375 },
    { "width": 390, "height": 844 },
    { "width": 428, "height": 926 },
    { "width": 1920 }
  ]
}

SDK Integration:

  • New getResponsiveWidths() function in @percy/sdk-utils
  • All SDKs can now fetch computed widths with a single API call
  • Graceful error handling returns empty array on failure

2. CORS Iframe Processing (Moved from Playwright SDK)

Centralized CORS iframe handling in core:

  • Previously, CORS iframe processing was implemented in @percy/playwright
  • Now moved to @percy/core for reusability across all SDKs
  • Processes corsIframes array in DOM snapshots before validation

Processing Logic:

  • Extracts iframe snapshots and resources from corsIframes array
  • Creates HTML resources for each iframe with text/html MIME type
  • Appends percy_width query parameter to iframe URLs for responsive context
  • Merges iframe resources into main snapshot resources
  • Updates iframe src attributes in HTML with processed URLs
  • Cleans up corsIframes property after processing

New Utility Functions:

  • processCorsIframes() - Handles single object or array of DOM snapshots
  • processCorsIframesInDomSnapshot() - Processes CORS iframes in single snapshot
  • appendUrlSearchParam() - Safely appends query parameters to URLs

Benefits:

  • ✅ All SDKs can now handle CORS iframes without duplicating logic
  • ✅ Consistent behavior across Playwright, Selenium, Puppeteer, etc.
  • ✅ Centralized maintenance and testing

Files Changed

Core Package

  • packages/core/src/api.js - New /percy/widths-config endpoint
  • packages/core/src/percy.js - CORS iframe processing integration
  • packages/core/src/utils.js - CORS processing & width computation utilities

SDK Utils Package

  • packages/sdk-utils/src/get-responsive-widths.js - New utility (added)
  • packages/sdk-utils/src/index.js - Export new function

Tests

  • packages/core/test/api.test.js - 11 tests for widths-config endpoint
  • packages/core/test/snapshot.test.js - 7 tests for CORS iframe processing
  • packages/core/test/utils.test.js - 37 tests for utility functions
  • packages/sdk-utils/test/index.test.js - 11 tests for getResponsiveWidths

Test Coverage

  • 48 new test cases added
  • ✅ CORS iframe processing (single, multiple, arrays, edge cases)
  • ✅ Responsive widths computation (deduplication, sorting, priorities)
  • ✅ URL parameter handling (append, update, invalid URLs)
  • ✅ SDK integration (error handling, response parsing, edge cases)

Breaking Changes

None - This is purely additive functionality.

Related Issues

  • PPLT-4949

- Add GET /percy/widths-config endpoint to compute responsive widths
- Implement computeResponsiveWidths() in core utils for width/height calculation
- Add getResponsiveWidths() SDK method to fetch computed widths
- Mobile device widths include height, other widths are height-agnostic
- Returns widths sorted in ascending order
- Add processCorsIframes and processCorsIframesInDomSnapshot utilities in utils.js
- Add appendUrlSearchParam utility for adding query parameters to URLs
- Integrate CORS iframe processing in snapshot method before validation
- Process iframe resources and update HTML src attributes with width-aware URLs
- Support both single domSnapshot object and array of domSnapshots
@bhokaremoin bhokaremoin changed the title Pplt 4949 add cors iframe handling and multi dom width config feat: Add responsive widths config endpoint and CORS iframe processing to core Feb 28, 2026
@bhokaremoin bhokaremoin marked this pull request as ready for review February 28, 2026 11:15
@bhokaremoin bhokaremoin requested a review from a team as a code owner February 28, 2026 11:15
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds core support for responsive-width computation (including mobile device height metadata) and centralizes CORS iframe snapshot processing so all SDKs can reuse the same logic.

Changes:

  • Introduces GET /percy/widths-config to compute a merged, deduped, sorted list of responsive widths (preserving mobile heights where applicable).
  • Moves CORS iframe extraction/rewriting into @percy/core snapshot flow via new utility helpers.
  • Adds getResponsiveWidths() to @percy/sdk-utils plus extensive tests for both features.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/core/src/api.js Adds /percy/widths-config endpoint that returns computed responsive widths.
packages/core/src/utils.js Adds URL param helper, CORS iframe processing utilities, and responsive-width computation helper.
packages/core/src/percy.js Hooks CORS iframe processing into snapshot flow before validation.
packages/sdk-utils/src/get-responsive-widths.js Adds SDK helper to call /percy/widths-config and return computed widths.
packages/sdk-utils/src/index.js Exports getResponsiveWidths from sdk-utils.
packages/core/test/api.test.js Tests for /percy/widths-config behavior across multiple scenarios.
packages/core/test/utils.test.js Unit tests for computeResponsiveWidths, URL param helper, and CORS iframe processing.
packages/core/test/snapshot.test.js Integration tests ensuring snapshots process CORS iframes and upload resources.
packages/sdk-utils/test/index.test.js Tests sdk-utils getResponsiveWidths() request formatting and failure handling.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@bhokaremoin
Copy link
Copy Markdown
Contributor Author

@copilot open a new pull request to apply changes based on the comments in this thread

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 28, 2026

@bhokaremoin I've opened a new pull request, #2123, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 28, 2026

@bhokaremoin I've opened a new pull request, #2124, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 28, 2026

@bhokaremoin I've opened a new pull request, #2125, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 28, 2026

@bhokaremoin I've opened a new pull request, #2126, to work on those changes. Once the pull request is ready, I'll request review from you.

…sInDomSnapshot (#2124)

* Initial plan

* fix: escape percyElementId before using in RegExp to prevent regex injection

Co-authored-by: bhokaremoin <75439850+bhokaremoin@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: bhokaremoin <75439850+bhokaremoin@users.noreply.github.com>
Copilot AI and others added 2 commits February 28, 2026 23:27
* Initial plan

* Use Array.isArray check in getResponsiveWidths to ensure stable array return type

Co-authored-by: bhokaremoin <75439850+bhokaremoin@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: bhokaremoin <75439850+bhokaremoin@users.noreply.github.com>
…malformed input (#2125)

* Initial plan

* Add validation for malformed corsIframes entries in processCorsIframesInDomSnapshot

Co-authored-by: bhokaremoin <75439850+bhokaremoin@users.noreply.github.com>

* adding validation for widths as well

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: bhokaremoin <75439850+bhokaremoin@users.noreply.github.com>
Co-authored-by: bhokaremoin <bhokaremoin@gmail.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 28, 2026

@bhokaremoin I've opened a new pull request, #2127, to work on those changes. Once the pull request is ready, I'll request review from you.

type: 'array',
items: { type: 'string' }
},
corsIframes: {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had to add it here, since post validate we can do domSnapshot.corsIframe.
Tried get it processed before validation, then faced the issue where for Js its domSnapshot and Python its dom_snapshot. adding handling for both didn't make sense

@prklm10 prklm10 merged commit bb0c86f into master Mar 10, 2026
72 of 75 checks passed
@prklm10 prklm10 deleted the PPLT-4949-add-cors-iframe-handling-and-multi-dom-width-config branch March 10, 2026 12:04
@bhokaremoin bhokaremoin added the ✨ enhancement New feature or request label Mar 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

✨ enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants