I have this React component on my gatsby site:
import React from 'react'
import Layout from '../components/layout'
import SEO from '../components/seo'
import { graphql } from "gatsby"
import Card from '../components/Card'
import Gallery from '../components/Gallery'
import shuffle from 'lodash/shuffle'
const IndexPage = ({data}) => {
function linkify(string){
return string.toLowerCase().replace(/["'()]/g,"").replace(/\s/g, '-');
}
return(
<Layout>
<SEO />
<Gallery>
{
shuffle(data.allContentfulAlbum.nodes).map((album) => {
return (
<Card src={album.cover.file.url} title={album.title} link={'/album/'+linkify(album.title)} />
)
})}
</Gallery>
</Layout>
)
}
export default IndexPage
export const query = graphql`
query Albums {
allContentfulAlbum {
nodes {
cover {
file {
url
}
}
title
}
}
}
`
Info:
- This is the index page of a photo gallery.
- Card is a component that receives and shows the cover photo, the album name in top of the photo and the whole Card is a link to the album page.
- You can visit this page at https://nicovial.tk so you get the idea, card shuffle is disabled for now.
- lodash is a JS library with many array processing functions available at npm.
Expected behavior:
- Card order shuffled on every page refresh or revisit.
Actual behavior:
- Cover photos and links are NOT shuffled, they're shown always in the original order.
- However, album titles are shuffled, so cards are shown with wrong names (except for coincidences on shuffle) but with right links, and in original order always.
The strange detail:
This only happens on production builds (either local build&serve or online github/netlify builds). When running 'gatsby develop' the behavior is the expected one.