Skip to content

Fix: Failed to read the 'cssRules'#51

Merged
jeawhanlee merged 8 commits into
feature/3.19-beaconsfrom
fix/cors-issue
May 23, 2025
Merged

Fix: Failed to read the 'cssRules'#51
jeawhanlee merged 8 commits into
feature/3.19-beaconsfrom
fix/cors-issue

Conversation

@Miraeld
Copy link
Copy Markdown
Contributor

@Miraeld Miraeld commented May 15, 2025

Description

Fixes #(issue number)
Fix the CORS issue leading to SecurityError: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules

Type of change

  • Bug fix (non-breaking change which fixes an issue).

Detailed scenario

What was tested

The beacon script

How to test

Load a page with a google font loaded externally

Technical description

Documentation

This pull request introduces a new feature to the BeaconPreloadFonts class that inlines Google Fonts CSS to avoid CORS issues during font loading. The changes ensure smoother font rendering by preloading and inlining external stylesheets.

New Feature: Inlining Google Fonts CSS

  • Added a new method inlineGoogleFonts to the BeaconPreloadFonts class in src/BeaconPreloadFonts.js. This method fetches and inlines Google Fonts CSS, deduplicates stylesheets, and removes external link elements to prevent CORS issues.
  • Updated the run method in BeaconPreloadFonts to call inlineGoogleFonts before waiting for fonts to load, ensuring that the inlined styles are applied early in the font loading process.

New dependencies

None

Risks

None

Mandatory Checklist

Code validation

  • I validated all the Acceptance Criteria. If possible, provide screenshots or videos.
  • I triggered all changed lines of code at least once without new errors/warnings/notices.
  • I implemented built-in tests to cover the new/changed code.

Code style

  • I wrote a self-explanatory code about what it does.
  • I protected entry points against unexpected inputs.
  • I did not introduce unnecessary complexity.
  • Output messages (errors, notices, logs) are explicit enough for users to understand the issue and are actionnable.

@codacy-production
Copy link
Copy Markdown

codacy-production Bot commented May 15, 2025

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
+0.31% (target: -1.00%) 84.64% (target: 50.00%)
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (88bf335) 1398 1113 79.61%
Head commit (cf8c954) 1624 (+226) 1298 (+185) 79.93% (+0.31%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#51) 267 226 84.64%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

@Miraeld Miraeld self-assigned this May 16, 2025
Miraeld and others added 4 commits May 16, 2025 03:15
@Mai-Saad
Copy link
Copy Markdown

on related PR at the plugin , we need to confirm that we fixed the double reqqquest mentioned here https://group-onecom.slack.com/archives/C08F4LZB2QG/p1747644521126159

@jeawhanlee jeawhanlee merged commit 65069d9 into feature/3.19-beacons May 23, 2025
5 of 6 checks passed
@jeawhanlee jeawhanlee deleted the fix/cors-issue branch May 23, 2025 14:05
jeawhanlee added a commit that referenced this pull request Jun 2, 2025
* Added preload fonts beacon

* Updated beacon manager and entry point

* Updated test

* Added test

* Remove comment

* Updated docblock

* Updated doc block

* Updated property

* Updated logger method

* Use updated method

* Added more tests

* Added more tests

* Updated entry point

* Added yet more test

* Fixed codacy critical issues

* Fixed codacy critical issues

* Try to fix medium codacy issue

* Added logic to return font urls

* Updated test

* Added preconnect beacon

* Added test

* Added doc block

* Updated test

* Update to use set to remove duplicate entries

* Added test

* Wait for fonts to be loaded

* Updated test

* Updated conditional to check if stylesheet is internal

* Added test

* Make font extensions exclusions dynamic

* Merge pull request #48 from wp-media/enhancement/47-exclusions-part

refactor system fonts to be generic exclusions list

* Closes #7289: 3.19 Preconnect to external domains - exclusions filter (#52)

* Updated exclusions logic to be compatible with array of strings pattern

* Updated tests

* Updated test data

* Store the url origin in the final result instead of hostname (PR #55)

* Fix: Failed to read the 'cssRules' (#51)

* Implement inlineGoogleFonts method to fetch and inline Google Fonts CSS, avoiding CORS issues

* Add tests for inlineGoogleFonts method to validate fetching and inlining of Google Fonts

* Refactor font handling: Implement externalStylesheetsDoc method to fetch and parse external font stylesheets, and update _initializeExternalFontSheets to manage parsed results.

* remove external urls from stylesheets

* fix test

* add Adame's suggestion and it is working

---------

Co-authored-by: WordPressFan <[email protected]>
Co-authored-by: Michael Lee <[email protected]>

* Add font family to allFonts object and push new variation only if matching url is valid (#56)

---------

Co-authored-by: Michael Lee <[email protected]>
Co-authored-by: Michael Lee <[email protected]>
Co-authored-by: Gaël Robin <[email protected]>
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.

3.19 - Preload Fonts - Beacon processing Pilot - Parse stylesheets and extract font properties

4 participants