-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98bae20
commit 08ae034
Showing
8 changed files
with
161 additions
and
25 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@headstartwp/core": minor | ||
"@headstartwp/next": minor | ||
--- | ||
|
||
Introducing `revalidateRouteHandler` for handling revalidate requests in Route Handlers (App Router) |
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,92 @@ | ||
import { VerifyTokenFetchStrategy } from '@headstartwp/core'; | ||
import { revalidatePath } from 'next/cache'; | ||
import { NextRequest } from 'next/server'; | ||
import { getHostAndConfigFromRequest } from './utils'; | ||
|
||
/** | ||
* The revalidateRouteHandler is responsible for handling revalidate requests in Route Requests. | ||
* | ||
* Handling revalidate requires the Headless WordPress Plugin. | ||
* | ||
* **Important**: This function is meant to be used in a route handler route e.g: `/app/api/revalidate/route.ts`. | ||
* | ||
* #### Usage | ||
* | ||
* ```ts | ||
* // pages/api/revalidate.js | ||
* import { revalidateHandler } from '@headstartwp/next'; | ||
* | ||
* export default async function handler(req, res) { | ||
* return revalidateHandler(req, res); | ||
* } | ||
* ``` | ||
* | ||
* @param request The Next Request | ||
* | ||
* @returns A response object. | ||
* | ||
* @category Route handlers | ||
*/ | ||
export async function revalidateRouteHandler(request: NextRequest) { | ||
const { searchParams } = request.nextUrl; | ||
const post_id = Number(searchParams.get('post_id') ?? 0); | ||
const path = searchParams.get('path'); | ||
|
||
const token = searchParams.get('token'); | ||
const locale = searchParams.get('locale'); | ||
|
||
if (!path || !post_id || !token) { | ||
return new Response('Missing required params', { status: 401 }); | ||
} | ||
|
||
if (typeof path !== 'string' || typeof token !== 'string') { | ||
return new Response('Invalid params', { status: 401 }); | ||
} | ||
|
||
const { | ||
host, | ||
config: { sourceUrl }, | ||
isMultisiteRequest, | ||
} = getHostAndConfigFromRequest(request); | ||
|
||
// call WordPress API to check token | ||
try { | ||
const verifyTokenStrategy = new VerifyTokenFetchStrategy(sourceUrl); | ||
const { | ||
result: { path, post_id }, | ||
} = await verifyTokenStrategy.get({ | ||
authToken: token, | ||
// TODO: check if this is correct (it's a separate github issue) | ||
lang: typeof locale === 'string' ? locale : undefined, | ||
}); | ||
|
||
const verifiedPath = path ?? ''; | ||
const verifiedPostId = post_id ?? 0; | ||
|
||
// make sure the path and post_id matches with what was encoded in the token | ||
if (verifiedPath !== path || Number(verifiedPostId) !== Number(post_id)) { | ||
throw new Error('Token mismatch'); | ||
} | ||
|
||
let pathToRevalidate = path; | ||
|
||
if (isMultisiteRequest) { | ||
if (locale) { | ||
pathToRevalidate = `/_sites/${host}/${locale}/${path}`; | ||
} | ||
pathToRevalidate = `/_sites/${host}${path}`; | ||
} | ||
|
||
revalidatePath(pathToRevalidate); | ||
|
||
return new Response(JSON.stringify({ message: 'success', path: pathToRevalidate }), { | ||
status: 200, | ||
}); | ||
} catch (err) { | ||
let errorMessage = 'Error verifying the token'; | ||
if (err instanceof Error) { | ||
errorMessage = err.message; | ||
} | ||
return new Response(errorMessage, { status: 500 }); | ||
} | ||
} |
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,21 @@ | ||
import { getHeadstartWPConfig, getSiteByHost } from '@headstartwp/core'; | ||
import { NextRequest } from 'next/server'; | ||
|
||
/** | ||
* Gets the host and config from Next Request | ||
* | ||
* @param request The Next Request | ||
* @returns | ||
*/ | ||
export function getHostAndConfigFromRequest(request: NextRequest) { | ||
const { searchParams } = request.nextUrl; | ||
|
||
const locale = searchParams.get('locale'); | ||
|
||
const host = request.headers.get('host') ?? ''; | ||
const site = getSiteByHost(host, typeof locale === 'string' ? locale : undefined); | ||
const isMultisiteRequest = site !== null && typeof site.sourceUrl === 'string'; | ||
const config = isMultisiteRequest ? site : getHeadstartWPConfig(); | ||
|
||
return { host, config, isMultisiteRequest }; | ||
} |
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,7 @@ | ||
import { revalidateRouteHandler } from '@headstartwp/next/app'; | ||
import type { NextRequest } from 'next/server'; | ||
|
||
export async function GET(request: NextRequest) { | ||
// @ts-expect-error | ||
return revalidateRouteHandler(request); | ||
} |