Why are my API routes being triggered during the build process of my NextJS project?

My project includes an API route that fetches data from my DataBase. This particular API route is triggered by a CRON job set up in Vercel.

After each build of the project, new data gets added to the database. I suspect this might be due to NextJS pre-executing an endpoint to cache it. How can I prevent unwanted data additions in my Database?

You can find the code snippet here: https://github.com/guifeliper/dart-throwing-monkey-crypto/blob/main/apps/monkey-crypto/app/api/generateTokens/route.ts

export const revalidate = 60 * 60 * 24 * 6; // every 6 days
export async function GET(request: Request) {
  // Add your logic here...
}

Answer №1

Make sure to add

export const dynamic = 'force-dynamic';
right above your function declaration.

As far as I know, next.js has a mechanism in place to determine if it can server-side render certain functions, such as checking for headers or cookies. However, if these calls are not detected immediately after the function declaration, it can lead to issues. By using force-dynamic, you can bypass this check.

Read more about this issue here

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

displaying several gltf 3D models simultaneously on a map using mapbox gl and three js

Recently, I encountered an issue with adding glTF 3D models to a map using Mapbox GL and Three.js. It seems that while I can successfully add a single glTF model in a separate layer on the map, I am facing difficulties when trying to add multiple glTF mode ...

Retrieve both the key and value from an array of objects

I have been scouring the internet for a solution to this question, but I haven't come across anything that fits my needs. Let's say we have an array of objects like this -- "points":[{"pt":"Point-1","value":"Java, j2ee developer"},{"pt":"Point ...

Different methods to insert data into a database without relying on mongoose

Looking for help implementing the populate() function without using mongoose within the code snippet below: ` course.students.forEach(async (student, i) => { const s = await Student.findById(student._id); console.log(s.toObject()); // ...

Why is my element still occupying space even though it has been hidden with a display of none?

Currently, I'm working on a dropdown navigation menu where one of the list items causes space issues when the display property is set to none. Specifically, the 'Book A Table' item isn't meant to be visible in the center div when the sc ...

Discover the method for accessing the current URL or page being loaded during navigation in Next.js

Is it possible to implement per-page custom skeleton loaders during page navigation? In order to achieve this, I need to determine which page is currently being loaded. I attempted to use the useRouter approach as shown below: //_app.tsx useEffect(() => ...

Executing multiple functions in a specific order within an asynchronous grunt task using async

I am facing an issue with my grunt tasks that run asynchronously using this.async. I have some asynchronous functions in the code and for a few tasks, I need them to run in series. To achieve this, I am utilizing async.series from the async npm module. How ...

Tips for providing support to a website without an internet connection

I am in the process of creating a sales platform for a grocery store that utilizes PHP/MySQL. I have come across some websites that are able to fully reload and function even without internet access. For instance, when I initially visited abc.com, everyth ...

Tips for inserting the <a> HTML tag into a JSON array object

I am relatively new to creating array objects and I am unsure on how to add a link within a <a> tag in a JSON file. Here is the JSON object code: powerUpList: [ { id: 8, name: 'Automate your API column type ', description: & ...

Decreased storage space requirements following transfer to S3 bucket using nodejs

I am currently facing an issue with uploading files from a specific folder location to an S3 bucket using the nodejs aws-sdk. The files I am working with are deepzoom images (.dzi). While the files seem to be successfully uploaded to my S3 bucket, I have n ...

Incorporating JavaScript into a pre-existing HTML and CSS user interface

After successfully coding a chat app UI for my project site using CSS and HTML, I am now facing the challenge of adding functionality to my existing code. The issue is setting up the client server and integrating chatting functions into my current UI. Mo ...

Setting the default option in an Autocomplete component using Material UI in React JS

I am currently working with autocomplete input using react material ui component. One specific challenge I am facing is setting a default selected value when a user enters edit mode for this input field. Although I can select the option using getOptionSe ...

The connection between Socket.io and the environment variable in Next.js is not working as expected

I am facing an issue where my working environment variable setup does not allow socketio to connect. The .env.local file contains the following: HOST=http://localhost:8080 In the index.js file: console.log(process.env.HOST) const socket = io(process.e ...

CSS problem with padding and width

Currently, I'm working on a conditional rendering issue where I want to give a div full width but due to padding applied to its parent, it's not expanding to the container's full width. Here is the snippet of the code: import Image from &qu ...

What options are available for managing state in angularjs, similar to Redux?

Currently, I'm involved in an extensive project where we are developing a highly interactive Dashboard. This platform allows users to visualize and analyze various data sets through charts, tables, and more. In order to enhance user experience, we ha ...

React / Express / MySQL - Best Practices for Managing MySQL Transactions

I'm currently working on a project that involves developing a React front-end with an Express back-end API that utilizes MySql. One of the challenges I am facing is determining where to handle MySql transactions in my project structure. The folder l ...

Error message: When using the Tab Element with Next.js and Semantic UI, an error occurs stating that the element

Currently, I am attempting to integrate semantic-ui tabs into my Next.js application. An error message that I encounter reads: The element type is invalid: was expecting a string (for built-in components) or a class/function (for composite components ...

Unable to find the matching closing parenthesis ')" before reaching the end

I've been struggling to create a regular expression that can identify strings like the ones below: var p1=@VAL([Test1Q1].[Bandwidth]) var p2=@VAL([Test1Q1].[Usages (KB)]) The criteria is to find strings starting with @VAL( and ending before the firs ...

Utilize SWR to retrieve data that corresponds to the chosen value from the dropdown menu

Can SWR fetch data based on the selected value in a dropdown? Initially, it works, but when I select a previously selected value, it doesn't fetch the correct data. Using the Fetch API const { data: entities } = useSWR( currentEntity?.enti ...

Why does the error message "$(…).functionName() is not a function" occur and what steps can be taken to prevent it from

I have encountered a console error message: $(...).functionName() is not a function Here is my function call: $("button").functionName(); This is the actual function: $.fn.functionName = function() { //Do Something }(jQuery); What ca ...

Utilizing Next-Auth for Seamless Mobile Authentication in React Native

I am currently working on a project with a website that utilizes next-auth for authentication. I am interested in building a React Native mobile application and incorporating the same authentication system. Is it feasible to achieve this, and if so, what s ...