feat: add revalidate fn for pages

Signed-off-by: Chloe <pinkcloudvnn@gmail.com>
This commit is contained in:
Chloe 2024-05-28 16:18:54 +07:00
parent 2c6bfcad65
commit b2be63187e
No known key found for this signature in database
GPG Key ID: CFD53CE570D42DF5
2 changed files with 12 additions and 4 deletions

View File

@ -28,7 +28,8 @@ export const PART_TYPES = [
export const TAGS = { export const TAGS = {
collections: 'collections', collections: 'collections',
products: 'products', products: 'products',
cart: 'cart' cart: 'cart',
pages: 'pages'
}; };
export const HIDDEN_PRODUCT_TAG = 'nextjs-frontend-hidden'; export const HIDDEN_PRODUCT_TAG = 'nextjs-frontend-hidden';

View File

@ -12,7 +12,7 @@ import {
} from 'lib/constants'; } from 'lib/constants';
import { isShopifyError } from 'lib/type-guards'; import { isShopifyError } from 'lib/type-guards';
import { ensureStartsWith, normalizeUrl, parseMetaFieldValue } from 'lib/utils'; import { ensureStartsWith, normalizeUrl, parseMetaFieldValue } from 'lib/utils';
import { revalidateTag } from 'next/cache'; import { revalidatePath, revalidateTag } from 'next/cache';
import { headers } from 'next/headers'; import { headers } from 'next/headers';
import { NextRequest, NextResponse } from 'next/server'; import { NextRequest, NextResponse } from 'next/server';
import { import {
@ -529,7 +529,8 @@ export async function getMetaobjectById(id: string) {
export async function getPage(handle: string): Promise<Page> { export async function getPage(handle: string): Promise<Page> {
const res = await shopifyFetch<ShopifyPageOperation>({ const res = await shopifyFetch<ShopifyPageOperation>({
query: getPageQuery, query: getPageQuery,
variables: { handle, key: 'page_content', namespace: 'custom' } variables: { handle, key: 'page_content', namespace: 'custom' },
tags: [TAGS.pages]
}); });
const page = res.body.data.pageByHandle; const page = res.body.data.pageByHandle;
@ -630,13 +631,14 @@ export async function revalidate(req: NextRequest): Promise<NextResponse> {
const secret = req.nextUrl.searchParams.get('secret'); const secret = req.nextUrl.searchParams.get('secret');
const isCollectionUpdate = collectionWebhooks.includes(topic); const isCollectionUpdate = collectionWebhooks.includes(topic);
const isProductUpdate = productWebhooks.includes(topic); const isProductUpdate = productWebhooks.includes(topic);
const isPageUpdate = topic.startsWith(TAGS.pages);
if (!secret || secret !== process.env.SHOPIFY_REVALIDATION_SECRET) { if (!secret || secret !== process.env.SHOPIFY_REVALIDATION_SECRET) {
console.error('Invalid revalidation secret.'); console.error('Invalid revalidation secret.');
return NextResponse.json({ status: 200 }); return NextResponse.json({ status: 200 });
} }
if (!isCollectionUpdate && !isProductUpdate) { if (!isCollectionUpdate && !isProductUpdate && !isPageUpdate) {
// We don't need to revalidate anything for any other topics. // We don't need to revalidate anything for any other topics.
return NextResponse.json({ status: 200 }); return NextResponse.json({ status: 200 });
} }
@ -649,6 +651,11 @@ export async function revalidate(req: NextRequest): Promise<NextResponse> {
revalidateTag(TAGS.products); revalidateTag(TAGS.products);
} }
if (isPageUpdate) {
const pageHandle = topic.split(':')[1];
pageHandle && revalidatePath(pageHandle);
}
return NextResponse.json({ status: 200, revalidated: true, now: Date.now() }); return NextResponse.json({ status: 200, revalidated: true, now: Date.now() });
} }