mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 15:36:58 +00:00
refactor
This commit is contained in:
parent
1ffd276ddc
commit
33a24a08af
@ -2,8 +2,5 @@ COMPANY_NAME="Vercel Inc."
|
||||
TWITTER_CREATOR="@vercel"
|
||||
TWITTER_SITE="https://nextjs.org/commerce"
|
||||
SITE_NAME="Next.js Commerce"
|
||||
# SHOPIFY_REVALIDATION_SECRET=""
|
||||
# SHOPIFY_STOREFRONT_ACCESS_TOKEN=""
|
||||
# SHOPIFY_STORE_DOMAIN="[your-shopify-store-subdomain].myshopify.com"
|
||||
|
||||
CMS_URL="http://localhost:3000"
|
||||
|
@ -1,6 +0,0 @@
|
||||
import { revalidate } from 'lib/shopify';
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
export async function POST(req: NextRequest): Promise<NextResponse> {
|
||||
return revalidate(req);
|
||||
}
|
@ -3,5 +3,6 @@ import OpengraphImage from 'components/opengraph-image';
|
||||
export const runtime = 'edge';
|
||||
|
||||
export default async function Image() {
|
||||
return await OpengraphImage();
|
||||
const title = process.env.SITE_NAME || 'Payload Store';
|
||||
return await OpengraphImage({ title });
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
import { TAGS } from 'lib/constants';
|
||||
import { AjaxError } from 'lib/shopify/ajax';
|
||||
import { Where, create, find, findByID, update } from 'lib/shopify/payload';
|
||||
import {
|
||||
@ -8,9 +7,6 @@ import {
|
||||
Option as PayloadOption,
|
||||
Product as PayloadProduct
|
||||
} from 'lib/shopify/payload-types';
|
||||
import { revalidateTag } from 'next/cache';
|
||||
import { headers } from 'next/headers';
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import {
|
||||
Cart,
|
||||
CartItem,
|
||||
@ -378,35 +374,3 @@ export async function getProducts({
|
||||
const products = await find<PayloadProduct>('products', { where });
|
||||
return products.docs.map(reshapeProduct);
|
||||
}
|
||||
|
||||
// This is called from `app/api/revalidate.ts` so providers can control revalidation logic.
|
||||
export async function revalidate(req: NextRequest): Promise<NextResponse> {
|
||||
// We always need to respond with a 200 status code to Shopify,
|
||||
// otherwise it will continue to retry the request.
|
||||
const collectionWebhooks = ['collections/create', 'collections/delete', 'collections/update'];
|
||||
const productWebhooks = ['products/create', 'products/delete', 'products/update'];
|
||||
const topic = headers().get('x-shopify-topic') || 'unknown';
|
||||
const secret = req.nextUrl.searchParams.get('secret');
|
||||
const isCollectionUpdate = collectionWebhooks.includes(topic);
|
||||
const isProductUpdate = productWebhooks.includes(topic);
|
||||
|
||||
if (!secret || secret !== process.env.SHOPIFY_REVALIDATION_SECRET) {
|
||||
console.error('Invalid revalidation secret.');
|
||||
return NextResponse.json({ status: 200 });
|
||||
}
|
||||
|
||||
if (!isCollectionUpdate && !isProductUpdate) {
|
||||
// We don't need to revalidate anything for any other topics.
|
||||
return NextResponse.json({ status: 200 });
|
||||
}
|
||||
|
||||
if (isCollectionUpdate) {
|
||||
revalidateTag(TAGS.collections);
|
||||
}
|
||||
|
||||
if (isProductUpdate) {
|
||||
revalidateTag(TAGS.products);
|
||||
}
|
||||
|
||||
return NextResponse.json({ status: 200, revalidated: true, now: Date.now() });
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user