Kirby + Datastar + Infinite scrolling

I’m trying to create a photo blog that will load new (big!) photos once you scroll past what’s already loaded.

I’ve got the basics working from this example (Infinite Scroll Example) plus Kirby’s pagination, but it swaps out all the entries instead of appending. I think I’m missing some way to have Datastar on the frontend tell Kirby more of what it’s doing. What am I missing or does someone have an example they could point me towards?

Hi @trey, Without code parts of how you are trying to achieve this, it’s hard to know what/where this could go wrong.

Oh, of course!

Something like…

Controller:

<?php
return function ($page) {
	$images = $page->children();

	return [
		"images" => $images->paginate(2),
	];
};

Template:

<div>
	<!-- List of images -->

	<div data-on-intersect="@get('<?= $images->pagination()->nextPageURL() ?>')">
		<p>Loading...</p>
	</div>
</div>

I am not familiar with Datastar, but I would assume you must only sent back the parts you want to load/append? Right now, you would be calling always the same URL for page 2 (as '<?= $images->pagination()->nextPageURL() ?> gets rendered on your original page load and then doesn’t update, say with the URL to page 3, 4…). And if that’s all in you controller, those request would send back full pages to Datastar - not just the additional images.

I thought Datastar had some automatic merging of DOM on render, but I think what you said is exactly what’s happening. Is there some sort of standard way of returning a template partial like that for Ajax requests? I assume folks have figured out how to do such things with Kirby and htmx?

@trey Datastar can append without SSE: when a backend action returns plain text/html, it reads the datastar-selector and datastar-mode response headers. It also sends Datastar-Request: true with every request, so your existing template can just branch on that:

<?php if ($kirby->request()->header('Datastar-Request')) {
  $kirby->response()->header('datastar-selector', '#photos');
  $kirby->response()->header('datastar-mode', 'append');
  snippet('photo-batch', ['images' => $images]);
  return;
} ?>
<div id="photos"><?php snippet('photo-batch', ['images' => $images]) ?></div>

Inside photo-batch, render the images and then, if $images->pagination()->hasNextPage(), a new sentinel div with data-on-intersect__once calling @get() on nextPageURL(). Each batch then carries the trigger for the next page, so you stop requesting page 2 over and over, and __once keeps the old sentinels from firing again.

Do you have Kirby’s page cache enabled for this template? If so, the fragment response could get cached as the full page.

Thanks for this! That Kirby request/response stuff definitely helped. I had, in the meantime, switched to htmx and it was truly dead simple. I tried going back to Data Star using some of this technique, but I can’t figure out how to get the loading indicator to go away. With hx-swap="outerHTML" in htmx, it replaces itself on every new page. No need for a __once. Interesting to see the differences and similarities in these progressive enhancing libraries.

Hello, if it merges by element id, the old “Loading…” div may stay unless the last response replaces or removes it. Might be worth checking what the final page sends back when there are no more images. Either way, good that the Kirby side worked out.