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 paginated urls with custom routing (ie. when using SSG) #288

Merged
merged 4 commits into from
Aug 4, 2023
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
52 changes: 31 additions & 21 deletions src/Cascade.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,17 @@ public function canonicalUrl()
{
$url = Str::trim($this->explicitUrl ?? $this->data->get('canonical_url'));

if (! app('request')->has('page')) {
if (! Str::startsWith($url, config('app.url'))) {
return $url;
}

$page = (int) app('request')->get('page');
if (! $paginator = Blink::get('tag-paginator')) {
return $url;
}

$paginator->setPath($url);

$page = $paginator->currentPage();

switch (true) {
case config('statamic.seo-pro.pagination') === false:
Expand All @@ -137,11 +143,7 @@ public function canonicalUrl()
return $url;
}

if (Str::startsWith($url, config('app.url'))) {
$url .= '?page='.$page;
}

return $url;
return URL::makeAbsolute($paginator->url($page));
}

protected function prevUrl()
Expand All @@ -150,21 +152,25 @@ protected function prevUrl()
return null;
}

$url = Str::trim($this->data->get('canonical_url'));

if (! Str::startsWith($url, config('app.url'))) {
return $url;
}

if (! $paginator = Blink::get('tag-paginator')) {
return null;
}

$url = Str::trim($this->data->get('canonical_url'));

$page = $paginator->currentPage();

if (config('statamic.seo-pro.pagination.enabled_on_first_page') === false && $page === 2) {
if (config('statamic.seo-pro.pagination.enabled_on_first_page') === false && $paginator->currentPage() === 2) {
return $url;
}

return $page > 1 && $page <= $paginator->lastPage()
? $url.'?page='.($page - 1)
: null;
if (! $prevUrl = $paginator->previousPageUrl()) {
return null;
}

return URL::makeAbsolute($prevUrl);
}

protected function nextUrl()
Expand All @@ -173,17 +179,21 @@ protected function nextUrl()
return null;
}

$url = Str::trim($this->data->get('canonical_url'));

if (! Str::startsWith($url, config('app.url'))) {
return $url;
}

if (! $paginator = Blink::get('tag-paginator')) {
return null;
}

$url = Str::trim($this->data->get('canonical_url'));

$page = $paginator->currentPage();
if (! $nextUrl = $paginator->nextPageUrl()) {
return null;
}

return $page < $paginator->lastPage()
? $url.'?page='.($page + 1)
: null;
return URL::makeAbsolute($nextUrl);
}

protected function parse($key, $item)
Expand Down
38 changes: 35 additions & 3 deletions tests/MetaTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Route;
use Statamic\Extensions\Pagination\LengthAwarePaginator as StatamicLengthAwarePaginator;
use Statamic\Facades\Blink;
use Statamic\Facades\Collection;
use Statamic\Facades\Config;
Expand Down Expand Up @@ -594,6 +595,25 @@ public function it_can_apply_pagination_to_first_page_when_configured_as_unique_
$response->assertSee('<link href="http://cool-runnings.com/about?page=1" rel="canonical" />', false);
}

/**
* @test
*
* @dataProvider viewScenarioProvider
*
* @environment-setup useFakeSsgPaginator
*/
public function it_applies_custom_pagination_routing_to_meta_urls($viewType)
{
$this->withoutExceptionHandling();
$this->prepareViews($viewType);

$response = $this->simulatePageOutOfFive(3, FakeSsgPaginator::class);
$response->assertSee("<h1>{$viewType}</h1>", false);
$response->assertSee('<link href="http://cool-runnings.com/about/page/3" rel="canonical" />', false);
$response->assertSee('<link href="http://cool-runnings.com/about/page/2" rel="prev" />', false);
$response->assertSee('<link href="http://cool-runnings.com/about/page/4" rel="next" />', false);
}

/**
* @test
*
Expand Down Expand Up @@ -765,10 +785,22 @@ protected function setCustomTwitterGlidePresetOnly($app)
]);
}

protected function simulatePageOutOfFive($currentPage)
protected function simulatePageOutOfFive($currentPage, $customPaginatorClass = null)
{
Blink::put('tag-paginator', new LengthAwarePaginator([], 15, 3, $currentPage));
$url = '/about';

$paginatorClass = $customPaginatorClass ?? LengthAwarePaginator::class;

return $this->call('GET', '/about', ['page' => $currentPage]);
Blink::put('tag-paginator', (new $paginatorClass([], 15, 3, $currentPage))->setPath($url));

return $this->call('GET', $url, ['page' => $currentPage]);
}
}

class FakeSsgPaginator extends StatamicLengthAwarePaginator
{
public function url($page)
{
return \Statamic\Facades\URL::makeRelative(sprintf('%s/page/%s', $this->path(), $page));
}
}