feat(astro): add observeDynamicLinks option for prefetch settings#16583
Open
rururux wants to merge 1 commit intowithastro:mainfrom
Open
feat(astro): add observeDynamicLinks option for prefetch settings#16583rururux wants to merge 1 commit intowithastro:mainfrom
observeDynamicLinks option for prefetch settings#16583rururux wants to merge 1 commit intowithastro:mainfrom
Conversation
🦋 Changeset detectedLatest commit: 717a9d9 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 |
rururux
commented
May 4, 2026
|
|
||
| // https://astro.build/config | ||
| export default defineConfig({ | ||
| adapter: node({ mode: 'standalone' }), |
Member
Author
There was a problem hiding this comment.
This adapter is required to run tests using components that utilize the server:defer property.
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.
fixes #13297
Changes
In the current implementation, enabling prefetch settings causes links to be collected via
document.getElementsByTagName('a')on the initial page load.While this approach works for most cases, it fails to cover certain scenarios, such as the one reported in issue #13297.
The root cause of issue #13297 is that when a component with the
server:deferattribute uses top-level await, its links are rendered after the Promise resolves, meaning they are not captured by the initialdocument.getElementsByTagName('a')call.More broadly, the same issue occurs whenever links are dynamically added to the DOM after the initial page load.
To address this, this pull request adds the
observeDynamicLinksoption, which uses aMutationObserverto watch for dynamically added link elements.This ensures that links added after the initial render are properly captured.
This feature is opt-in.
Why MutationObserver?
Investigation process
server:deferto see if it used any event notifications.astro:page-loadevent].astro/packages/astro/src/prefetch/index.ts
Line 316 in 7711e47
astro:page-loadcould be used here, I found that the constant defining this event name is marked as@deprecated, so I decided to look for a different approach.astro/packages/astro/src/transitions/events.ts
Lines 12 to 13 in 7711e47
server:defer, the same problem can occur in other situations where links are added to the DOM dynamically. For this reason, I wanted an approach that resolves the issue within the prefetch code itself, rather than adding prefetch-dependent logic to theserver:deferside.MutationObserveris the best approach, as it is well-suited for observing DOM changes and allows the fix to be fully self-contained within the prefetch implementation.Testing
Added tests to verify that the
observeDynamicLinksoption works correctly in the following scenarios:server:defer, as reported in issue Prefetch not working in components with server:defer and top-level await #13297ToggleButton.jsx)Docs
/cc @withastro/maintainers-docs for feedback!