Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting Cookies Via Server Action Reloads Entire App #50163

Open
1 task done
johnson-jesse opened this issue May 22, 2023 · 11 comments
Open
1 task done

Setting Cookies Via Server Action Reloads Entire App #50163

johnson-jesse opened this issue May 22, 2023 · 11 comments
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template.

Comments

@johnson-jesse
Copy link

johnson-jesse commented May 22, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.4.0: Mon Mar  6 20:59:28 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6000
    Binaries:
      Node: 16.14.0
      npm: 8.3.1
      Yarn: 1.22.19
      pnpm: 6.11.0
    Relevant packages:
      next: 13.4.4-canary.0
      eslint-config-next: 13.4.3
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.0.4

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

App directory (appDir: true)

Link to the code that reproduces this issue or a replay of the bug

https://github.com/johnson-jesse/next-server-action-cookie

To Reproduce

  1. Clone repo, install, run dev.
  2. Click the first button labeled with "Via Server Action With Cookies"
  3. Notice the whole app re-renders.
  4. Optionally turn on "Highlight updates when components render." from developer window to see updates from button clicks.

Describe the Bug

Setting cookies in a server action causes the entire app to reload. This appears as a render glitch most noticeable when the layout is using a custom font that also needs to load.

Screen.Recording.2023-05-22.at.10.04.59.AM.mov

Expected Behavior

Setting cookies in a server action does not cause the entire app to reload thus avoiding loading the font again.

Which browser are you using? (if relevant)

Chrome Version 112.0.5615.137 (Official Build) (x86_64)

How are you deploying your application? (if relevant)

N/A

@johnson-jesse johnson-jesse added the bug Issue was opened via the bug report template. label May 22, 2023
@github-actions github-actions bot added the area: app App directory (appDir: true) label May 22, 2023
@shuding
Copy link
Member

shuding commented May 22, 2023

This appears as a render glitch most noticeable when the layout is using a custom font that also needs to load.

That's dev only as we ditch all the cache of CSS resources when a server refresh is triggered, which should be fine. Are you seeing that on prod as well?

Also, if cookies are mutated inside an Action, it's expected to re-render the app because there could be data that depends on cookies.

@johnson-jesse
Copy link
Author

@shuding In prod, the glitch is only apparent when turning on highlighting. The prod example can be interacted with here.

Screen.Recording.2023-05-22.at.12.29.50.PM.mov

The visual glitch is gone here, but, do we really want to assume the entire app should reload every time "any" cookie is set or mutated? In some cases, a cookie should change frequently, with every click.

@J4v4Scr1pt

This comment has been minimized.

@GuillaumeHalb
Copy link

Same issue here.
Setting cookie triggers db queries, thus I would like to avoid those reloads.

@aaaltonen
Copy link

Almost year has passed since you posted this and I'm experiencing same issue.

@coderbahrom

This comment has been minimized.

@a-bugaj
Copy link

a-bugaj commented May 21, 2024

I'm experiencing same issue.. will this be resolved somehow? setting a cookie using api route does not sound good.

@zoolyka
Copy link

zoolyka commented Jun 11, 2024

I'm facing the same issue.

The server action triggers all database queries (used to server side render the page of the actual route) when a cookie is set or deleted, even in production. This seems totally useless, since the components are not even rendered on the server and not sent to the client.

Looking for an explanation why this happens, and potentionally how to avoid it wihout switching to an API route.

@macdigger
Copy link

I wonder to which degree using api route could be considered a good practice.

I mean if it is - so it will be, but on the other hand, I'm using server actions (useFormState basically) to process user login form, which I process with server action, set a cookie if login was a success, and return json response.

However, due to full reload, the return value never actually has a chance to reach its caller, because the whole app gets remounted (not even refreshed, but complete remount, including reset of all vars defined by useState). It feels like a really thermonuclear thing to me?…

@jsabol
Copy link

jsabol commented Jun 29, 2024

I'm experiencing this issue when running getSession inside of a server action, in combination with useFormState. I tried this with essentially a NOOP just executing getSession, and I am experiencing a full page refresh with all fonts/css reloaded, causing the message not displaying due to the refresh. Next.js is recommending this paradigm (useFormState) instead of API requests. If I strip out the getSession call, the page does not refresh and the message displays.

import { getSession } from "@auth0/nextjs-auth0";

export async function saveUsername(
  prevState: SetUsernameFormState,
  formData: FormData
): Promise<SetUsernameFormState> {
  const session = await getSession();
  return {
    message: "Nothing to do",
  };
}

@comtef
Copy link

comtef commented Oct 4, 2024

I'm facing the same issue, using auth0 to check for authenticated user on server actions.
It triggers a full reload of the page. I'd like to find a solution to avoid refactoring all the app to api routes...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests