page-cache icon indicating copy to clipboard operation
page-cache copied to clipboard

How to cache paginated urls?

Open hmzisb opened this issue 3 years ago • 3 comments

Paginated pages return the same content as first page. As there any way around it?

hmzisb avatar Jan 26 '22 20:01 hmzisb

@hmzisb the issue is that Laravel uses a query string for pagination. I did see a solution for caching views that have query string parameters, but I don't particularly like this because a bot could generate a huge number of cached files by just changing the query string slightly each time.

The solution I used was to implement pretty pagination - that is where the page number forms part of the route, rather than using a query string. I used this package: https://github.com/michaloravec/laravel-paginateroute

So your URLs become /products/widgets/2 rather than /products/widgets?page=2

1stwebdesigns avatar Jan 26 '22 21:01 1stwebdesigns

@1stwebdesigns Thanks for the help. I saw this package but it seems like it hasn't been maintained in the last three years. Does it still work with the latest version of laravel?

hmzisb avatar Jan 26 '22 21:01 hmzisb

@hmzisb Yes it works fine - although it was created 3 years ago I have seen the author reference it on the Laravel forum more recently. In my case it was a choice between disabling the caching of paginated pages, implementing a work-around to cache pages with query strings, or implement pretty pagination. The latter seemed the lesser of 3 evils. Not to mention I think pretty pagination looks nicer, so there is a benefit there.

1stwebdesigns avatar Jan 26 '22 22:01 1stwebdesigns