next-runtime icon indicating copy to clipboard operation
next-runtime copied to clipboard

[Bug]: app-dir, API routes with `dynamic` = `error` are still rendered at runtime

Open Fox32 opened this issue 11 months ago • 0 comments

Summary

I'm using the new app-dir router. I want to use dynamic = error for my API route that have access to data and build time, but not later during execution.

A link to a reproduction repository

https://github.com/Fox32/nextjs-dynamic

Expected Result

I would expect that the state of the route created during build time is served, or otherwise an error is triggered. In no situation it should try to reevaluated the page.

Actual Result

I see that a .next/server/app/[filename].body and .next/server/app/[filename].meta file is generated during build that contain the right state, but these aren't served during runtime. Instead the route is reevaluated, which fails as my dependecies are not available at runtime.

Steps to reproduce

I have two deployments:

  • Vercel
    • https://nextjs-dynamic-pearl.vercel.app/test-page
    • https://nextjs-dynamic-pearl.vercel.app/test-api
  • Netlify
    • Page https://lucent-stroopwafel-beb979.netlify.app/test-page
    • API Route https://lucent-stroopwafel-beb979.netlify.app/test-api

For Vercel, both routes return newly the same date/time, but more important, the value stays the same. For Netlify, the page seems to be cached (or even using the static value from build time) and the API is reevaluates every time.

As a workaround, I added this code the the beginning of my route, but I don't think this scales very well if I have a lot of routes 😉

  if (process.env.AWS_REGION) {
    const body = await readFile('./.next/server/app/[filename].body', 'utf-8');
    const metadata = JSON.parse(
      await readFile('./.next/server/app/[filename].meta', 'utf-8')
    );
    return new Response(body, metadata);
  }

Next Runtime version

4.39.4

Is your issue related to the app directory?

  • [X] Yes, I am using the app directory

More information about your build

  • [ ] I am building using the CLI
  • [ ] I am building using file-based configuration (netlify.toml)

What OS are you using?

None

Your netlify.toml file

`netlify.toml`
# Paste content of your `netlify.toml` file here

Your public/_redirects file

`_redirects`
# Paste content of your `_redirects` file here

Your next.config.js file

`next.config.js`
# Paste content of your `next.config.js` file here. Check there is no private info in there.

Builds logs (or link to your logs)

Build logs
# Paste logs here

Function logs

Function logs
# Paste logs here

.next JSON files

generated .next JSON files
# Paste file contents here. Please check there isn't any private info in them
# You can either build locally, or download the deploy from Netlify by clicking the arrow next to the deploy time.

Fox32 avatar Aug 13 '23 11:08 Fox32