Skip to content

Commit 5e5a06b

Browse files
authored
Fix paginated urls with custom routing (ie. when using SSG) (#288)
1 parent 56ac0d0 commit 5e5a06b

File tree

2 files changed

+66
-24
lines changed

2 files changed

+66
-24
lines changed

src/Cascade.php

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,17 @@ public function canonicalUrl()
124124
{
125125
$url = Str::trim($this->explicitUrl ?? $this->data->get('canonical_url'));
126126

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

131-
$page = (int) app('request')->get('page');
131+
if (! $paginator = Blink::get('tag-paginator')) {
132+
return $url;
133+
}
134+
135+
$paginator->setPath($url);
136+
137+
$page = $paginator->currentPage();
132138

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

140-
if (Str::startsWith($url, config('app.url'))) {
141-
$url .= '?page='.$page;
142-
}
143-
144-
return $url;
146+
return URL::makeAbsolute($paginator->url($page));
145147
}
146148

147149
protected function prevUrl()
@@ -150,21 +152,25 @@ protected function prevUrl()
150152
return null;
151153
}
152154

155+
$url = Str::trim($this->data->get('canonical_url'));
156+
157+
if (! Str::startsWith($url, config('app.url'))) {
158+
return $url;
159+
}
160+
153161
if (! $paginator = Blink::get('tag-paginator')) {
154162
return null;
155163
}
156164

157-
$url = Str::trim($this->data->get('canonical_url'));
158-
159-
$page = $paginator->currentPage();
160-
161-
if (config('statamic.seo-pro.pagination.enabled_on_first_page') === false && $page === 2) {
165+
if (config('statamic.seo-pro.pagination.enabled_on_first_page') === false && $paginator->currentPage() === 2) {
162166
return $url;
163167
}
164168

165-
return $page > 1 && $page <= $paginator->lastPage()
166-
? $url.'?page='.($page - 1)
167-
: null;
169+
if (! $prevUrl = $paginator->previousPageUrl()) {
170+
return null;
171+
}
172+
173+
return URL::makeAbsolute($prevUrl);
168174
}
169175

170176
protected function nextUrl()
@@ -173,17 +179,21 @@ protected function nextUrl()
173179
return null;
174180
}
175181

182+
$url = Str::trim($this->data->get('canonical_url'));
183+
184+
if (! Str::startsWith($url, config('app.url'))) {
185+
return $url;
186+
}
187+
176188
if (! $paginator = Blink::get('tag-paginator')) {
177189
return null;
178190
}
179191

180-
$url = Str::trim($this->data->get('canonical_url'));
181-
182-
$page = $paginator->currentPage();
192+
if (! $nextUrl = $paginator->nextPageUrl()) {
193+
return null;
194+
}
183195

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

189199
protected function parse($key, $item)

tests/MetaTagTest.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Pagination\LengthAwarePaginator;
66
use Illuminate\Support\Facades\Artisan;
77
use Illuminate\Support\Facades\Route;
8+
use Statamic\Extensions\Pagination\LengthAwarePaginator as StatamicLengthAwarePaginator;
89
use Statamic\Facades\Blink;
910
use Statamic\Facades\Collection;
1011
use Statamic\Facades\Config;
@@ -594,6 +595,25 @@ public function it_can_apply_pagination_to_first_page_when_configured_as_unique_
594595
$response->assertSee('<link href="http://cool-runnings.com/about?page=1" rel="canonical" />', false);
595596
}
596597

598+
/**
599+
* @test
600+
*
601+
* @dataProvider viewScenarioProvider
602+
*
603+
* @environment-setup useFakeSsgPaginator
604+
*/
605+
public function it_applies_custom_pagination_routing_to_meta_urls($viewType)
606+
{
607+
$this->withoutExceptionHandling();
608+
$this->prepareViews($viewType);
609+
610+
$response = $this->simulatePageOutOfFive(3, FakeSsgPaginator::class);
611+
$response->assertSee("<h1>{$viewType}</h1>", false);
612+
$response->assertSee('<link href="http://cool-runnings.com/about/page/3" rel="canonical" />', false);
613+
$response->assertSee('<link href="http://cool-runnings.com/about/page/2" rel="prev" />', false);
614+
$response->assertSee('<link href="http://cool-runnings.com/about/page/4" rel="next" />', false);
615+
}
616+
597617
/**
598618
* @test
599619
*
@@ -765,10 +785,22 @@ protected function setCustomTwitterGlidePresetOnly($app)
765785
]);
766786
}
767787

768-
protected function simulatePageOutOfFive($currentPage)
788+
protected function simulatePageOutOfFive($currentPage, $customPaginatorClass = null)
769789
{
770-
Blink::put('tag-paginator', new LengthAwarePaginator([], 15, 3, $currentPage));
790+
$url = '/about';
791+
792+
$paginatorClass = $customPaginatorClass ?? LengthAwarePaginator::class;
771793

772-
return $this->call('GET', '/about', ['page' => $currentPage]);
794+
Blink::put('tag-paginator', (new $paginatorClass([], 15, 3, $currentPage))->setPath($url));
795+
796+
return $this->call('GET', $url, ['page' => $currentPage]);
797+
}
798+
}
799+
800+
class FakeSsgPaginator extends StatamicLengthAwarePaginator
801+
{
802+
public function url($page)
803+
{
804+
return \Statamic\Facades\URL::makeRelative(sprintf('%s/page/%s', $this->path(), $page));
773805
}
774806
}

0 commit comments

Comments
 (0)