Skip to content

Commit 0246a67

Browse files
authored
Merge pull request #71 from atiksoftware/3.x
Fix canonical URL generation for first page in pagination
2 parents f407bbe + e912cb1 commit 0246a67

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/MetaTags/Concerns/ManageLinksTags.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ public function getCanonical(): ?TagInterface
5757

5858
public function setPaginationLinks(Paginator $paginator): self
5959
{
60-
$this->setCanonical($paginator->currentPage() > 1 ? $paginator->url($paginator->currentPage()) : $paginator->url(1));
61-
60+
$canonical = $paginator->url($paginator->currentPage());
61+
if ($paginator->currentPage() == 1) {
62+
$canonical = preg_replace('/[?&]page=\d+/', '', $canonical);
63+
$canonical = rtrim($canonical, '?');
64+
}
65+
$this->setCanonical($canonical);
6266
$this->setNextHref($paginator->nextPageUrl());
6367
$this->setPrevHref($paginator->previousPageUrl());
6468

tests/MetaTags/PaginatorMetaTagsTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function test_its_can_be_set_from_paginator()
2121
// Mock methods to simulate paginator behavior for the first page.
2222
$paginator->shouldReceive('nextPageUrl')->once()->andReturn('http://site.com?page=2');
2323
$paginator->shouldReceive('previousPageUrl')->once()->andReturn(null); // No previous page for the first page
24-
$paginator->shouldReceive('currentPage')->once()->andReturn(1);
24+
$paginator->shouldReceive('currentPage')->twice()->andReturn(1);
2525
$paginator->shouldReceive('url')->with(1)->andReturn('http://site.com'); // Canonical URL without ?page=1
2626
$paginator->shouldReceive('url')->with(2)->andReturn('http://site.com?page=2');
2727

0 commit comments

Comments
 (0)