Randomise sort of array in component #34080
Unanswered
NickBarreto
asked this question in
Help
Replies: 1 comment 4 replies
-
I don't really understand your need, but you should use predictable random numbers to perform your shuffle ? |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Similar to this proposed feature, it would be useful for me to be able to, only one time, on build, shuffle the order of an array of items passed into a component. The component displays images from gatsby-plugin-image, which are links to detail pages in specific items.
Both for performance reasons (I don't want to delay rendering this component until the shuffle has happened – it's the first thing on the page, above the fold), as well as user expectations, I don't want the placement to change on page reload or revisit (I want the user to find the items in the same place they were before on each page visit).
That said, we will likely be rebuilding our site at least once a week, maybe more often, to account for changes in the source API. And it would be nice if each time the order of the elements on this component changed. This post is a great example on how to get the component to shuffle each time on page load, but the delayed drawing is (rightfully) affecting my lighthouse scores, and as I said it's maybe not the best UI to change this if the user decides to go back and pick another item.
For the time being, I've made the choice that the shuffle isn't worth the trouble.
But I can't help but think, if I create a random order that evaluates on build, during static site generation, the result of which is passed into the component, and no longer changes, I can have the performance of no shuffle, but also change the order around once a week (or more often) when we run a scheduled build for updates or new content from changes in my source API.
But I'm not sure how I'd go about it. Currently this component in in my homepage, which is just in
src/pages/index.js
, and has it's own page query. I guess those sorts of operations are perfect for placing in mygatsby-node
file.Would I need to change my strategy of how this page is generated, instead using createPage explicitly for it and passing the random order in as context, as suggested in the feature proposal? Could I go about it another way?
Beta Was this translation helpful? Give feedback.
All reactions