Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pagination): ensure canonical URLs exclude ?page=1 #70

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/MetaTags/Concerns/ManageLinksTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getCanonical(): ?TagInterface

public function setPaginationLinks(Paginator $paginator): self
{
$this->setCanonical($paginator->url($paginator->currentPage()));
$this->setCanonical($paginator->currentPage() > 1 ? $paginator->url($paginator->currentPage()) : $paginator->url(1));

$this->setNextHref($paginator->nextPageUrl());
$this->setPrevHref($paginator->previousPageUrl());
Expand Down
52 changes: 45 additions & 7 deletions tests/MetaTags/PaginatorMetaTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,69 @@

class PaginatorMetaTagsTest extends TestCase
{
/**
* Test pagination meta tags for the first page.
* Ensures the canonical URL does not include ?page=1 for SEO reasons.
*/
function test_its_can_be_set_from_paginator()
{
$meta = $this->makeMetaTags();

$paginator = m::mock(Paginator::class);

$paginator->shouldReceive('nextPageUrl')->once()->andReturn('http://site.com/next');
$paginator->shouldReceive('previousPageUrl')->once()->andReturn('http://site.com/prev');
// Mock methods to simulate paginator behavior for the first page.
$paginator->shouldReceive('nextPageUrl')->once()->andReturn('http://site.com?page=2');
$paginator->shouldReceive('previousPageUrl')->once()->andReturn(null); // No previous page for the first page
$paginator->shouldReceive('currentPage')->once()->andReturn(1);
$paginator->shouldReceive('url')->once()->andReturn('http://site.com/1');
$paginator->shouldReceive('url')->with(1)->andReturn('http://site.com'); // Canonical URL without ?page=1
$paginator->shouldReceive('url')->with(2)->andReturn('http://site.com?page=2');

$meta->setPaginationLinks($paginator);

$this->assertHtmlableContains(
'<link rel="next" href="http://site.com/next">',
'<link rel="next" href="http://site.com?page=2">',
$meta->getNextHref()
);

$this->assertHtmlableContains(
'<link rel="prev" href="http://site.com/prev">',
'<link rel="canonical" href="http://site.com">',
$meta->getCanonical()
);
}

/**
* Test pagination meta tags for page 10.
* Ensures canonical and next/prev links are generated correctly.
*/
function test_its_can_be_set_from_paginator_page_10()
{
$meta = $this->makeMetaTags();

$paginator = m::mock(Paginator::class);

// Allow multiple calls to currentPage()
$paginator->shouldReceive('currentPage')->atLeast()->once()->andReturn(10);

// Other mock setups remain unchanged
$paginator->shouldReceive('nextPageUrl')->once()->andReturn('http://site.com?page=11');
$paginator->shouldReceive('previousPageUrl')->once()->andReturn('http://site.com?page=9');
$paginator->shouldReceive('url')->with(10)->andReturn('http://site.com?page=10');
$paginator->shouldReceive('url')->with(11)->andReturn('http://site.com?page=11');

$meta->setPaginationLinks($paginator);

$this->assertHtmlableContains(
'<link rel="next" href="http://site.com?page=11">',
$meta->getNextHref()
);

$this->assertHtmlableContains(
'<link rel="prev" href="http://site.com?page=9">',
$meta->getPrevHref()
);

$this->assertHtmlableContains(
'<link rel="canonical" href="http://site.com/1">',
'<link rel="canonical" href="http://site.com?page=10">',
$meta->getCanonical()
);
}
Expand Down Expand Up @@ -160,4 +198,4 @@ function test_next_href_string_should_be_cleaned()
$meta->getNextHref()
);
}
}
}
Loading