Instatic icon indicating copy to clipboard operation
Instatic copied to clipboard

[Bug]: infinite-scroll pagination returns 404 when loop is inside a template-composed page

Open Daniel-Dev123 opened this issue 1 month ago • 1 comments

Summary

Bug description:

Infinite-scroll pagination (pagination: infinite) on base.loop nodes returns a 404 when the loop lives on a page that is rendered through a template chain (e.g. a page using an "everywhere" layout template). The "Load more" button shows "Try again" instead of loading the next page.

Steps to reproduce

------------------------ Steps to reproduce ------------------------

  1. Create a template with an outlet (e.g. a layout template).
  2. Create a page that uses this template.
  3. Add a base.loop node with pagination: infinite and a data.rows source on that page.
  4. Publish the site.
  5. Visit the published page and click "Load more".
  6. The browser console shows:

GET /instatic/loop/c0?page=2&pagePath=%2F 404 (Not Found) [instatic-loop] Error: Loop fetch failed: 404

----------------------- Root cause ---------------------------

When a page is rendered through a template chain, composeTemplateChain in src/core/templates/templateCompose.ts prefixes terminal-page node IDs with c0_ (and outer template IDs with t). The published HTML therefore carries data-instatic-loop="c0", and the client-side runtime (server/publish/loopRuntime.ts) sends that composed ID to the loop endpoint.

However, the published loop index built by getPublishedLoopIndexForVersion in server/publish/publishedSnapshotCache.ts iterates the original (non-composed) page nodes, so it stores the original ID without any prefix. The direct lookup in handleLoopRequest (server/handlers/cms/loop.ts) misses:

const indexed = loopIndex.loops.get(loopId) // loopId is "c0_", index has ""

------------------------ for team of INSTATIC . Fix(solution for especific case) ------------------------

The composedNodeSourceId utility in src/core/templates/templateCompose.ts already strips the c0_/t_ prefix. Using it as a fallback in the lookup resolves the original node:

  • import { composedNodeSourceId } from '@core/templates'

    // ...

  • const indexed = loopIndex.loops.get(loopId)
  • // The runtime sends the loop id as it appears in the DOM. When the page
  • // is rendered through a template chain, composeTemplateChain prefixes
  • // terminal-page node ids with c0_ (and outer template ids with t<i>_),
  • // so the DOM attribute carries a composed id like c0_<original>. The
  • // published loop index is built from the original (non-composed) page
  • // nodes, so a direct lookup misses. Strip the composition prefix as a
  • // fallback before giving up.
  • const indexed =
  • loopIndex.loops.get(loopId) ??
  • loopIndex.loops.get(composedNodeSourceId(loopId))

File: server/handlers/cms/loop.ts

------------------------ Verification  ------------------------
  • bun run build passes.
  • bun test passes (7005 pass; pre-existing failures in cmsPlugins.test.ts are unrelated).
  • Manual test: infinite-scroll pagination now loads subsequent pages correctly on template-composed pages.

------------------------ Environment ------------------------

  • Instatic version: 0.0.20
  • Reproduced on both SQLite and Postgres modes, local dev and published output.

Expected behavior

When clicking "Load more" on an infinite-scroll loop that lives on a template-composed page, the server should resolve the composed loop ID (e.g. c0_) back to the original node ID in the published loop index, fetch the next page slice from the loop's source, and return the rendered HTML fragment. The runtime appends the fragment before the "Load more" button and the pagination continues seamlessly — matching the behavior of infinite-scroll loops on non-template pages, which already work correctly.

Actual behavior

The "Load more" button shows "Try again" instead of loading the next page.

Version or commit

Instatic version: 0.0.20

Deployment mode

None

Logs or screenshots


Daniel-Dev123 avatar Oct 11 '26 22:10 Daniel-Dev123

I would be interested in picking this up. My initial thought is that the pagination request is carrying a composed page ID generated by the template chain, but the lookup logic is searching for the original node ID. I would start by tracing the page ID generation and resolution flow, then add a fallback that strips the composition prefix and retries the lookup against the original node ID. This seems aligned with the root cause described and should help restore the expected infinite scroll behaviour on template composed pages.

anonymousknight07 avatar Oct 12 '26 10:10 anonymousknight07