fix: dedupe preload links and stop double-processing category collections#11
Merged
Conversation
…ions Toolbar::setCollection() fires twice per category page render (a pre-existing Magento/Luma quirk, same toolbar/collection instance both times), so SetCollection::afterSetCollection ran the same preload work twice, doubling every category-grid <link rel="preload"> tag. Two fixes: - SetCollection now tracks processed collections by spl_object_id() and skips a collection it's already handled. Holds a reference to the collection itself (not just the id) so the id can't be recycled by an unrelated object for the rest of the request. - LinkStore now keys its internal store by a hash of each link's getAttrs(), so any other duplicate-invocation case (present or future, in any observer/plugin) collapses to one entry as a backstop.
SamJUK
force-pushed
the
fix/duplicate-preload-links
branch
from
July 15, 2026 23:14
5430e60 to
996d42d
Compare
phpcs (Magento2 standard) forbids md5() and serialize() outright. Attrs is a flat scalar array, so json_encode() + sha256 is a drop-in replacement with no new dependency needed.
SamJUK
force-pushed
the
fix/duplicate-preload-links
branch
from
July 16, 2026 07:58
c90f9a3 to
03bec23
Compare
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.
Summary
QA on category pages found
<link rel="preload">tags doubled (8 tags for 4 images) onevery category listing. Root cause:
Magento\Catalog\Block\Product\ProductList\Toolbar::setCollection()fires twice per render (confirmed via instrumentation — same toolbar/collection instance
both times, a pre-existing Magento/Luma template quirk, not something this module introduced),
so
SetCollection::afterSetCollectionran its preload work twice.Two fixes:
SetCollection: tracks processed collections byspl_object_id(), skips a collectionit's already handled. Holds a reference to the collection object itself (not just the id)
so the id can't be recycled by an unrelated object later in the same request —
spl_object_id()values are only unique while the object is alive.
LinkStore: keys its internal store bymd5(serialize($link->getAttrs()))instead ofappending unconditionally, so any other duplicate-invocation case (present or future, in any
observer/plugin) collapses to one entry as a backstop — O(1) per
add(), not a scan.Test plan
testAddingLinkWithIdenticalAttrsIsDeduped/testAddingLinkWithDifferentAttrsIsNotDedupedto
LinkStoreTest.testDoesNotReprocessTheSameCollectionInstanceTwice/testProcessesDifferentCollectionInstancesIndependentlytoSetCollectionTest.?product_list_order=price&product_list_dir=desc):exactly 4
<link rel="preload" as="image">tags now (was 8), sort-order fix (SAM-6) still holds,no-gallery-image fix (SAM-44) still holds, product-page preload still emits exactly 1 link.