Skip to content

Fix MDX optimize edge case#15475

Merged
delucis merged 3 commits intomainfrom
chris/14611
Feb 11, 2026
Merged

Fix MDX optimize edge case#15475
delucis merged 3 commits intomainfrom
chris/14611

Conversation

@delucis
Copy link
Member

@delucis delucis commented Feb 11, 2026

Changes

Fixes some edge cases in the MDX optimize option

mdx({ optimize: true }) enables our rehypeOptimizeStatic() plugin which turns as much of the AST as possible into static HTML to speed up rendering.

As part of this, it looks for a user-declared components variable, which in MDX is used to override specific nodes with a custom component, e.g. export const components = { a: MyLinkComponent } renders all Markdown links ([text](link)) using the custom component. It extracts the keys in that object, and then skips optimizing the parts of the document tree that contain that kind of node.

The logic to do this previously was quite basic and could fail to correctly extract the keys in some scenarios, like not having the export separated by new lines. This change fixes support those cases.

Notes

  • Found this while investigating [@astrojs/mdx]: optimize config option causes passed components to be ignored #14611. It technically does not fix that issue — the behaviour described there by OP is expected — but this fixes an issue described by another user later in the thread.

  • In terms of performance, this change does moderately more work: it iterates over some arrays instead of just looking at the first entry each time. However, I think it should not be too big an impact: this code is already gated by a regular expression check, so only runs on relevant nodes, max. 1 per document, and in most cases these are very small arrays (1–10 entries).

  • I also noticed while testing this that we never optimise top-level nodes (see the skipped test in this PR). I’m not 100% sure that was intentional: it makes for some odd behaviour where <Content components={{ p: MyP }} /> will override for top-level paragraphs but not for any nested ones. It would be a breaking change, but we should probably also optimise the top-level nodes where we can.

Testing

Added a test to the fixture to cover the case this fixes.

Docs

N/A — bug fix only.

@changeset-bot
Copy link

changeset-bot bot commented Feb 11, 2026

🦋 Changeset detected

Latest commit: d2358f2

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: integration Related to any renderer integration (scope) label Feb 11, 2026
@delucis delucis changed the title Chris/14611 Fix MDX optimize edge case Feb 11, 2026
@delucis delucis merged commit 36fc0e0 into main Feb 11, 2026
58 of 60 checks passed
@delucis delucis deleted the chris/14611 branch February 11, 2026 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: integration Related to any renderer integration (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants