fix: update canonical tag on AJAX navigation for category and landing pages#389
Open
ahuininga-orisha wants to merge 1 commit into
Open
fix: update canonical tag on AJAX navigation for category and landing pages#389ahuininga-orisha wants to merge 1 commit into
ahuininga-orisha wants to merge 1 commit into
Conversation
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.
When AJAX filtering is active, the
<link rel="canonical">tag in the page<head>was never updated after navigation. This meant that after a user applied filters or changed pages without a full reload, the canonical still pointed to the initial page URL — potentially causing duplicate-content issues in search engines.This PR adds a
canonicalkey to the AJAX JSON response and updates the canonical tag in the DOM after each successful AJAX navigation. The canonical is only updated if a canonical tag already exists in the page; no tag is inserted if the site has canonicals disabled.Changes:
Model/AjaxNavigationResult.php— addsgetCanonicalUrl()method and includescanonicalin the serialized AJAX response. Whenp >= 2the page parameter is appended to the URL; otherwise the response URL is returned as-is. Returns empty string when the feature is disabled via config.Model/Config.php— addsisPaginatedCanonicalEnabled()readingtweakwise/seo/paginated_canonical_enabled.Model/Observer/CategoryPaginatedCanonical.php— new observer oncore_layout_block_create_afterthat updates the server-side canonical tag for non-AJAX paginated category pages (reinforces Magento's own canonical behaviour when the feature is enabled).etc/frontend/events.xml— wires the new observer.etc/adminhtml/system.xml— addsEnable Paginated Canonical URLstoggle in the SEO group.etc/config.xml— default value0for the new config.view/frontend/web/js/navigation-form.js— calls_updateCanonical(response.canonical)in the AJAX success handler; the method updateshrefon the existing canonical tag if present.README.md— documents the new setting.Requires
setup:di:compileandcache:cleanafter deploy. Related to Magento2TweakwiseHyva and Magento2AttributeLandingTweakwise for Hyva and landing page support.How to test
Scenario 1 — AJAX navigation updates canonical (Luma)
tweakwise/layered/enabled = 1,tweakwise/layered/ajax_filters = 1,catalog/seo/category_canonical_tag = 1,tweakwise/seo/paginated_canonical_enabled = 1. Runcache:clean./women/tops-women2/).<link rel="canonical">in DevTools — note the initial value.?p=2). Canonical should include?p=2.Scenario 2 — Config disabled, canonical unchanged
tweakwise/seo/paginated_canonical_enabled = 0. Runcache:clean.Scenario 3 — No canonical tag present, nothing added
catalog/seo/category_canonical_tag = 0. Runcache:clean.<link rel="canonical">in<head>.