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

Draft Mode does not work in Middleware #67075

Closed
JKarlavige opened this issue Jun 20, 2024 · 3 comments · Fixed by #67779
Closed

Draft Mode does not work in Middleware #67075

JKarlavige opened this issue Jun 20, 2024 · 3 comments · Fixed by #67779
Labels
bug Issue was opened via the bug report template. locked Middleware Related to Next.js Middleware.

Comments

@JKarlavige
Copy link

Link to the code that reproduces this issue

https://github.com/JKarlavige/nextjs-draftmode-reproduction

To Reproduce

  1. Clone reproduction repository locally
  2. Install packages with npm i
  3. Run app with npm run dev
  4. In a browser, hit the following url: localhost:3000/api/draft?secret=secret-token&slug=preview-page

Current vs. Expected behavior

Note - Draft mode on the page itself works correctly. draftMode().isEnabled returns true.

Current behavior:
In middleware, draftMode().isEnabled returns false after hitting the draft endpoint in step 4 above.

Expected behavior:
draftMode().isEnabled should return true after the draft endpoint has been hit.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.5.0: Wed May  1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000
  Available memory (MB): 16384
  Available CPU cores: 10
Binaries:
  Node: 20.11.1
  npm: 10.2.4
Relevant Packages:
  next: 14.2.4 // Latest available version is detected (14.2.4).
  react: 18.3.1
  react-dom: 18.3.1

Which area(s) are affected? (Select all that apply)

Middleware

Which stage(s) are affected? (Select all that apply)

next dev (local), Vercel (Deployed)

Additional context

On next version 14.1.4 (which we currently use in production), we experience inconsistencies where draft mode in middleware sometimes returns true, and on the following middleware run it returns false.

On version 14.2.4, it appears that draft mode always returns false.
middleware-draftmode

@JKarlavige JKarlavige added the bug Issue was opened via the bug report template. label Jun 20, 2024
@github-actions github-actions bot added the Middleware Related to Next.js Middleware. label Jun 20, 2024
@coffeecupjapan
Copy link
Contributor

@JKarlavige
Hi. I am not quite understand implementation of draft mode, but it seems like isEnable is readonly and no chance to be updated when enable() or disable() is called.

this.isEnabled = Boolean(
!isOnDemandRevalidate &&
cookieValue &&
previewProps &&
cookieValue === previewProps.previewModeId
)

May be you can use request.cookies.has('__prerender_bypass') to test whether draft mode is enabled or not.

#52080 (comment)

@taylor-lindores-reeves
Copy link

Quick question on Draft Mode - does it require integration of a Headless CMS or can we use it without one?

Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. 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 Jul 30, 2024
lubieowoce pushed a commit that referenced this issue Aug 22, 2024
Chnage the way of preview props injection to edge runtime, directly read
from env vars of preview props instead of writing to and reading from
prerender manifest.js

Previously we're trying to make these preview props values become
deterministic that we can replace in edge deployment pipeline in #64521

But the way of serializing process env vars in edge runtime is not
correct. They'll remain as string "process.env.xxx" in the manifest and
also after consumed. This PR fixes that behavior, instead of writing it
into manifest, alwyas consuming from process.env.var directly.

I created a shared util to access the preview props of edge runtime
across all the templates.

On draft provider side, we still need to handle dev mode case when
preview id is `development-id`, but we already have the cookie, it
cannot be aligned with the preview id. So we do a fallback check for dev
mode if the cookie is present and preview id is `development-id` then we
still treat it as draft mode is enabled.

Fixes #52080
Fixes #67075

Closes NEXT-3541
lubieowoce pushed a commit that referenced this issue Sep 2, 2024
Chnage the way of preview props injection to edge runtime, directly read
from env vars of preview props instead of writing to and reading from
prerender manifest.js

Previously we're trying to make these preview props values become
deterministic that we can replace in edge deployment pipeline in #64521

But the way of serializing process env vars in edge runtime is not
correct. They'll remain as string "process.env.xxx" in the manifest and
also after consumed. This PR fixes that behavior, instead of writing it
into manifest, alwyas consuming from process.env.var directly.

I created a shared util to access the preview props of edge runtime
across all the templates.

On draft provider side, we still need to handle dev mode case when
preview id is `development-id`, but we already have the cookie, it
cannot be aligned with the preview id. So we do a fallback check for dev
mode if the cookie is present and preview id is `development-id` then we
still treat it as draft mode is enabled.

Fixes #52080
Fixes #67075

Closes NEXT-3541
lubieowoce pushed a commit that referenced this issue Sep 2, 2024
Chnage the way of preview props injection to edge runtime, directly read
from env vars of preview props instead of writing to and reading from
prerender manifest.js

Previously we're trying to make these preview props values become
deterministic that we can replace in edge deployment pipeline in #64521

But the way of serializing process env vars in edge runtime is not
correct. They'll remain as string "process.env.xxx" in the manifest and
also after consumed. This PR fixes that behavior, instead of writing it
into manifest, alwyas consuming from process.env.var directly.

I created a shared util to access the preview props of edge runtime
across all the templates.

On draft provider side, we still need to handle dev mode case when
preview id is `development-id`, but we already have the cookie, it
cannot be aligned with the preview id. So we do a fallback check for dev
mode if the cookie is present and preview id is `development-id` then we
still treat it as draft mode is enabled.

Fixes #52080
Fixes #67075

Closes NEXT-3541
lubieowoce pushed a commit that referenced this issue Sep 2, 2024
Chnage the way of preview props injection to edge runtime, directly read
from env vars of preview props instead of writing to and reading from
prerender manifest.js

Previously we're trying to make these preview props values become
deterministic that we can replace in edge deployment pipeline in #64521

But the way of serializing process env vars in edge runtime is not
correct. They'll remain as string "process.env.xxx" in the manifest and
also after consumed. This PR fixes that behavior, instead of writing it
into manifest, alwyas consuming from process.env.var directly.

I created a shared util to access the preview props of edge runtime
across all the templates.

On draft provider side, we still need to handle dev mode case when
preview id is `development-id`, but we already have the cookie, it
cannot be aligned with the preview id. So we do a fallback check for dev
mode if the cookie is present and preview id is `development-id` then we
still treat it as draft mode is enabled.

Fixes #52080
Fixes #67075

Closes NEXT-3541
lubieowoce pushed a commit that referenced this issue Sep 2, 2024
Chnage the way of preview props injection to edge runtime, directly read
from env vars of preview props instead of writing to and reading from
prerender manifest.js

Previously we're trying to make these preview props values become
deterministic that we can replace in edge deployment pipeline in #64521

But the way of serializing process env vars in edge runtime is not
correct. They'll remain as string "process.env.xxx" in the manifest and
also after consumed. This PR fixes that behavior, instead of writing it
into manifest, alwyas consuming from process.env.var directly.

I created a shared util to access the preview props of edge runtime
across all the templates.

On draft provider side, we still need to handle dev mode case when
preview id is `development-id`, but we already have the cookie, it
cannot be aligned with the preview id. So we do a fallback check for dev
mode if the cookie is present and preview id is `development-id` then we
still treat it as draft mode is enabled.

Fixes #52080
Fixes #67075

Closes NEXT-3541
lubieowoce pushed a commit that referenced this issue Sep 2, 2024
Chnage the way of preview props injection to edge runtime, directly read
from env vars of preview props instead of writing to and reading from
prerender manifest.js

Previously we're trying to make these preview props values become
deterministic that we can replace in edge deployment pipeline in #64521

But the way of serializing process env vars in edge runtime is not
correct. They'll remain as string "process.env.xxx" in the manifest and
also after consumed. This PR fixes that behavior, instead of writing it
into manifest, alwyas consuming from process.env.var directly.

I created a shared util to access the preview props of edge runtime
across all the templates.

On draft provider side, we still need to handle dev mode case when
preview id is `development-id`, but we already have the cookie, it
cannot be aligned with the preview id. So we do a fallback check for dev
mode if the cookie is present and preview id is `development-id` then we
still treat it as draft mode is enabled.

Fixes #52080
Fixes #67075

Closes NEXT-3541
lubieowoce pushed a commit that referenced this issue Sep 2, 2024
Chnage the way of preview props injection to edge runtime, directly read
from env vars of preview props instead of writing to and reading from
prerender manifest.js

Previously we're trying to make these preview props values become
deterministic that we can replace in edge deployment pipeline in #64521

But the way of serializing process env vars in edge runtime is not
correct. They'll remain as string "process.env.xxx" in the manifest and
also after consumed. This PR fixes that behavior, instead of writing it
into manifest, alwyas consuming from process.env.var directly.

I created a shared util to access the preview props of edge runtime
across all the templates.

On draft provider side, we still need to handle dev mode case when
preview id is `development-id`, but we already have the cookie, it
cannot be aligned with the preview id. So we do a fallback check for dev
mode if the cookie is present and preview id is `development-id` then we
still treat it as draft mode is enabled.

Fixes #52080
Fixes #67075

Closes NEXT-3541
lubieowoce pushed a commit that referenced this issue Sep 2, 2024
Chnage the way of preview props injection to edge runtime, directly read
from env vars of preview props instead of writing to and reading from
prerender manifest.js

Previously we're trying to make these preview props values become
deterministic that we can replace in edge deployment pipeline in #64521

But the way of serializing process env vars in edge runtime is not
correct. They'll remain as string "process.env.xxx" in the manifest and
also after consumed. This PR fixes that behavior, instead of writing it
into manifest, alwyas consuming from process.env.var directly.

I created a shared util to access the preview props of edge runtime
across all the templates.

On draft provider side, we still need to handle dev mode case when
preview id is `development-id`, but we already have the cookie, it
cannot be aligned with the preview id. So we do a fallback check for dev
mode if the cookie is present and preview id is `development-id` then we
still treat it as draft mode is enabled.

Fixes #52080
Fixes #67075

Closes NEXT-3541
lubieowoce pushed a commit that referenced this issue Sep 2, 2024
Chnage the way of preview props injection to edge runtime, directly read
from env vars of preview props instead of writing to and reading from
prerender manifest.js

Previously we're trying to make these preview props values become
deterministic that we can replace in edge deployment pipeline in #64521

But the way of serializing process env vars in edge runtime is not
correct. They'll remain as string "process.env.xxx" in the manifest and
also after consumed. This PR fixes that behavior, instead of writing it
into manifest, alwyas consuming from process.env.var directly.

I created a shared util to access the preview props of edge runtime
across all the templates.

On draft provider side, we still need to handle dev mode case when
preview id is `development-id`, but we already have the cookie, it
cannot be aligned with the preview id. So we do a fallback check for dev
mode if the cookie is present and preview id is `development-id` then we
still treat it as draft mode is enabled.

Fixes #52080
Fixes #67075

Closes NEXT-3541
lubieowoce pushed a commit that referenced this issue Sep 3, 2024
Chnage the way of preview props injection to edge runtime, directly read
from env vars of preview props instead of writing to and reading from
prerender manifest.js

Previously we're trying to make these preview props values become
deterministic that we can replace in edge deployment pipeline in #64521

But the way of serializing process env vars in edge runtime is not
correct. They'll remain as string "process.env.xxx" in the manifest and
also after consumed. This PR fixes that behavior, instead of writing it
into manifest, alwyas consuming from process.env.var directly.

I created a shared util to access the preview props of edge runtime
across all the templates.

On draft provider side, we still need to handle dev mode case when
preview id is `development-id`, but we already have the cookie, it
cannot be aligned with the preview id. So we do a fallback check for dev
mode if the cookie is present and preview id is `development-id` then we
still treat it as draft mode is enabled.

Fixes #52080
Fixes #67075

Closes NEXT-3541
lubieowoce pushed a commit that referenced this issue Sep 3, 2024
Chnage the way of preview props injection to edge runtime, directly read
from env vars of preview props instead of writing to and reading from
prerender manifest.js

Previously we're trying to make these preview props values become
deterministic that we can replace in edge deployment pipeline in #64521

But the way of serializing process env vars in edge runtime is not
correct. They'll remain as string "process.env.xxx" in the manifest and
also after consumed. This PR fixes that behavior, instead of writing it
into manifest, alwyas consuming from process.env.var directly.

I created a shared util to access the preview props of edge runtime
across all the templates.

On draft provider side, we still need to handle dev mode case when
preview id is `development-id`, but we already have the cookie, it
cannot be aligned with the preview id. So we do a fallback check for dev
mode if the cookie is present and preview id is `development-id` then we
still treat it as draft mode is enabled.

Fixes #52080
Fixes #67075

Closes NEXT-3541
lubieowoce pushed a commit that referenced this issue Sep 3, 2024
Chnage the way of preview props injection to edge runtime, directly read
from env vars of preview props instead of writing to and reading from
prerender manifest.js

Previously we're trying to make these preview props values become
deterministic that we can replace in edge deployment pipeline in #64521

But the way of serializing process env vars in edge runtime is not
correct. They'll remain as string "process.env.xxx" in the manifest and
also after consumed. This PR fixes that behavior, instead of writing it
into manifest, alwyas consuming from process.env.var directly.

I created a shared util to access the preview props of edge runtime
across all the templates.

On draft provider side, we still need to handle dev mode case when
preview id is `development-id`, but we already have the cookie, it
cannot be aligned with the preview id. So we do a fallback check for dev
mode if the cookie is present and preview id is `development-id` then we
still treat it as draft mode is enabled.

Fixes #52080
Fixes #67075

Closes NEXT-3541
lubieowoce pushed a commit that referenced this issue Sep 3, 2024
Chnage the way of preview props injection to edge runtime, directly read
from env vars of preview props instead of writing to and reading from
prerender manifest.js

Previously we're trying to make these preview props values become
deterministic that we can replace in edge deployment pipeline in #64521

But the way of serializing process env vars in edge runtime is not
correct. They'll remain as string "process.env.xxx" in the manifest and
also after consumed. This PR fixes that behavior, instead of writing it
into manifest, alwyas consuming from process.env.var directly.

I created a shared util to access the preview props of edge runtime
across all the templates.

On draft provider side, we still need to handle dev mode case when
preview id is `development-id`, but we already have the cookie, it
cannot be aligned with the preview id. So we do a fallback check for dev
mode if the cookie is present and preview id is `development-id` then we
still treat it as draft mode is enabled.

Fixes #52080
Fixes #67075

Closes NEXT-3541
lubieowoce pushed a commit that referenced this issue Sep 3, 2024
Chnage the way of preview props injection to edge runtime, directly read
from env vars of preview props instead of writing to and reading from
prerender manifest.js

Previously we're trying to make these preview props values become
deterministic that we can replace in edge deployment pipeline in #64521

But the way of serializing process env vars in edge runtime is not
correct. They'll remain as string "process.env.xxx" in the manifest and
also after consumed. This PR fixes that behavior, instead of writing it
into manifest, alwyas consuming from process.env.var directly.

I created a shared util to access the preview props of edge runtime
across all the templates.

On draft provider side, we still need to handle dev mode case when
preview id is `development-id`, but we already have the cookie, it
cannot be aligned with the preview id. So we do a fallback check for dev
mode if the cookie is present and preview id is `development-id` then we
still treat it as draft mode is enabled.

Fixes #52080
Fixes #67075

Closes NEXT-3541
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked Middleware Related to Next.js Middleware.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants