What is the best way to bring in ts and html using getStaticProps for a static website in Next.js?

I am trying to display static data sourced from a TypeScript file to the user. I understand that I need to use getStaticProps to ensure that the site is indeed static. However, I encountered an error indicating that it is not a JSON file. Do I really need to pass it through getStaticProps to make it static, or is it already considered static by default since it's just a javascript import with data? How can I resolve the Error message provided below?

Error:

Error: Error serializing `.projects[0].extra.$$typeof` returned from `getStaticProps` in "/index".
Reason: `symbol` cannot be serialized as JSON. Please only return JSON serializable data types.

projects.tsx

const projects: [
  {
    id: "6853939",
    name: "Project 01",
    title: "Title 01 ",
    previewImg: "/images/projectThumbnails/image01.jpg",
    extra: (<div>Extra 01</div>),

  },
  {
    id: "6853939",
    name: "Project 02",
    title: "Title 02 ",
    previewImg: "/images/projectThumbnails/image02.jpg",
    extra: (<div>Extra 02</div>),
  }
];

export default projects;

index.tsx

import projects from "../data/projects.tsx";

const IndexPage = ({projects}) => {
  return (
    <>
      <div>
        {projects.map((i) => (
          <div key={i.id}>{i.title}</div>
        ))}
      </div>
      <div>
        {names.names.map((i) => (
          <div key={i.name}>{i.name}</div>
        ))}
      </div>
    </>
  );
};

export const getStaticProps = async () => {
  return {
    props: { projects: projects },
  };
};

Answer №1

The data returned by the getStaticProps function must be able to be converted into a format that can be shared across different programming languages using JSON.stringify() and JSON.parse(). Non-serializable properties such as JavaScript functions, Symbols, JSX elements are not supported.

For a list of supported formats, visit https://www.json.org/json-en.html

In the example below, the value of the extra key in the projects array is a JSX element which is not serializable:

const projects: [
  {
    id: "6853939",
    name: "Project 01",
    title: "Title 01 ",
    previewImg: "/images/projectThumbnails/image01.jpg",
    extra: (<div>Extra 01</div>), // <= this is not serializable property

  },
  {
    id: "6853939",
    name: "Project 02",
    title: "Title 02 ",
    previewImg: "/images/projectThumbnails/image02.jpg",
    extra: (<div>Extra 02</div>), // <= this is not serializable property
  }
];

export default projects;

This results in the following error message:

Error: Error serializing `.projects[0].extra.$$typeof` returned from `getStaticProps` in "/index".
Reason: `symbol` cannot be serialized as JSON. Please only return JSON serializable data types.

To resolve this error, you can change the value of extra to a string instead. For example: extra: "Extra 02", then use this string to display the value inside a div in the component.

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

Ways to specifically locate the initial list items

Need help creating an array of the first li elements but unsure of how to proceed. Here is the HTML structure: <div class="nav"> <ul> <li>Element 1</li> <li>Element 2 <ul> <li>Element ...

Using Node.js and Multer to update a file uploaded to MongoDB

I have a website that allows users to upload files, but I also want them to be able to edit those files. This would involve the user pressing "edit" and replacing the existing file in the database with a new one. Normally, you can use findByIdAndUpdate for ...

JavaScript: Generating multiple variables using a for loop?

Is there a way to dynamically create n variables a_1, a_2, a_3 ... a_n, where the value of n is determined during runtime? Attempting to use the following code would not produce the desired outcome: var n = prompt("Enter number of variables?"); for (i= ...

Troubles with Azure Active Directory Authentication in Next-Auth

Currently, I am working on integrating Azure Active Directory for login functionality using Next-auth in our NextJS project. After implementing the code below, upon loading the application, it redirects to show a login button. When clicked, it directs to ...

Tips for successfully implementing an ng-click function in AngularJs on a button

I am looking to achieve the following: <form name="newAppointment" onsubmit="return validateForm()" method="post" action="newAppointment.php"> ... <button ng-click="setMstep(1)">1</button> <button ng-click="setMstep(5)">5</butto ...

Is it recommended to use django's render_to_string response with innerHTML?

This is my first Django project, so please bear with me if I'm making some basic mistakes. I'm currently working on a webpage that will display a report in an HTML table. However, I feel like the process I'm using to gather the data and cons ...

Design of Redux middleware with focus on return values

I just finished learning about redux middleware, and it seems really useful. However, I have a question regarding the return values of middleware. I understand that some middleware return values (such as redux-promise), while others like logging do not - ...

Increasing the CPU usage of Chrome can be achieved by optimizing certain settings within the browser

Recently, I created an AI using NeroEvolution that plays the game of snake in p5.js. To enhance the training process, I am attempting to increase the number of game cycles per frame, but Chrome seems to be struggling to keep up with the demand. Is there ...

Axios mistakenly includes an additional trailing slash in body parameters

Currently, in the process of developing an application utilizing React Native, I am faced with the challenge of communicating with an IoT chip that possesses minimal RAM memory. Due to this constraint, all logic must be executed on the client side. One sp ...

Creating Unique Layouts for Specific Routes in Next.js App Router

Issue with Layout Configuration I am facing a challenge in creating a unique layout for the /api/auth/* routes without including the components CustomNavbar and Footer from the main RootLayout. Despite my attempts, the main layout continues to be displaye ...

Adding items from top to bottom in Vue.js

Recently, I came across a code snippet that caught my attention: <comment-form @created="add"></comment-form> <div v-for="(comment, index) in items" :key="comment.id"> <comment :data="comment"></comment> </div> Wit ...

The references to the differential loading script in index.html vary between running ng serve versus ng build

After the upgrade to Angular 8, I encountered a problem where ng build was generating an index.html file that supported differential loading. However, when using ng serve, it produced a different index.html with references to only some 'es5' scri ...

What steps should I take to seamlessly integrate my API with my Next.js application?

As a newcomer to the world of development, it has only been about six weeks since I began learning. Currently, I find myself at a roadblock - I have successfully set up my Login API in Spring Boot, which functions properly with Postman, and I have also c ...

Save a SQL query as a text file using Node.js

I'm having an issue with my code. I am trying to save the results of a SQL query into a text file, but instead of getting the actual results, all I see in the file is the word "object." const fs = require('fs'); const sql = require('mss ...

An error has occurred in NodeJS: Value undefined is out of the acceptable range for an unspecified property in the options

I am currently leveraging worker_threads in nodejs to handle the task of reading multiple files and processing the rows for subsequent insertion into a database (using oracle-db). The volume of data I deal with is substantial, typically exceeding one mill ...

Hiding the TabBar in Expo React-Native when the keyboard is open

At the moment, the TabBarBottom briefly appears above the keyboard before moving back down. I am looking to have the TabBar completely hidden when the keyboard is open. Using expo sdk: 38 react-navigation": "^4.0.9", "react-navigation-tabs": " ...

Issue with getStaticProps functionality on Vercel seems to be persisting

UPDATE: I realize now that my mistake was calling my own API instead of querying the MongoDB database directly in getStaticProps(). I have made the necessary changes: export async function getStaticProps() { await dbConnect(); const user = await User.find ...

Top method for identifying genuine users and preventing bots

Utilizing a Maps API can be costly, especially with the fees per request To minimize requests, I heavily rely on caching techniques The API is invoked on every pageload, but unnecessary for non-human users like googlebot What would be the most effective ...

How does a web worker behave when the owner is not actively engaged?

[edit: I am using Chrome 49.0.2623.112 m (64-bit) with default settings and HTTPS, but the issue occurred before the switch.] My application utilizes web workers to handle AJAX polling. While this may not provide a significant performance boost, it does e ...

Discord.js Discord bot error: 'gateway' down

Hello, I could really use some assistance. Two days back, my discord bot began crashing unexpectedly. The error that keeps popping up is: events.js:367 throw err; // Unhandled 'error' event ^ Error [ERR_UNHANDLED_ERROR]: An uncau ...