-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move preview route and update usages of nextjs-adapter methods #773
- Loading branch information
Showing
20 changed files
with
79 additions
and
459 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/image-types/global" /> | ||
/// <reference types="next/navigation-types/compat/navigation" /> | ||
|
||
// NOTE: This file should not be edited | ||
// see https://nextjs.org/docs/basic-features/typescript for more information. |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {NextRequest, NextResponse} from 'next/server'; | ||
import {redirect} from 'next/navigation'; | ||
import {draftMode} from 'next/headers'; | ||
|
||
export function HEAD(req: NextRequest) { | ||
return processRequest(req); | ||
} | ||
|
||
export function GET(req: NextRequest) { | ||
return processRequest(req); | ||
} | ||
|
||
function processRequest(req: NextRequest) { | ||
const params = req.nextUrl.searchParams; | ||
|
||
const token = params.get('token'); | ||
let response = validateToken(token); | ||
if (response !== null) { | ||
return response; | ||
} | ||
|
||
const path = params.get('path'); | ||
response = validatePath(path); | ||
if (response !== null) { | ||
return response; | ||
} | ||
|
||
console.info(`Previewing [${path}]...`); | ||
|
||
draftMode().enable(); | ||
|
||
redirect(!path?.length ? '/' : path); | ||
} | ||
|
||
function validateToken(token: string | null) { | ||
if (token !== process.env.ENONIC_API_TOKEN) { | ||
// XP hijacks 401 to show login page, so send 407 instead | ||
return NextResponse.json({message: 'Invalid token'}, { | ||
status: 407, | ||
}); | ||
} | ||
return null; | ||
} | ||
|
||
function validatePath(path: string | string[] | null) { | ||
// If the slug doesn't exist prevent preview mode from being enabled | ||
if (path === null || path === undefined) { | ||
return NextResponse.json({message: 'Invalid path'}, { | ||
status: 400, | ||
}); | ||
} | ||
return null; | ||
} |
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.