-
Notifications
You must be signed in to change notification settings - Fork 27.4k
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
Fix edge preview props are not matched with cookie #67779
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3aac3c8
Fix edge preview props are not matched with cookie
huozhi 9d28482
pass conditionally
huozhi bead147
fix edge runtime bypass id and typo in tests
huozhi 229e249
move to e2e
huozhi c8039fc
fix dev mode
huozhi e296664
fallback to dev compare
huozhi 06110c7
refactor and extract util
huozhi 319108a
remove comments
huozhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* In edge runtime, these props directly accessed from environment variables. | ||
* - local: env vars will be injected through edge-runtime as runtime env vars | ||
* - deployment: env vars will be replaced by edge build pipeline | ||
*/ | ||
export function getEdgePreviewProps() { | ||
return { | ||
previewModeId: | ||
process.env.NODE_ENV === 'production' | ||
? process.env.__NEXT_PREVIEW_MODE_ID! | ||
: 'development-id', | ||
previewModeSigningKey: process.env.__NEXT_PREVIEW_MODE_SIGNING_KEY || '', | ||
previewModeEncryptionKey: | ||
process.env.__NEXT_PREVIEW_MODE_ENCRYPTION_KEY || '', | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
test/e2e/app-dir/draft-mode-middleware/app/api/disable-draft/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { draftMode } from 'next/headers' | ||
|
||
export async function GET(request: Request) { | ||
draftMode().disable() | ||
return new Response('Draft mode is disabled') | ||
} |
23 changes: 23 additions & 0 deletions
23
test/e2e/app-dir/draft-mode-middleware/app/api/draft/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// route handler with secret and slug | ||
import { draftMode } from 'next/headers' | ||
import { redirect } from 'next/navigation' | ||
|
||
// Preview URL: localhost:3000/api/draft?secret=secret-token&slug=preview-page | ||
|
||
export async function GET(request: Request) { | ||
// Parse query string parameters | ||
const { searchParams } = new URL(request.url) | ||
const secret = searchParams.get('secret') | ||
const slug = searchParams.get('slug') | ||
|
||
// Check the secret and next parameters | ||
if (secret !== 'secret-token' || !slug) { | ||
return new Response('Invalid token', { status: 401 }) | ||
} | ||
|
||
// Enable Draft Mode by setting the cookie | ||
draftMode().enable() | ||
|
||
// Redirect to the path | ||
redirect(`/${slug}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const metadata = { | ||
title: 'Next.js', | ||
description: 'Generated by Next.js', | ||
} | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}) { | ||
return ( | ||
<html lang="en"> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
6 changes: 6 additions & 0 deletions
6
test/e2e/app-dir/draft-mode-middleware/app/preview-page/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { draftMode } from 'next/headers' | ||
|
||
export default function PreviewPage() { | ||
const { isEnabled } = draftMode() | ||
return <h1>{isEnabled ? 'draft' : 'none'}</h1> | ||
} |
42 changes: 42 additions & 0 deletions
42
test/e2e/app-dir/draft-mode-middleware/draft-mode-middleware.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
import { retry } from 'next-test-utils' | ||
|
||
describe('app-dir - draft-mode-middleware', () => { | ||
const { next, skipped } = nextTestSetup({ | ||
files: __dirname, | ||
skipDeployment: true, | ||
}) | ||
|
||
if (skipped) { | ||
return | ||
} | ||
|
||
it('should be able to enable draft mode with middleware present', async () => { | ||
const browser = await next.browser( | ||
'/api/draft?secret=secret-token&slug=preview-page' | ||
) | ||
|
||
await retry(async () => { | ||
expect(next.cliOutput).toContain( | ||
'draftMode().isEnabled from middleware: true' | ||
) | ||
}) | ||
|
||
await browser.loadPage(new URL('/preview-page', next.url).toString()) | ||
const draftText = await browser.elementByCss('h1').text() | ||
expect(draftText).toBe('draft') | ||
}) | ||
|
||
it('should be able to disable draft mode with middleware present', async () => { | ||
const browser = await next.browser('/api/disable-draft') | ||
await retry(async () => { | ||
expect(next.cliOutput).toContain( | ||
'draftMode().isEnabled from middleware: false' | ||
) | ||
}) | ||
|
||
await browser.loadPage(new URL('/preview-page', next.url).toString()) | ||
const draftText = await browser.elementByCss('h1').text() | ||
expect(draftText).toBe('none') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { NextResponse, type NextRequest } from 'next/server' | ||
import { draftMode } from 'next/headers' | ||
|
||
export function middleware(req: NextRequest) { | ||
const { isEnabled } = draftMode() | ||
console.log('draftMode().isEnabled from middleware:', isEnabled) | ||
return NextResponse.next() | ||
} | ||
|
||
export const config = { | ||
matcher: ['/((?!_next/static|_next/image|img|assets|ui|favicon.ico).*)'], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These values were the main reason this manifest was added iirc so should we just fully remove it?
Could be in a separate PR just noting for cleanup purposes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I'll get a follow up PR to remove it from edge runtime