Encountering a roadblock while trying to implement apollo-client with NextJS getStaticPaths and getStaticProps (SSG) - facing an error during the build

I have successfully set up a dynamic route for a statically generated page component (I hope that's correct?) which functions perfectly in development mode. I am able to create pages in my headless CMS (KeystoneJS) and view them on my local environment.

However, when I run npm run build (for continuous integration), I encounter a peculiar error like the one below:

type: 'ApolloError',
  graphQLErrors: [],
  networkError: {
    message: 'request to http://localhost:3000/admin/api failed, reason: connect ECONNREFUSED 127.0.0.1:3000',
    type: 'system',
    errno: 'ECONNREFUSED',
    code: 'ECONNREFUSED'
  }

and also a series of these errors:

events.js:287
      throw er; // Unhandled 'error' event
      ^

Error: write EPIPE
    at process.target._send (internal/child_process.js:806:20)
    at process.target.send (internal/child_process.js:677:19)
    at callback (/Users/ibrahim/projects/guppy-tron/node_modules/worker-farm/lib/child/index.js:32:17)
    at module.exports (/Users/ibrahim/projects/guppy-tron/node_modules/terser-webpack-plugin/dist/worker.js:13:5)
    at handle (/Users/ibrahim/projects/guppy-tron/node_modules/worker-farm/lib/child/index.js:44:8)
    at process.<anonymous> (/Users/ibrahim/projects/guppy-tron/node_modules/worker-farm/lib/child/index.js:55:3)
    at process.emit (events.js:310:20)
    at emit (internal/child_process.js:876:12)
    at processTicksAndRejections (internal/process/task_queues.js:85:21)
Emitted 'error' event on process instance at:
    at internal/child_process.js:810:39
    at processTicksAndRejections (internal/process/task_queues.js:79:11) {
  errno: 'EPIPE',
  code: 'EPIPE',
  syscall: 'write'
}

This is how my page component is structured:

import React from 'react';
import { GetStaticPaths, GetStaticProps } from 'next';
import {
  GET_ALL_STATIC_PAGES,
  GET_STATIC_PAGES_BY_URL,
  IStaticPage,
} from '../../graphql/static-page-queries';
import client from '../../graphqlClient';
import BlocksContent from '../components/BlocksContent';

interface IPageProps {
  pageData: IStaticPage;
}

const Page = ({ pageData }: IPageProps) => {
  return (
    <div className="mh2 mh3-ns">
      <h1 className="f1">{pageData.title}</h1>
      <BlocksContent content={pageData.content} />
    </div>
  );
};

enum Status {
  published = 'published',
  draft = 'draft',
  archived = 'archived',
}

export const getStaticPaths: GetStaticPaths = async () => {
  const { data, errors } = await client.query({
    query: GET_ALL_STATIC_PAGES,
    variables: { status: Status.published },
  });

  if (errors) {
    console.log('error in query in getStaticPaths, ', errors);
  }

  const paths = data.allStaticPages.map((page) => {
    return { params: { url: page.url } };
  });

  return {
    paths,
    fallback: false,
  };
};


export const getStaticProps: GetStaticProps = async ({ params }) => {
  const { data, errors } = await client.query({
    query: GET_STATIC_PAGES_BY_URL,
    variables: { url: params.url },
  });

  if (errors) {
    console.log('error in query in getStaticProps, ', errors);
  }

  const { allStaticPages } = data;
  const pageData = allStaticPages[0];

  return {
    props: {
      pageData,
    },
  };
};

export default Page;

The ApolloClient configuration looks like this:

import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { createHttpLink } from 'apollo-link-http';

const isBrowser = typeof window !== 'undefined';
const uriHost = !isBrowser ? 'http://localhost:3000' : '';

const cache = new InMemoryCache();

const link = createHttpLink({
  uri: `${uriHost}/admin/api`,
  credentials: 'same-origin',
});

const client = new ApolloClient({
  ssrMode: typeof window === 'undefined',
  cache,
  link,
});

export default client;

Any suggestions or solutions?

Answer №1

When working in a CI environment, you may encounter the need to request a local resource such as localhost:3000. In order to access this resource in your CI setup, you will need to host your backend (Keystone) on a server that is accessible by your CI. I personally use Heroku for this purpose, but any server that allows external access will work.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

React Native - Implementing a dynamic form that adapts based on the answer given by its parent

My JavaScript Object has a simple state structure as follows: pertanyaan: [{ label: "label1", type: 'dropdown', key: 'keyFoo1', option: [{ value: "foo1" }, { value: "foo2", additional ...

The information is undefined, yet it is being recorded in the console

After retrieving data from the backend, it seems to be undefined when I try to use it. However, I can see the data logged in the console when using console.log. //I attempted to fetch data using axios but encountered a 404 error and received und ...

Implementing timeout in Next.js Serverless Functions with TRPC: A step-by-step guide

While navigating through the NextJS dashboard, I noticed calls being made to the serverless function /api/trpc/dailyTrends.getDailyTrends I am interested in setting a custom timeout for this function within the vercel.json file: { "functions": ...

Encountering a 404 error when trying to click on 'allow' after using Firebase Cloud Messaging, and attempting to add a service worker manually proves unsuccessful in resolving the issue

Currently, I am working on integrating FCM (Firebase Cloud Messaging) into a React app. Initially, I tested it on a simple project without React and everything worked smoothly. However, implementing it in the React app has presented some challenges for me. ...

The autocomplete feature fails to properly highlight the selected value from the dropdown menu and ends up selecting duplicate values

After working on creating a multiple select search dropdown using MUI, my API data was successfully transformed into the desired format named transformedSubLocationData. https://i.stack.imgur.com/ZrbQq.png 0: {label: 'Dialed Number 1', value: &a ...

Is there a way to extract the values from a range slider individually and then display them as the minimum and maximum values on the screen?

Currently, I am facing an issue with a range slider where the value I am retrieving is concatenated. For example, when printed, it appears as 2080, with 20 and 80 being separate values visually combined on screen. My goal is to extract the minimum and maxi ...

Struggling to set up the connection between React-Redux connect and the Provider store

Currently utilizing Redux with React Native for state management. I've set up the store and Provider successfully it seems, as I can utilize store.getState() and store.dispatch(action()) from any component without issue. However, I'm encountering ...

Firebase Operation Not Supported Error in next.js (auth/operation-not-supported-in-this-environment)

After successfully deploying a Next.js app hosted on Vercel with Firebase authentication that functions properly for users, I encountered a mysterious error during the "npm run build" phase: FirebaseError: Firebase: Error (auth/operation-not-supported-in ...

MaterialUI React components being stacked on top of each other

Currently working on a project with React and MaterialUI. Just about done with the setup, but I've run into an unexpected issue. The problem is that my simple navbar is overlapping the content beneath it. I set up a code sandbox to demonstrate the i ...

What is the best way to simulate cookies in NextJS server components?

I'm currently working on a test for a NextJS application that involves cookies. I am utilizing vitest and want to simulate the cookies in order to test if the conditional rendering is functioning correctly. import { cookies } from "next/headers&q ...

Arranging the objects within the flatList. The flatList component does not refresh after using setState()

I am currently facing an issue with a list of products that is displayed using a Flatlist component. The problem arises when I try to toggle the display of this list. When the toggle button is set to true, it should show the products sorted by price in as ...

Having trouble rendering the response text from the API fetched in Next.js on a webpage

I've written some code to back up a session border controller (SBC) and it seems to be working well based on the output from console.log. The issue I'm facing is that the response comes in as a text/ini file object and I'm unable to display ...

The props remain unchanged between prevProps and current props during the componentDidUpdate lifecycle method

One common issue I am facing is receiving the same props in my componentDidUpdate function, much like many other posts on this platform. Below is the snippet of code where this issue arises. Child componentDidUpdate(prevProps, prevState) { if (prevPro ...

What is the method for equalizing the height of the first block with the second block?

Is there a way to ensure the first grid item is the same height as the second grid item? <Grid container spacing={2}> <Grid item xs={6}> <div> {[...Array(20).keys()].map(item => ( <div style={styles.container}& ...

Display different screens depending on whether the user is authenticated in React Native

My goal is to dynamically render a React Native screen based on different conditions: 1) when the app is loading while checking authentication status, 2) if the user is already authenticated and show the home screen, or 3) if the user is not authenticated ...

What are the Steps to Deploy a React + Express Application on Heroku Platform?

My project folder structure looks like this: Client //Contains the react app Node_modules App.js //Where server code is located Package.json Package-lock.json I am looking to deploy it to Heroku. Within one of the files in the react app, I have: This is ...

distinguishing the container component from the presentational component within react native

I just started developing apps using React Native and implemented a basic login function with the view and login logic on the same page named HomeScreen.js. However, after reading some articles online, I learned that it's recommended to separate the l ...

justify-content property seems to be ineffective when used in Chakra UI

Currently in the process of developing a website using Next.js and Chakra-ui. I'm facing some issues with aligning the ButtonGroup to the end of the container. Despite trying to use justifyContent="space-between", it doesn't seem to be ...

Error TS2694 is being caused by Electron Typescript Material-UI withStyles because the namespace "".../node_modules/csstype/index"" does not have an exported member called 'FontFace'

While I am experienced with Material-UI, I am relatively new to Electron and using React, TypeScript, and Material-UI together. Recently, I encountered an error while attempting to create an electron boilerplate code for future project initialization. Init ...

Oops! You forgot to include the necessary getStaticPaths function for dynamic SSG pages on '/blogs/[post]'

Whenever I attempt to execute npm run build, an error occurs. The following build error occurred: Error: getStaticPaths is required for dynamic SSG pages and is missing for '/blogs/[post]'. This is the code snippet causing the issue: function ...