Random Sort Order For Large E-commerce Inventory

I am developing an e-commerce site, and have am getting comfortable with extending the Strapi system. I would like to randomize the products returned but am not sure what approach to take.

I employ an infinite scroll pagination, which is working nicely - thank you to the admin that recommended I go REST vs GraphQL - however, with infinite scroll I am only fetching 12-24 products per request.

I experimented with the sort parameter - using multiple criteria. This didn’t really randomize things enough and led to duplicate product side effect (not sure if it was a strapi issue or something with the infinite scroll).

This leads me to think I need to do something with the controller, but how can I randomize the full set of results if I am only fetching a small portion at a time?

Another thought was to assign a new data field to each product that would probably just be a random, unique integer, and serve as the sort order.


System Information
  • 3.4:
  • Win10:
  • postgres:

To get random entries directly from DB you should write some custom or raw queries directly to ORM.
You can search in google for: get random rows for bookshelf OR Postgres OR knex.

To avoid duplication you should store all returned IDs in the frontend then exclude them from the request to DB. But this will cause performance issues since the more data you scroll the more IDs you will have to exclude from the next request.

1 Like