Skip to content

Commit

Permalink
refactor: small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Jul 9, 2024
1 parent 08ae034 commit db7486e
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions packages/next/src/rsc/handlers/revalidateRouterHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@ import { VerifyTokenFetchStrategy } from '@headstartwp/core';
import { revalidatePath } from 'next/cache';
import { NextRequest } from 'next/server';
import { getHostAndConfigFromRequest } from './utils';
/**
* Returns the path to revalidate
*
* @param path The path being revalidated
* @param host The host for which the path is being revalidated
* @param locale The locale for which the path is being revalidated
* @param isMultisiteRequest Whether this is a multisite request
* @returns
*/
function getPathToRevalidate(
path: string,
host: string,
locale: string | null,
isMultisiteRequest: boolean,
) {
let pathToRevalidate = path;

if (isMultisiteRequest) {
if (locale) {
pathToRevalidate = `/_sites/${host}/${locale}/${path}`;
}
pathToRevalidate = `/_sites/${host}${path}`;
}

return pathToRevalidate;
}

/**
* The revalidateRouteHandler is responsible for handling revalidate requests in Route Requests.
Expand All @@ -13,11 +39,11 @@ import { getHostAndConfigFromRequest } from './utils';
* #### Usage
*
* ```ts
* // pages/api/revalidate.js
* import { revalidateHandler } from '@headstartwp/next';
* import { revalidateRouteHandler } from '@headstartwp/next/app';
* import type { NextRequest } from 'next/server';
*
* export default async function handler(req, res) {
* return revalidateHandler(req, res);
* export async function GET(request: NextRequest) {
* return revalidateRouteHandler(request);
* }
* ```
*
Expand All @@ -29,9 +55,9 @@ import { getHostAndConfigFromRequest } from './utils';
*/
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');

Expand All @@ -49,33 +75,28 @@ export async function revalidateRouteHandler(request: NextRequest) {
isMultisiteRequest,
} = getHostAndConfigFromRequest(request);

// call WordPress API to check token
try {
const verifyTokenStrategy = new VerifyTokenFetchStrategy(sourceUrl);
const {
result: { path, post_id },
} = await verifyTokenStrategy.get({
const { result } = 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;
const verifiedPath = result.path ?? '';
const verifiedPostId = result.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}`;
}
const pathToRevalidate = getPathToRevalidate(
verifiedPath,
host,
locale,
isMultisiteRequest,
);

revalidatePath(pathToRevalidate);

Expand Down

0 comments on commit db7486e

Please sign in to comment.