Pagination error on refresh

Hello,

I’m having a problem with a pagination item.
I have a collection of items (programs) that can be filtered into three categories: Originals, Seasons, and Episodes. A filtered link would look like this

domain.test/programs/category:Episodes

So, I created a pagination for that collection and everything goes well, it works like this:

domain.test/programs/page:2
domain.test/programs/page:3
domain.test/programs/category:Episodes/page:2
domain.test/programs/category:Episodes/page:3
domain.test/programs/category:Seasons/page:2

Now, the problem comes whenever I refresh the page with a pagination number greater than “page:1” I get the following error:

Kirby\Exception\ErrorPageException thrown with message "Pagination page 2 does not exist, expected 1-1"

image

No matter if I have a category filter on or not, if I hit refresh or land on this page, this happens.

This is my controller:

<?php

return function($page) {

	// fetch the basic set of pages
	$programs = $page->children()->listed();
	$episodes = $page->children()->listed()->children()->listed()->children()->listed()->sortBy('date','desc');
	$seasons = $page->children()->listed()->children()->listed()->sortBy('date','desc');

	// fetch all categories
	$categories = $programs->pluck('categories', ',', true);
	$categoriesnumber = $programs->pluck('categories', ',');


	// add the category filter
	if($category = urldecode(param('category'))) {
		if ($category == 'Original') {
			$programs = $programs->filterBy('original', true);
		} elseif ($category == 'Temporadas') {
			$programs = $seasons;
		} elseif ($category == 'Episodios') {
			$programs = $episodes;
		} else{
			$programs = $programs->filterBy('categories', $category, ',');
		}
	}

	// add the tag filter
	if($tag = urldecode(param('tag'))) {
		$programs = $episodes->filterBy('tags', $tag, ',');
	}

	$categoriesnumber = array_count_values($categoriesnumber);

	// apply pagination
	$pagination = $programs->paginate(3); //This is number to test the pagination
	return compact('programs', 'categories', 'category', 'categoriesnumber', 'tag', 'pagination', 'episodes', 'seasons');

};

Thanks in advance.

Edit:

Forgot to mention I’m using Fetch in Javascript to dynamically load everything. Could this be a problem?

The problem is that you haven’t defined the pagination object (correctly):

$programs   = $programs->paginate(3);
$pagination = $programs->pagination();

Hmm, that’s weird, still happenning. :frowning_face:

My controller with suggested edit:

<?php

return function($page) {

	// fetch the basic set of pages
	$programs = $page->children()->listed();
	$episodes = $page->children()->listed()->children()->listed()->children()->listed()->sortBy('date','desc');
	$seasons = $page->children()->listed()->children()->listed()->sortBy('date','desc');

	// fetch all categories
	$categories = $programs->pluck('categories', ',', true);
	$categoriesnumber = $programs->pluck('categories', ',');

	// add the category filter
	if($category = urldecode(param('category'))) {
		if ($category == 'Original') {
			$programs = $programs->filterBy('original', true);
		} elseif ($category == 'Temporadas') {
			$programs = $seasons;
		} elseif ($category == 'Episodios') {
			$programs = $episodes;
		} else{
			$programs = $programs->filterBy('categories', $category, ',');
		}
	}

	// add the tag filter
	if($tag = urldecode(param('tag'))) {
		$programs = $episodes->filterBy('tags', $tag, ',');
	}

	$categoriesnumber = array_count_values($categoriesnumber);

	// apply pagination
	$programs   = $programs->paginate(3);
	$pagination = $programs->pagination();
	//$pagination = $programs->paginate(3);

	//return compact('programs', 'categories', 'category', 'categoriesnumber', 'tags', 'tag', 'pagination', 'tagsnumber');
	return compact('programs', 'categories', 'category', 'categoriesnumber', 'tag', 'pagination', 'episodes', 'seasons');

};

My template

    <section class="ener__programs grid-margin">
        <div class="is-row is-flex column-padding">
            <div class="is-col offset-1 col-10">
                <div class="is-row is-row--responsive is-flex">
                    
                        <?php foreach ($programs as $program): ?>
                            <?php snippet('modules/card.program', ['program' => $program, 'size' => 'm', 'xs_size' => '6',]) ?>
                        <?php endforeach ?>
                    
                </div>
                <?php if ($pagination->hasPages()): ?>
                <div class="item-60"></div>
                <nav class="container grid-margin">
                    <ul class="is-row is-row--responsive is-flex column-padding ener__latest-releases__feed__pagination__list">
                        <?php if ($pagination->hasPrevPage()): ?>
                        <li class="is-col col-6 xs-col-6 ener__latest-releases__feed__pagination__list--left">
                            <h2>
                                <a href="<?php echo $pagination->prevPageURL() ?>" class="section-link" data-uri="" data-title=""><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16" height="16" viewBox="0 0 16 16"><path d="M16 7H3.83l5.59-5.59L8 0 0 8l8 8 1.41-1.41L3.83 9H16z" fill="currentColor"></path></svg> Anterior</a>
                            </h2>
                        </li>
                        <?php else: ?>
                        <li class="is-col col-6 xs-col-6 ener__latest-releases__feed__pagination__list--left inactive-pagination">
                            <h2>
                                <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16" height="16" viewBox="0 0 16 16"><path d="M16 7H3.83l5.59-5.59L8 0 0 8l8 8 1.41-1.41L3.83 9H16z" fill="currentColor"></path></svg> Anterior
                            </h2>
                        </li>
                        <?php endif ?>
                        <?php if ($pagination->hasNextPage()): ?>
                        <li class="is-col col-6 xs-col-6 ener__latest-releases__feed__pagination__list--right">
                            <h2>
                                <a href="<?php echo $pagination->nextPageURL() ?>" class="section-link" data-uri="" data-title="">Siguiente <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16" height="16" viewBox="0 0 16 16"><path d="M8 0L6.59 1.41 12.17 7H0v2h12.17l-5.58 5.59L8 16l8-8z" fill="currentColor"></path></svg></a>
                            </h2>
                        </li>
                        <?php else: ?>
                        <li class="is-col col-6 xs-col-6 ener__latest-releases__feed__pagination__list--right inactive-pagination">
                            <h2>
                                Siguiente <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16" height="16" viewBox="0 0 16 16"><path d="M8 0L6.59 1.41 12.17 7H0v2h12.17l-5.58 5.59L8 16l8-8z" fill="currentColor"></path></svg>
                            </h2>
                        </li>
                        <?php endif ?>
                    </ul>
                </nav>
                <?php endif ?>
            </div>
        </div>
    </section>
    <!-- PROGRAMS -->

List and Pagination work but can’t still refresh.

Jeez, turns out I had a duplicate pagination var in another snippet that was messing this up on new Fetch load.

Oh well, it’s solved now. :sparkles: :ok_hand: