pageContext array is accessible locally but not work on built

I don’t know why I can read this pageContext array content only locally but not on build

I can access to the entire array but I can’t access to the single element inside id:

2:09:56 PM: error Cannot read properties of undefined (reading '0')
2:09:56 PM:   TypeError: Cannot read properties of undefined (reading '0')

indexTemplate.js

import React, {useState} from 'react'
import Layout from "../../components/Layout"


const IndexPage = ({data, pageContext}) => {
  const sliderRef = useRef();
  const {slideshow} = pageContext;
    return (
      <Layout >
          {typeof slideshow} 
/*
output:

Array(3)
   0: {duration: 4}
   1: {duration: 3}
   2: {duration: 2}
length: 3
[[Prototype]]: Array(0)
*/
         {typeof slideshow[0].duration}
/*
on NETLIFY build:
TypeError: Cannot read properties of undefined (reading '0')
*/

      </Layout>
    )
  }
export default IndexPage;

p.s. locally it works

Hi there, glad you found us :wave: before we can help you, we need a little more information on the issues you are facing.

First, have you looked through this resource? It is a compilation of all of our build and deploy resources. This is a good starting point.

If you have worked through those guides and are still encountering issues please share the following information:

  • the name of your build instance: “example-jones.netlify.com
  • your full deploy log
  • any error messages you have received in your terminal window or in the developer console

Thanks!

1 Like

The problem was that I use Gatsby for this project and that file was in /pages directory.

src  
└───pages
│   │   index.md
│   │   index.js 

pages directory work in development mode in build mode works only if page files are in /template or different folder:

src  
└───pages
│   │   index.md
└───templates
    │   index.js