putyourlightson / craft-sprig

A reactive Twig component framework for Craft CMS.
https://putyourlightson.com/plugins/sprig
MIT License
129 stars 9 forks source link

Pagination URL segments instead of query parameters #316

Closed aloco closed 1 year ago

aloco commented 1 year ago

Hi,

we often get the requirement to deeplink paginated views. This is already possible, thanks to s-push-url however, when talking with SEO experts, we always hear "don´t use query parameters, use URL´s" or "paginated views need canonical URLs which includes the current page"

So instead of having https://example.dev/news?page=1 we would like to achieve something like https://example.dev/news/1 We are using SEOmatic most of the time, ideally the canonical URL is also https://example.dev/news/1 and not https://example.dev/news. While this can be achieved by overwriting the canonical URL manually, I don´t see a clean way to read the URL segments as variables in sprig/twig

I recognized, that in htmx you can already do hx-push-url="/1" -> https://htmx.org/attributes/hx-push-url/ is this also possible with sprig?

Thank you!

bencroker commented 1 year ago

Sprig uses htmx and any s- attributes map directly to their hx- equivalents (that exist), so s-push-url will work exactly the same as hx-push-url. Does that answer your question?

aloco commented 1 year ago

Hi Ben,

thank you for the fast reply. This part is clear, however how can I read those segments on initial page load within twig? So when going directly to https://example.dev/news/1 and having no s-vals used on button clicks, how can I use /1 from the url? Do I have to use craft.app.request.segments and read them manually or is there any build in sprig magic for that?

bencroker commented 1 year ago

Yes, you have all of Twig at your fingertips, so I’d suggest using this approach, for example.

{{ sprig('_my-component', { page: craft.app.request.segment(2) ?? 1 }) }}
aloco commented 1 year ago

Hi Ben, ok thank you! We will give it a shot