question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Load more pagination fails to show next entries when query is using relations

See original GitHub issue

Steps to reproduce the issue.

I’ve an entries’ query which is a little bit more complex than in the example, as it’s filtering using section and categories. The entries are filtered using sprig checkbox inputs that send the data to the entries query, which should then be paginated.

Below is a reduced test case of my code.

Section template

{{ sprig('_sprig/events', {
	limit: 3,
	base: entry.url
}) }}

Sprig template

{% set offset = offset ?? 0 %}
{% set entries = craft.entries.section((sections ?? null is not empty) ? sections : ['events', 'museums']).andRelatedTo(categories ?? []).with(['category', 'cover']).offset(offset).limit(limit) %}

{% for option in [{
	type: 'sections',
	id: 'events',
	title: 'Events' | t
}, {
	type: 'sections',
	id: 'museums',
	title: 'Museums' | t
}] | merge(craft.categories.group('categories').relatedTo(craft.entries.section(['events', 'museums']).ids()).all() | map((value, key) => {
	type: 'categories',
	id: value.id,
	title: value.title
})) %}
	{{ tag('input', {
		sprig: true,
		type: 'checkbox',
		id: '{type}-{i}' | t({
			type: option.type,
			i: loop.index
		}),
		name: '{type}[]' | t({
			type: option.type
		}),
		value: option.id,
		checked: (option.id in sections ?? [] or option.id in categories ?? []) ? 'checked',
	}) }}
{% endfor %}

{% for entry in entries.all() %}
	<div id="entries">
		{% set attr = {
			href: '{base}/{slug}' | t({
				base: base,
				slug: entry.slug
			})
		} %}
		<a {{ attr(attr) }}>
			{{ entry.title }}
		</a>
	</div>
{% endfor %}

{% if entries.count() > offset + entries.all() | length %}
	{% set attr = {
		id: 'load-more-oob',
		sprig: true,
		's-vals': {
			offset: (offset + limit) - 1,
			sections: sections ?? [],
			categories: categories ?? []
		} | json_encode,
		's-target': '#entries',
		's-select': '#entries a',
		's-swap': 'beforeend',
		's-swap-oob': (sprig.trigger == 'load-more-oob') ? 'true'
	}%}
	<button {{ attr(attr) }}>
		{{ 'Load more' | t | typogrify }}
	</button>
{% else %}
	<button id="load-more-oob" s-swap-oob="true"></button>
{% endif %}

A clear and concise description of what you expected to happen.

When the view is filtered using the sections ones, so first options, the pagination is working fine. When the view is filtered using the categories ones, it shows the first correctly, the show more button, but when clicking on it I get an empty content.

I tried to debug by adding this after <div id="entries">:

{{ d({
	entriesCount: entries.count(),
	offset: offset,
	entriesAllLength: entries.all() | length,
	limit: limit,
	loadMore: entries.count() > offset + entries.all() | length,
	sections: (sections ?? null is not empty) ? sections : ['events', 'museums'],
	categories: categories ?? []
}) }}

While entriesCount is correct, and loadMore is true, the next entries are not showing and then the load more button disappears.

I also tried to simplify the entries query and options, by removing the section filter:

  • Removing .section() from craft.entries and change andRelatedTo to relatedTo;
  • Removing events and museums from the options;

But I finally end with the same problem: load more button is showing, but clicking on it load an empty content + hide the load more button.

It looks like there’s a bug with Sprig when playing with relation (relatedTo or andRelatedTo).

The plugin and Craft version numbers

  • craftcms/cms: 4.2.3
  • putyourlightson/craft-sprig: 2.1.0

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
bencrokercommented, Sep 9, 2022

Great!

0reactions
romainpoiriercommented, Sep 9, 2022

Thank you, now that works! Don’t know why it was still not working in my previous answer, but you were right that it seems that the issue was 's-val:categories': categories ?? [] which is useless.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Efficient Pagination Using Deferred Joins - Aaron Francis
Cursor based pagination works by storing some state about the last record presented to the user and then basing the next query off...
Read more >
Pagination with rel="next" and rel="prev" - Google Developers
Hint to Google the relationship between the component URLs of your series with rel="next" and rel="prev" . This helps us more accurately index...
Read more >
SQL OFFSET FETCH Feature: Loading Large Volumes of ...
In this article, we illustrate how to use the OFFSET FETCH feature as a solution for loading large volumes of data from a...
Read more >
Need "connect by" type query with MySQL for pagination
My understanding of the issue is that one of the comments is returned out of order. Currently, your query is ordering all of...
Read more >
How To Query Tables and Paginate Data in Flask-SQLAlchemy
In this tutorial, you'll use Flask and Flask-SQLAlchemy to create an employee management system with a database that has a table for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found