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

[NEXT-1120] draftMode().enable() does not set cookie if followed by redirect() #49237

Closed
1 task done
jornki opened this issue May 4, 2023 · 7 comments · Fixed by #49965
Closed
1 task done

[NEXT-1120] draftMode().enable() does not set cookie if followed by redirect() #49237

jornki opened this issue May 4, 2023 · 7 comments · Fixed by #49965
Assignees
Labels
area: app App directory (appDir: true) linear: next Confirmed issue that is tracked by the Next.js team.

Comments

@jornki
Copy link

jornki commented May 4, 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.19.0
      npm: 8.19.3
      Yarn: 1.22.19
      pnpm: N/A
    Relevant packages:
      next: 13.4.0
      eslint-config-next: 13.4.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)

Link to the code that reproduces this issue

https://github.com/jornki/next-draft-bug

To Reproduce

  1. Create the following Route Handler (/api/demo)
// app/api/demo/route.ts
import { draftMode } from "next/headers";
import { redirect } from "next/navigation";

export async function GET(req: Request) {
	draftMode().enable();
	return redirect("/");
}
  1. Call the the route.

Describe the Bug

When using redirect in combination with draftMode().enable();, the __prerender_bypass cookie is not set. However, if you return a Response instead of using a redirect the cookie is set.

Expected Behavior

The __prerender_bypass cookie should be set.

Which browser are you using? (if relevant)

Tested with latest Firefox and Edge

How are you deploying your application? (if relevant)

Tested locally (dev and start)

NEXT-1120

@jornki jornki added the bug Issue was opened via the bug report template. label May 4, 2023
@github-actions github-actions bot added the area: app App directory (appDir: true) label May 4, 2023
@andrew-mcmaster
Copy link

This was happening for me too, was able to fix with this for now.

return new Response(null, {
    status: 307,
    headers: {
        Location: path,
    },
})

@jornki
Copy link
Author

jornki commented May 5, 2023

Thank you for the alternative solution @andrew-mcmaster.

@hrithikroboto
Copy link

I was pulling my hair until I saw this.

@timneutkens timneutkens added the linear: next Confirmed issue that is tracked by the Next.js team. label May 9, 2023
@timneutkens timneutkens changed the title Setting «draftMode().enable()» does not set a cookie if followed by a redirect() [NEXT-1120] Setting «draftMode().enable()» does not set a cookie if followed by a redirect() May 9, 2023
@leo-cheron
Copy link

leo-cheron commented May 11, 2023

andrew-mcmaster solves __prerender_bypass cookie writing, but despite being correctly set, draftMode().isEnabled stays always false in any of my server components.

This was happening because my runtime was forced to edge.

export const runtime = 'edge'

@maximilianschmitt
Copy link

It seems that draftMode().disable() also doesn't work when followed by a redirect(). @andrew-mcmaster's workaround works here too! :)

@surjithctly
Copy link

I checked my 3rd party settings in chrome as given in the notes and wondered why it did not work in localhost. decided to upload on server and still had the same error.

Now, I see this issue, I can finally sleep :)

@styfle styfle added kind: bug and removed bug Issue was opened via the bug report template. labels May 17, 2023
@styfle styfle self-assigned this May 17, 2023
@styfle styfle changed the title [NEXT-1120] Setting «draftMode().enable()» does not set a cookie if followed by a redirect() [NEXT-1120] draftMode().enable() does not set cookie if followed by redirect() May 17, 2023
@kodiakhq kodiakhq bot closed this as completed in #49965 May 19, 2023
kodiakhq bot pushed a commit that referenced this issue May 19, 2023
- Fixes #49237 


fix NEXT-1120

Co-authored-by: Wyatt Johnson <633002+wyattjoh@users.noreply.github.com>
hydRAnger pushed a commit to hydRAnger/next.js that referenced this issue Jun 12, 2023
- Fixes vercel#49237 


fix NEXT-1120

Co-authored-by: Wyatt Johnson <633002+wyattjoh@users.noreply.github.com>
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: app App directory (appDir: true) linear: next Confirmed issue that is tracked by the Next.js team.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants