Merged
Conversation
🦋 Changeset detectedLatest 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 |
1 task
matthewp
approved these changes
Feb 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
Fixes some edge cases in the MDX
optimizeoptionmdx({ optimize: true })enables ourrehypeOptimizeStatic()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
componentsvariable, 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]:optimizeconfig option causes passedcomponentsto 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.