karthi
karthi

Reputation: 51

API route with nextjs 13 after build is not working

When I built nextjs 12 app and deployed in IIS. everything is working fine, but I built with Nextjs 13(using app router). After deployed in IIS, API route is not working. The logs for both nextjs 12 and nextjs13 after running command npm run build.

This is Nextjs12

    Route (pages)                              Size     First Load JS
┌ ○ /                                      5.72 kB        84.7 kB
├   └ css/ae0e3e027412e072.css             707 B
├   /_app                                  0 B              79 kB
├ ○ /404                                   212 B          79.2 kB
└ λ /api/hello                             0 B              79 kB
+ First Load JS shared by all              79.2 kB
  ├ chunks/framework-0f397528c01bc177.js   45.7 kB
  ├ chunks/main-5cebf592faf0463a.js        31.8 kB
  ├ chunks/pages/_app-05f53d08443c56f6.js  402 B
  ├ chunks/webpack-7a6cea4e6a92562f.js     1.08 kB
  └ css/ab44ce7add5c3d11.css               247 B

λ  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)
○  (Static)  automatically rendered as static HTML (uses no initial props)

This is Nextjs13

 Route (app)                                Size     First Load JS
    ┌ ○ /                                      4.76 kB        81.7 kB
    ├ ○ /api/hello                             0 B                0 B
    └ ○ /favicon.ico                           0 B                0 B
    + First Load JS shared by all              76.9 kB
      ├ chunks/139-bbae89bf9cd2e4a9.js         24.5 kB
      ├ chunks/2443530c-d4a6bd7fc7f21e3c.js    50.5 kB
      ├ chunks/main-app-513f118d98a60ecc.js    212 B
      └ chunks/webpack-1102598922600a22.js     1.64 kB
    
    Route (pages)                              Size     First Load JS
    ─ ○ /404                                   178 B          85.9 kB
    + First Load JS shared by all              85.8 kB
      ├ chunks/main-5706b45374408a68.js        83.9 kB
      ├ chunks/pages/_app-c544d6df833bfd4a.js  192 B
      └ chunks/webpack-1102598922600a22.js     1.64 kB
    
    ○  (Static)  automatically rendered as static HTML (uses no initial props)

Why is it showing api/hello as 0b in nextjs 13. Do I need to add extra code for nextjs13, or API route in nextjs is not stable? Can someone help me with this?

Upvotes: 2

Views: 1893

Answers (2)

yoyoyojoe
yoyoyojoe

Reputation: 133

Can't say if this is anything specific to IIS, but I'll share my 2 cents and hope it might be relatable to your issiue as I ran into something similar lately. Next.js 13 uses server components by default..

Fetching the data on your own site, e.g. at /api/hello route, or any other routes, is not a good idea, because, in this case your /api/ routes will not be available during build because it is not running. The Next.js/patterns doc also reminds us that you should fetch directly to your desired API to get the data that you want because you will be doing this on the server.

Even though the build summary states that it is a SSR route, I'd recommend double checking the route handler's behavior because it could be tricky as a GET route handler could be cached in some cases.

λ  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)

Upvotes: 0

Michelangelo Pucci
Michelangelo Pucci

Reputation: 499

During the build phase, Next.js optimizes sources, and sometimes it may incorrectly mark API routes as static files. To work around this issue, you can add a dummy check in your API route that cannot be resolved statically by Next.js. This helps ensure that the route is treated as dynamic.

Here's an example of how you can implement this workaround:

export async function GET(request: Request) {
    if (request.url.length < 0)
        return new Response("Error"); // Unreachable

    // Do stuff
}

Upvotes: 0

Related Questions