diff --git a/lib/constants.ts b/lib/constants.ts index 2ec301cfd..33fd75c8c 100644 --- a/lib/constants.ts +++ b/lib/constants.ts @@ -28,7 +28,8 @@ export const PART_TYPES = [ export const TAGS = { collections: 'collections', products: 'products', - cart: 'cart' + cart: 'cart', + pages: 'pages' }; export const HIDDEN_PRODUCT_TAG = 'nextjs-frontend-hidden'; diff --git a/lib/shopify/index.ts b/lib/shopify/index.ts index 6b5968082..3f12c8d65 100644 --- a/lib/shopify/index.ts +++ b/lib/shopify/index.ts @@ -12,7 +12,7 @@ import { } from 'lib/constants'; import { isShopifyError } from 'lib/type-guards'; import { ensureStartsWith, normalizeUrl, parseMetaFieldValue } from 'lib/utils'; -import { revalidateTag } from 'next/cache'; +import { revalidatePath, revalidateTag } from 'next/cache'; import { headers } from 'next/headers'; import { NextRequest, NextResponse } from 'next/server'; import { @@ -529,7 +529,8 @@ export async function getMetaobjectById(id: string) { export async function getPage(handle: string): Promise { const res = await shopifyFetch({ 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; @@ -630,13 +631,14 @@ export async function revalidate(req: NextRequest): Promise { const secret = req.nextUrl.searchParams.get('secret'); const isCollectionUpdate = collectionWebhooks.includes(topic); const isProductUpdate = productWebhooks.includes(topic); + const isPageUpdate = topic.startsWith(TAGS.pages); if (!secret || secret !== process.env.SHOPIFY_REVALIDATION_SECRET) { console.error('Invalid revalidation secret.'); return NextResponse.json({ status: 200 }); } - if (!isCollectionUpdate && !isProductUpdate) { + if (!isCollectionUpdate && !isProductUpdate && !isPageUpdate) { // We don't need to revalidate anything for any other topics. return NextResponse.json({ status: 200 }); } @@ -649,6 +651,11 @@ export async function revalidate(req: NextRequest): Promise { revalidateTag(TAGS.products); } + if (isPageUpdate) { + const pageHandle = topic.split(':')[1]; + pageHandle && revalidatePath(pageHandle); + } + return NextResponse.json({ status: 200, revalidated: true, now: Date.now() }); }