Skip to content

Avoid Array.prototype.toSorted to fix editor crash in older browsers - #86132

Closed
mdtanjid0 wants to merge 2 commits into
WordPress:trunkfrom
mdtanjid0:fix/86129-avoid-array-to-sorted
Closed

mdtanjid0 wants to merge 2 commits into
WordPress:trunkfrom
mdtanjid0:fix/86129-avoid-array-to-sorted

Conversation

@mdtanjid0

Copy link
Copy Markdown
Contributor

What?

Closes #86129

Replaces browser-side Array.prototype.toSorted() calls with an explicit copy-then-sort, and adds an ESLint rule so toSorted isn't reintroduced in code that runs in the browser.

Why?

#52213 switched several sorts to Array.prototype.toSorted() (ES2023). toSorted is not available in Chrome < 110, Safari < 16 or Firefox < 115, and the polyfill build doesn't include it because those browsers are outside the browserslist targets. In those browsers the call throws TypeError: … .toSorted is not a function, which crashes the editor, for example when opening the inserter (orderBy in block-editor/src/utils/sorting.ts).

Some of these browser versions cannot be upgraded on the operating systems they run on. For example, the last Chrome for macOS 10.11/10.12 is 103, and the last Chrome for Windows 7/8.1 is 109. Before #52213 the editor loaded in these browsers.

How?

  • array.toSorted( cmp ) → [ ...array ].sort( cmp ) in all 13 browser-side call sites. The behavior is the same: a new sorted array is returned, the input is not mutated, and Array.prototype.sort is stable.
  • Where the call is chained directly on .filter() (blocks/src/api/factory.ts, core-fields/…/media_thumbnail/view.tsx), it becomes a plain .sort(), because filter already returns a new array.
  • Adds a noArrayToSorted entry to the shared restrictedSyntax list in tools/eslint/config.mjs. A new override exempts Node-only tooling (bin/, tools/, packages/babel-plugin-makepot), where toSorted is supported, so the existing calls there stay as they are. test/performance has its own no-restricted-syntax config and is unaffected.

Testing Instructions

  1. Open the post editor in a browser with this PR applied.
  2. In the DevTools console, run delete Array.prototype.toSorted to simulate Chrome < 110 / Safari < 16.
  3. Open the block inserter, search for a block, and insert one. On trunk this crashes the editor; with this PR it works.
  4. With toSorted still deleted, smoke-test the other changed paths:
    • Gallery block: reorder images by date or title.
    • Navigation block: create a menu from classic menu items.
    • Playlist block: reorder tracks.
    • Tags panel: add and remove tags.
    • Notes (collab sidebar): add notes to several blocks.
  5. Optionally, repeat in a real old browser such as Chrome 103 or Safari 15.
  6. npx eslint on any file under packages/*/src that calls .toSorted() should now report no-restricted-syntax.

Automated checks run locally:

  • npm run test:unit for the affected areas (block-editor utils, blocks API, gallery/navigation/playlist, editor collab-sidebar/collaborators-presence/post-taxonomies, views, media-fields, core-fields, fields and media-utils media-edit): 107 files and 1,457 tests pass.
  • With Array.prototype.toSorted deleted in a test, orderBy and sortImageBlocks throw on trunk and pass on this branch.
  • ESLint shows no new problems compared with trunk on the changed files.

Testing Instructions for Keyboard

No UI changes.

Screenshots or screencast

N/A. There are no visual changes.

Use of AI Tools

This PR was authored with Claude Code (Claude Opus 5.5). It was used to locate the call sites, apply the mechanical replacement, write the ESLint rule, and run the lint and unit tests above. I reviewed the diff and the test results.

🤖 Generated with Claude Code

`Array.prototype.toSorted()` (ES2023) is not available in Chrome < 110,
Safari < 16 and Firefox < 115. Since WordPress#52213, calling it in the editor
throws `TypeError: ... .toSorted is not a function` in those browsers,
which crashes the editor (e.g. when opening the inserter).

- Replace browser-side `.toSorted( cmp )` calls with
  `[ ...array ].sort( cmp )`, or `.sort( cmp )` when chained directly on
  a `.filter()` result that is already a new array. Behavior is
  unchanged: a new sorted array is returned and the input is not mutated.
- Add a `no-restricted-syntax` ESLint rule against `toSorted`, exempting
  Node-only tooling (`bin/`, `tools/`, `babel-plugin-makepot`), where it
  is supported.

Fixes WordPress#86129.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added [Package] Blocks /packages/blocks [Package] Editor /packages/editor [Package] Block library /packages/block-library [Package] Block editor /packages/block-editor [Package] Media Utils /packages/media-utils [Package] Fields /packages/fields [Package] Views /packages/views labels Nov 1, 2026
@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground

👋 Welcome

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @mdtanjid0! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

🎉 Props

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @mohammad.tanjid@automattic.com.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: mohammad.tanjid@automattic.com.

Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>
Co-authored-by: mdtanjid0 <mdtanjid@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

@github-actions github-actions Bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Nov 1, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ramonjd
ramonjd requested a review from jsnajdr November 1, 2026 21:30
@ramonjd ramonjd added the [Type] Plugin Interoperability Incompatibilities between a specific plugin and the block editor. Close with workaround notes. label Nov 1, 2026
@ramonjd

ramonjd commented Nov 1, 2026

Copy link
Copy Markdown
Member

Thanks for the PR. I think reverting to sort is harmless, and probably a good measure for now.

Curious to hear what @jsnajdr and others thing about the lint rule however, which codifies an exception to the browserlist rules.

@jsnajdr

jsnajdr commented Nov 2, 2026

Copy link
Copy Markdown
Member

I'd rather prefer #86159 which adds the toSorted polyfill. Keeps the modern syntax, and adds an explicit polyfill for older browsers. The polyfill was not added automatically because our browserslist config says we support only the 2 latest versions of Chrome. The Chrome 103 version that doesn't support toSorted yet is 4 years and 50 versions behind.

@jsnajdr

jsnajdr commented Nov 2, 2026

Copy link
Copy Markdown
Member

Closing in favor of #86159.

@jsnajdr jsnajdr closed this Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Block editor /packages/block-editor [Package] Block library /packages/block-library [Package] Blocks /packages/blocks [Package] Editor /packages/editor [Package] Fields /packages/fields [Package] Media Utils /packages/media-utils [Package] Views /packages/views [Type] Plugin Interoperability Incompatibilities between a specific plugin and the block editor. Close with workaround notes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Array.prototype.toSorted() breaks editor in Chrome <110, Safari <16, Firefox <115 (no polyfill)

3 participants