Skip to content

[4.x] Do not add identical preload links twice - #14

Open
Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-deduplicate-links
Open

Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-deduplicate-links

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary of Changes

GenericLinkProvider de-duplicates links by object identity only, and PreloadManager::link() creates a new Link object on every call. Preloading the same asset twice — typical when several modules or plugins add the same file — therefore puts the same entry into the Link header twice (preload('/a.css', ['as' => 'style']) twice → two identical entries), which grows the header needlessly towards server/proxy header-size limits.

link() now skips a link whose URI, relations and attributes (compared strictly, independent of order) equal those of a link the provider already holds, including links that were already in a custom provider passed to setLinkProvider(). Links that differ in any attribute value are still added.

The finding also mentioned an optional cap on the number of links; that is not part of this PR, as there is no obvious limit that fits every server and the duplicates were the actual source of growth.

Testing Instructions

php vendor/bin/phpunit. New tests in PreloadManagerTest: identical links (also with attributes in a different order, and via link()) are stored once; links differing in URI, relation or attributes are all kept; a link already in a custom provider is not added again. Before the change testIdenticalLinkIsAddedOnce and testLinkAlreadyInProviderIsNotAddedAgain fail.

Documentation Changes Required

docs/classes/PreloadManager.md, section "Adding links": describes the de-duplication.

The owner's docs PR #12 says in docs/overview.md ("Things to know before you build on this" → No deduplication and no limit.) that identical entries are produced twice; that paragraph needs adjusting (the missing limit still applies) when the second of the two PRs is merged.

🤖 Generated with Claude Code

GenericLinkProvider only de-duplicates by object identity, so preloading
the same asset twice (for example from two modules) put the same entry
into the Link header twice. link() now skips a link whose URI, relations
and attributes equal those of a link the provider already holds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant