Skip to content

fix: update canonical tag on AJAX navigation for category and landing pages#389

Open
ahuininga-orisha wants to merge 1 commit into
developfrom
fix/220254-ajax-canonical-pagination
Open

fix: update canonical tag on AJAX navigation for category and landing pages#389
ahuininga-orisha wants to merge 1 commit into
developfrom
fix/220254-ajax-canonical-pagination

Conversation

@ahuininga-orisha

@ahuininga-orisha ahuininga-orisha commented May 22, 2026

Copy link
Copy Markdown
Collaborator

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 canonical key 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 — adds getCanonicalUrl() method and includes canonical in the serialized AJAX response. When p >= 2 the 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 — adds isPaginatedCanonicalEnabled() reading tweakwise/seo/paginated_canonical_enabled.
  • Model/Observer/CategoryPaginatedCanonical.php — new observer on core_layout_block_create_after that 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 — adds Enable Paginated Canonical URLs toggle in the SEO group.
  • etc/config.xml — default value 0 for the new config.
  • view/frontend/web/js/navigation-form.js — calls _updateCanonical(response.canonical) in the AJAX success handler; the method updates href on the existing canonical tag if present.
  • README.md — documents the new setting.

Requires setup:di:compile and cache:clean after deploy. Related to Magento2TweakwiseHyva and Magento2AttributeLandingTweakwise for Hyva and landing page support.

How to test

Scenario 1 — AJAX navigation updates canonical (Luma)

  1. Enable the module: tweakwise/layered/enabled = 1, tweakwise/layered/ajax_filters = 1, catalog/seo/category_canonical_tag = 1, tweakwise/seo/paginated_canonical_enabled = 1. Run cache:clean.
  2. Open a category page (e.g. /women/tops-women2/).
  3. Inspect <link rel="canonical"> in DevTools — note the initial value.
  4. Apply a filter using the layered navigation sidebar.
  5. After the AJAX response, inspect the canonical tag again — it should reflect the current filtered URL.
  6. Navigate to page 2 (?p=2). Canonical should include ?p=2.

Scenario 2 — Config disabled, canonical unchanged

  1. Set tweakwise/seo/paginated_canonical_enabled = 0. Run cache:clean.
  2. Apply a filter via AJAX.
  3. Canonical tag in DOM should remain unchanged after navigation.

Scenario 3 — No canonical tag present, nothing added

  1. Set catalog/seo/category_canonical_tag = 0. Run cache:clean.
  2. Load a category page — confirm no <link rel="canonical"> in <head>.
  3. Apply a filter via AJAX.
  4. Confirm no canonical tag is inserted into the DOM.

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