vercel / next.js

The React Framework
https://nextjs.org
MIT License
127k stars 26.99k forks source link

[NEXT-1191] [App Router] Inconsistent behaviour in dev and build when using force-dynamic and generateStaticParams #48153

Open cmngan opened 1 year ago

cmngan commented 1 year ago

Verify canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 22.4.0: Mon Mar  6 21:00:17 PST 2023; root:xnu-8796.101.5~3/RELEASE_X86_64
    Binaries:
      Node: 16.15.0
      npm: 9.2.0
      Yarn: 1.22.19
      pnpm: 8.0.0
    Relevant packages:
      next: 13.3.1-canary.3
      eslint-config-next: 13.3.0
      react: 18.2.0
      react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

App directory (appDir: true), Internationalization (i18n), Routing (next/router, next/navigation, next/link)

Link to the code that reproduces this issue

https://github.com/cmngan/nextjs-router-test

To Reproduce

This is a minimum reproducible example for testing nextJs router behavior.

Scenario 1: with force-dynamic and generateStaticParams

Dev: it will throw error

  1. Clone the repo
  2. Run npm install
  3. Run npm run dev
  4. Open http://localhost:3000/en-hk in your browser
  5. Click the button <= it will throw error, [Error: Dynamic server usage: force-dynamic]

Build: it works

  1. Clone the repo
  2. Run npm install
  3. Run npm run build
  4. Run npm run start
  5. Open http://localhost:3000/en-hk in your browser
  6. Click the button <= it works

Scenario 2: without force-dynamic and with generateStaticParams

Dev: it doesn't work, empty searchParams

  1. Similar to Scenarios1, but remove force-dynamic in /src/pages/[lang]/page.tsx
  2. Click the button <= searchParams in server log is empty

Build: it doesn't work, no log

  1. Similar to Scenario 1, but remove force-dynamic in /src/pages/[lang]/page.tsx
  2. Click the button <= no server log, I think it's because the page is static

Scenario 3: without force-dynamic and without generateStaticParams

It works

Describe the Bug

When using app router with force-dynamic and generateStaticParams, build and dev behaviours are different.

Although Scenario 3 is working, it means that we cannot static generated all pages except some pages. i.e. Scenario 1. To achieve it, we need to add generateStaticParams in all individual page.

Not sure if I miss something. It sounds related to https://github.com/vercel/next.js/issues/44712

Expected Behaviour

  1. dev and build should behaviour the same
  2. allow force-dynamic even with generateStaticParams, or any other recommended ways to handle the situation

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

NEXT-1191

Livog commented 1 year ago

I m using 13.4.1 and also still getting this issue.

norbertkovacs95 commented 1 year ago

Hi, is there any update on this issue? I am also using Internationalization with generateStaticParams in order to statically generate some of the routes for the supported languages. I also get the same error for a page I am trying to paginate. Does it mean that right now if generateStaticParams is used in a layout, its children layouts cannot be dynamically rendered?

lmo-wt commented 1 year ago

We're having the same issue on 13.4.3 : it works as intended with run start but fails on run dev. It is very confusing and cleary seems like a bug.

RobbieMcKinstry commented 1 year ago

I'm also experiencing this issue; this behavior is very bizarre, and the error message isn't helpful.

le-huy-jh commented 1 year ago

So many errors

reza-ebrahimi commented 1 year ago

Same for me with next 13.4.5, force-static is not working as expected.

umeshkukreti27 commented 1 year ago

same for me with 13.4.9-canary.2

rrcobb commented 1 year ago

Also seeing this in a layout. it's fairly annoying for any authenticated routes in the /app folder in dev...

Seems to work fine in build / production with export const dynamic = "force-dynamic"

loakliu commented 11 months ago

same for me with 13.4.9