NextJs creates serverless applications using static HTML files rather than lambdas

As I work on developing an application using Next.js serverless, I am facing an issue where instead of generating JS files in the designated folder (next/serverless/pages/), my pages are being transformed into static HTML files. This is evident during the build process as indicated by Next.js:

Compiled successfully. 

Page            Size     Files  Packages                                                                                
┌   /_app       2.04 kB    195         0                                                                                
├   /_document                                                                                                          
├   /_error     7.58 kB    230         0                                                                                
├ ⚡ /About      420 B      196         0                                                                                
└ ⚡ /Index      420 B      196         0                                                                                                                                                                                                        
λ  (Lambda)       page was emitted as a lambda (i.e. getInitialProps)                                                   
⚡  (Static File)  page was prerendered as static HTML  

This is how my next.config.js looks like:

module.exports = {
  target: "serverless",
  distDir: "../../dist/functions/next"
};

The output of the build with two pages (About and Index) is:

dist/functions/next/serverless/pages/
  _error.js
  404.html
  About.html
  Index.html

However, I specifically need them to be in JS file format since I will be utilizing them as modules for performing HTTP requests via firebase functions.

Answer №1

Beginning with Next.js 9, it implements Automatic Static Optimization as the default setting for static exports.

While static HTML .html is beneficial for performance, if there is a specific need to export files as .js, Next.js 9 also accommodates this through partial static export.

To generate javascript files instead of HTML files, simply include getInitialProps in the pages you intend to export as .js.

class Index extends Component {

  // Add this line to any of your `pages/any-file.js`
  static getInitialProps(ctx) {
    return {};
  }

  render() {
    return (
      <div>
        <h1>Index Page</h1>
      </div>
    );
  }
}

export default Index;

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

What is the reason behind SyntheticEvent not functioning properly in Next.js with React?

Currently, I am utilizing Next.js and encountered an issue with stopping my form using e.preventDefault(), which proved to be ineffective. After some troubleshooting, I discovered a workable solution by replacing e.preventDefault() with e.nativeEvent.preve ...

What is the syntax for accessing an element within an array in a function?

This code snippet retrieves an array of users stored in a Firestore database. Each document in the collection corresponds to a user and has a unique ID. const [user] = useAuthState(auth); const [userData, setUserData] = useState([]); const usersColl ...

After deploying my Next.js app to Vercel, I encountered a problem where the rss.xml file in the public folder was returning

I have successfully created an rss.xml file and wrote it using fs.writeFileSync. It works fine when viewed locally, but on vercel, the build fails with an error stating "no such file or directory ./public/rss.xml." If I comment out the fs.writeFileSync li ...

Is there a way to retrieve the API response from Antd's upload action once the upload has been completed?

Currently, I am utilizing Antd's Upload component for my image uploads to Cloudinary. Upon successful upload, the response provides details about the file, including the file URL on the server. Any suggestions on how I can retrieve this file URL from ...

What is the best way to showcase information retrieved using the getDocs function from Firebase Firestore?

Hello! I am currently exploring the world of Firestore and facing a challenge. My goal is to retrieve a data object from Firestore, set it as state, and then display it on my application: Below are the imports that I have utilized for this task (please ig ...

What is the best way to forward a file upload request from a Next.js API to another API?

Trying to crop an image within a Next.js application, then sending it through an API route within the app before reaching an external API endpoint is proving to be a challenge. The process works fine without going through the API route, but once that step ...

Is there a way to make a try-catch block pause and wait for a response before moving

I've been successfully retrieving data from my Firestore database, but I've encountered a major issue that I can't seem to resolve... Whenever I click the "Read Data" button, I have to press it twice in order to see the console log of the d ...

I have the ability to see HTTP-only cookies within my web browser

So, I always believed that httpOnly cookies could only be accessed during a http request. But the other day, while inspecting Firefox dev tools, I noticed that I could actually view the cookies' values. Is this standard behavior? ...

Rearranging components in CrafterCMS Studio

I'm currently learning how to integrate a headless CMS (CrafterCMS) with Next.js. Within CrafterCMS studio, I have created a simple content type consisting of a title and text field: Here is the code associated with it: export default async function ...

What could be causing React to retain the value in the form input within my modal?

Currently in my project, I am utilizing Next.js along with Tailwind CSS and Daisy UI. The functionality on the page includes fetching a JSON object from an API endpoint and displaying two tables: one for source systems and another for attached domains. Cl ...

Is there a way to align the line under the hyperlinks to match the text size using CSS?

<div className={styles.container}> <ul className={styles.links}> <li className={styles.link}> <a href="/">Home</a> </li> <li className={styles.link}> <a href="/portfolio& ...

Check if the user is null using React's useEffect hook and then perform a

I am currently working on implementing a protected route in Next JS. I have included a useEffect to redirect to the sign-in page if there is no user detected. const analytics = () => { const { userLoaded, user, signOut, session, userDetails, subscri ...

I'm having trouble connecting to port 3000 on my Docker container from the browser

I have dockerized my application using Laravel, Nginx, and MySQL. Below are the docker-compose file and Dockerfiles used: docker-compose.yaml version: '3.8' services: # Application app: container_name: app build: context: . ...

Pre-fetch page data in Next.js before rendering it

I need help optimizing my heavy web page with numerous images. How can I implement a loader to preload the page data before displaying it? Currently, I am working with Next.js. One possible solution I came up with is: const AboutPage = () => { const ...

Does NextJs have a feature or function similar to the createRef() hook found in traditional ReactJs?

Trying to utilize a 3rd party package called ReactPhotoSphereViewer in NextJs to showcase panoramic images on the website. The package functions well in both NextJs and ReactJs. Below is the code that functions in ReactJs: import { ReactPhotoSphereViewer ...

The issue persists with re-rendering the component despite implementing shallow in Zustand. Additionally, my custom comparison function does not seem to resolve

i am currently working with Next.js 13 and TypeScript here is the store setup I am using: export const useModeStore = create<modeState>()( devtools( ((set) => ({ // defining mode state mode: "time", ...

The issue of experiencing an infinite loop when updating state on the Next.js router query change

Allow me to provide some background: I'm currently working on updating data on the server using React Query's useMutation (It's somewhat similar to setting a local state). However, the mutation I want to perform should depend on whether a ...

An issue arises when attempting to fetch data using next.js: a TypeError occurs indicating that

I'm currently working on setting up a next.js website integrated with strapi, but I've encountered an issue with my fetch request. Oddly enough, when I test the request using Postman, the data is successfully returned, indicating that the endpoin ...

Top method for setting a cookie in the App router server component using NextJS

I am faced with a task that involves updating a flag in the database and creating a cookie with a 5-minute expiry time whenever a page is loaded. Furthermore, upon subsequent page loads, I need to check if the cookie has expired, update the flag, and gener ...

The Next.js middleware, specifically NextRequest.nextUrl.locale, will return an empty string once it is deployed

Encountering a bug in the next-js middleware The middleware function is returning a NextRequest param According to the documentation from Next.js: The NextRequest object is an extension of the native Request interface, with the following added metho ...