-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce unnecessary requests to drupal for 404 pages
- Loading branch information
Showing
7 changed files
with
127 additions
and
61 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,34 @@ | ||
// route handler with secret and slug | ||
import {draftMode} from 'next/headers' | ||
import {redirect} from 'next/navigation' | ||
import {NextRequest, NextResponse} from "next/server"; | ||
import {getResourceByPath} from "@lib/drupal/get-resource"; | ||
import {StanfordNode} from "@lib/types"; | ||
|
||
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 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}) | ||
} | ||
|
||
if (!slug) { | ||
return NextResponse.json({message: 'Invalid slug path'}, {status: 401}) | ||
} | ||
// Enable Draft Mode by setting the cookie | ||
draftMode().enable() | ||
|
||
// Fetch the headless CMS to check if the provided `slug` exists | ||
// getPostBySlug would implement the required fetching logic to the headless CMS | ||
const node = await getResourceByPath<StanfordNode>(slug); | ||
const node = await getResourceByPath<StanfordNode>(slug, {draftMode: true}) | ||
|
||
// If the slug doesn't exist prevent draft mode from being enabled | ||
// If the slug doesn't exist prevent disable draft mode and return | ||
if (!node) { | ||
return new Response('Invalid slug', {status: 401}) | ||
return NextResponse.json({message: 'Invalid slug'}, {status: 401}) | ||
} | ||
|
||
draftMode().enable() | ||
|
||
// Redirect to the path from the fetched post | ||
// We don't redirect to searchParams.slug as that might lead to open redirect vulnerabilities | ||
redirect(node.path.alias) | ||
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
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
7dcd46f
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.
Successfully deployed to the following URLs:
cardinal-sites-next – ./
cardinal-sites-next-pookmish.vercel.app
cardinal-sites-next.vercel.app
cardinal-sites-next-git-main-pookmish.vercel.app