-
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.
add revalidate global functionality, remove all optional locales
- Loading branch information
Showing
6 changed files
with
114 additions
and
8 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,52 @@ | ||
// revalidate the page in the background, so the user doesn't have to wait | ||
// notice that the hook itself is not async and we are not awaiting `revalidate` | ||
|
||
import type { AfterChangeHook } from "payload/dist/globals/config/types"; | ||
|
||
// only revalidate existing docs that are published (not drafts) | ||
export const revalidateGlobal: AfterChangeHook = ({ doc, req, global }) => { | ||
const locale = req.locale; | ||
if (!locale) { | ||
req.payload.logger.error("locale not set, cannot revalidate"); | ||
return; | ||
} | ||
const revalidate = async (): Promise<void> => { | ||
const revalidationKey = process.env.PAYLOAD_REVALIDATION_KEY; | ||
if (!revalidationKey) { | ||
req.payload.logger.error( | ||
"PAYLOAD_REVALIDATION_KEY not set, cannot revalidate", | ||
); | ||
return; | ||
} | ||
try { | ||
const fetchUrl = `${ | ||
process.env.PUBLIC_FRONTEND_URL | ||
}/next_api/revalidate-global?${new URLSearchParams({ | ||
secret: encodeURIComponent(revalidationKey), | ||
global: encodeURIComponent(global.slug), | ||
locale: encodeURIComponent(locale), | ||
}).toString()}`; | ||
req.payload.logger.info( | ||
`sending revalidate request ${fetchUrl.replace(revalidationKey, "REDACTED")}`, | ||
); | ||
const res = await fetch(fetchUrl); | ||
if (res.ok) { | ||
const thing = await res.json(); | ||
req.payload.logger.info(`revalidate response ${JSON.stringify(thing)}`); | ||
req.payload.logger.info(`Revalidated global ${global.slug}`); | ||
} else { | ||
req.payload.logger.error( | ||
`Error revalidating collection ${global.slug}`, | ||
); | ||
} | ||
} catch (err: unknown) { | ||
req.payload.logger.error( | ||
`Error hitting revalidate collection ${global.slug}`, | ||
); | ||
} | ||
}; | ||
|
||
void revalidate(); | ||
|
||
return doc; | ||
}; |
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,38 @@ | ||
import { revalidateTag } from "next/cache"; | ||
import type { NextRequest } from "next/server"; | ||
import { NextResponse } from "next/server"; | ||
|
||
// this endpoint will revalidate a page by tag or path | ||
// this is to achieve on-demand revalidation of pages that use this data | ||
// send either `collection` and `slug` or `revalidatePath` as query params | ||
export function GET(request: NextRequest): NextResponse { | ||
const global = decodeURIComponent( | ||
request.nextUrl.searchParams.get("global") ?? "", | ||
); | ||
const locale = decodeURIComponent( | ||
request.nextUrl.searchParams.get("locale") ?? "", | ||
); | ||
const secret = decodeURIComponent( | ||
request.nextUrl.searchParams.get("secret") ?? "", | ||
); | ||
|
||
if (secret !== process.env.NEXT_REVALIDATION_KEY) { | ||
// eslint-disable-next-line no-console -- for debugging purposes | ||
console.log("invalid secret from revalidate request: ", secret); | ||
return NextResponse.json({ revalidated: false, now: Date.now() }); | ||
} | ||
|
||
if (typeof global === "string" && typeof locale === "string") { | ||
const tagToRevalidate = `getGlobal_/api/globals/${global}?locale=${locale}`; | ||
// eslint-disable-next-line no-console -- for debugging purposes | ||
console.log("revalidating tag: ", tagToRevalidate); | ||
revalidateTag(tagToRevalidate); | ||
return NextResponse.json({ revalidated: true, now: Date.now() }); | ||
} | ||
// eslint-disable-next-line no-console -- for debugging purposes | ||
console.log( | ||
"invalid collection or fetchData from revalidate request: ", | ||
global, | ||
); | ||
return NextResponse.json({ revalidated: false, now: Date.now() }); | ||
} |
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,6 +1,5 @@ | ||
import type { LandingPage } from "@tietokilta/cms-types/payload"; | ||
import { getGlobal } from "./fetcher"; | ||
|
||
export const fetchLandingPage = getGlobal<LandingPage>( | ||
"/api/globals/landing-page", | ||
); | ||
export const fetchLandingPage = (locale: string) => | ||
getGlobal<LandingPage>("/api/globals/landing-page", locale); |