-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The legacyPagesRouterSitePreviewApiHandler used parts from SitePreviewUtils which had a server-only restriction. However, the server-only package can only be used in RSC compatible setups (i.e. the App Router). To fix this, we move the App Router specific parts to a separate file, ensuring that SitePreviewUtils can be used for both Pages Router and App Router.
- Loading branch information
1 parent
e0dea4c
commit 46df5a6
Showing
5 changed files
with
48 additions
and
43 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
40 changes: 40 additions & 0 deletions
40
packages/site/cms-site/src/sitePreview/appRouter/sitePreviewRoute.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,40 @@ | ||
import "server-only"; | ||
|
||
import { cookies, draftMode } from "next/headers"; | ||
import { redirect } from "next/navigation"; | ||
import type { NextRequest } from "next/server"; | ||
|
||
import { SitePreviewParams, verifySitePreviewJwt } from "../SitePreviewUtils"; | ||
|
||
export async function sitePreviewRoute(request: NextRequest, _graphQLFetch: unknown /* deprecated: remove argument in v8 */) { | ||
const params = request.nextUrl.searchParams; | ||
const jwt = params.get("jwt"); | ||
if (!jwt) { | ||
throw new Error("Missing jwt parameter"); | ||
} | ||
|
||
const data = await verifySitePreviewJwt(jwt); | ||
|
||
cookies().set("__comet_preview", jwt); | ||
|
||
draftMode().enable(); | ||
|
||
return redirect(data.path); | ||
} | ||
|
||
/** | ||
* Helper for SitePreview | ||
* @param options.skipDraftModeCheck Allows skipping the draft mode check, only required when called from middleware.ts (see https://github.com/vercel/next.js/issues/52080) | ||
* @return If SitePreview is active the current preview settings | ||
*/ | ||
export async function previewParams(options: { skipDraftModeCheck: boolean } = { skipDraftModeCheck: false }): Promise<SitePreviewParams | null> { | ||
if (!options.skipDraftModeCheck) { | ||
if (!draftMode().isEnabled) return null; | ||
} | ||
|
||
const cookie = cookies().get("__comet_preview"); | ||
if (cookie) { | ||
return verifySitePreviewJwt(cookie.value); | ||
} | ||
return null; | ||
} |