-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor preview mode for drupal to use manual cookie instead of draf…
…t mode
- Loading branch information
Showing
30 changed files
with
229 additions
and
200 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,32 @@ | ||
// route handler with secret and slug | ||
import {draftMode} from 'next/headers' | ||
import {NextRequest, NextResponse} from "next/server"; | ||
import {redirect} from 'next/navigation' | ||
import {cookies} from "next/headers"; | ||
|
||
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') | ||
export const revalidate = 0; | ||
|
||
export async function GET(request: NextRequest) { | ||
|
||
const secret = request.nextUrl.searchParams.get('secret') | ||
const slug = request.nextUrl.searchParams.get('slug') | ||
|
||
// Check the secret and next parameters | ||
// This secret should only be known to this route handler and the CMS | ||
if (secret !== process.env.DRUPAL_PREVIEW_SECRET || !slug) { | ||
return new Response('Invalid token', {status: 401}) | ||
if (secret !== process.env.DRUPAL_PREVIEW_SECRET) { | ||
return NextResponse.json({message: 'Invalid token'}, {status: 401}) | ||
} | ||
// Enable Draft Mode by setting the cookie | ||
draftMode().enable() | ||
redirect(slug) | ||
|
||
if (!slug) { | ||
return NextResponse.json({message: 'Invalid slug path'}, {status: 401}) | ||
} | ||
cookies().set('preview', secret, { | ||
maxAge: 60 * 60, | ||
httpOnly: true, | ||
sameSite: 'none', | ||
secure: true, | ||
partitioned: true, | ||
}); | ||
|
||
// Redirect to the path from the fetched post | ||
// We don't redirect to searchParams.slug as that might lead to open redirect vulnerabilities | ||
redirect(`/preview${slug}`) | ||
} |
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,34 +1,20 @@ | ||
import InternalHeaderBanner from "@/components/patterns/internal-header-banner"; | ||
import Header from "@/components/layout/header"; | ||
import LibraryFooter from "@/components/layout/library-footer"; | ||
import GlobalFooter from "@/components/layout/global-footer"; | ||
|
||
const NotFound = () => { | ||
return ( | ||
<div className="grid grid-rows-1 min-h-screen"> | ||
<div> | ||
<Header/> | ||
<main id="main-content" className="mb-50"> | ||
<InternalHeaderBanner> | ||
<h1 | ||
className="w-full max-w-[calc(100vw-10rem)] md::max-w-[calc(100vw-20rem)] 3xl:max-w-[calc(1500px-20rem)] mx-auto relative text-white mt-80 md:mt-100 mb-50 p-0"> | ||
Page Not Found | ||
</h1> | ||
</InternalHeaderBanner> | ||
|
||
<main id="main-content" className="mb-50"> | ||
<InternalHeaderBanner> | ||
<h1 | ||
className="w-full max-w-[calc(100vw-10rem)] md::max-w-[calc(100vw-20rem)] 3xl:max-w-[calc(1500px-20rem)] mx-auto relative text-white mt-80 md:mt-100 mb-50 p-0"> | ||
Page Not Found | ||
</h1> | ||
</InternalHeaderBanner> | ||
|
||
<div className="centered mb-50"> | ||
Unable to find the content you are looking for. Please try the <a href="/all">search</a> to find what you | ||
were looking for. | ||
</div> | ||
</main> | ||
<div className="centered mb-50"> | ||
Unable to find the content you are looking for. Please try the <a href="/all">search</a> to find what you were | ||
looking for. | ||
</div> | ||
|
||
<footer className="row-start-2 row-end-3"> | ||
<LibraryFooter/> | ||
<GlobalFooter/> | ||
</footer> | ||
</div> | ||
</main> | ||
) | ||
} | ||
export default NotFound; |
File renamed without changes.
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 @@ | ||
import {notFound} from "next/navigation"; | ||
import {isPreviewMode} from "@/lib/drupal/is-draft-mode"; | ||
import NodePage from "../../[...slug]/page"; | ||
|
||
const PreviewNodePage = async ({params}: PageProps) => { | ||
if (!isPreviewMode()) notFound(); | ||
return <NodePage params={params}/> | ||
} | ||
|
||
type PageProps = { | ||
params: { slug: string | string[] } | ||
searchParams?: Record<string, string | string[] | undefined> | ||
} | ||
|
||
|
||
export default PreviewNodePage; |
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,10 @@ | ||
import {notFound} from "next/navigation"; | ||
import {isPreviewMode} from "@/lib/drupal/is-draft-mode"; | ||
import Page from "../../page"; | ||
|
||
const PreviewHomePage = async () => { | ||
if (!isPreviewMode()) notFound(); | ||
return <Page/> | ||
} | ||
|
||
export default PreviewHomePage |
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,22 @@ | ||
import {ReactNode} from "react"; | ||
import {isPreviewMode} from "@/lib/drupal/is-draft-mode"; | ||
import Editori11y from "@/components/editori11y"; | ||
import {ExclamationCircleIcon} from "@heroicons/react/20/solid"; | ||
|
||
const RootLayout = ({children}: { children: ReactNode }) => { | ||
const previewMode = isPreviewMode() | ||
return ( | ||
<> | ||
{previewMode && | ||
<> | ||
<div className="bg-illuminating py-10 text-3xl font-bold"> | ||
<div className="centered-container flex gap-10"><ExclamationCircleIcon width={20}/>Previewing Content</div> | ||
</div> | ||
<Editori11y/> | ||
</> | ||
} | ||
{children} | ||
</> | ||
) | ||
} | ||
export default RootLayout; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.