| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- import { type DocumentNode, type OperationVariables } from "@apollo/client";
- import { graphqlRequest, type CacheLifeOption, GraphqlRequestResult } from "@/lib/graphql-fetch";
- import { GET_FILTER_ATTRIBUTES } from "@/graphql";
- export interface PageCacheConfig {
- tags: string[];
- life: CacheLifeOption;
- }
- /**
- * Cache configuration for different pages and queries
- * Centralized management of cache tags and revalidation times
- */
- export const PAGE_CACHE_CONFIG: Record<string, PageCacheConfig> = {
- // Home page
- home: {
- tags: ["home-page"],
- life: "hours",
- },
- // Product pages
- product: {
- tags: ["all-products"],
- life: "hours",
- },
- // Category/Collection pages
- category: {
- tags: ["categories"],
- life: "hours",
- },
- // Static content
- static: {
- tags: ["static-content"],
- life: "days",
- },
- // Search results
- search: {
- tags: ["search-results"],
- life: "hours",
- },
- };
- /**
- * Helper to get cache config for a specific page
- */
- export function getPageCacheConfig(
- page: keyof typeof PAGE_CACHE_CONFIG,
- ): PageCacheConfig {
- return PAGE_CACHE_CONFIG[page];
- }
- /**
- * Helper to create dynamic product cache config with specific product identifier
- */
- // export function getProductCacheConfig(productId: string): PageCacheConfig {
- // return {
- // tags: ["products", `product-${productId}`],
- // life: "hours",
- // };
- // }
- export function getProductCacheConfig(productId: string): {noCache: boolean} {
- return {noCache: true};
- }
- /**
- * Helper to create dynamic category cache config with specific category identifier
- */
- export function getCategoryCacheConfig(categoryId: string): PageCacheConfig {
- return {
- tags: ["categories", `category-${categoryId}`],
- life: "hours",
- };
- }
- /**
- * Wrapper hook for graphqlRequest with automatic cache management
- * Usage: const data = await cachedGraphQLRequest('home', query, variables);
- */
- export async function cachedGraphQLRequest<
- TData = unknown,
- TVariables extends OperationVariables = OperationVariables,
- >(
- page: keyof typeof PAGE_CACHE_CONFIG,
- query: DocumentNode,
- variables?: TVariables,
- ): Promise<GraphqlRequestResult<TData>> {
- const config = getPageCacheConfig(page);
- return graphqlRequest<TData, TVariables>(query, variables, config);
- }
- /**
- * Wrapper for product-specific queries with dynamic cache tags
- */
- export async function cachedProductRequest<
- TData = unknown,
- TVariables extends OperationVariables = OperationVariables,
- >(
- productId: string,
- query: DocumentNode,
- variables?: TVariables,
- ): Promise<GraphqlRequestResult<TData>> {
- const config = getProductCacheConfig(productId);
- return graphqlRequest<TData, TVariables>(query, variables, config);
- }
- /**
- * Wrapper for category-specific queries with dynamic cache tags
- */
- export async function cachedCategoryRequest<
- TData = unknown,
- TVariables extends OperationVariables = OperationVariables,
- >(
- categoryId: string,
- query: DocumentNode,
- variables?: TVariables,
- ): Promise<GraphqlRequestResult<TData>> {
- const config = getCategoryCacheConfig(categoryId);
- return graphqlRequest<TData, TVariables>(query, variables, config);
- }
- /**
- * Fetches filter attributes (color, size, brand) for product filtering
- *
- * @returns Promise with formatted filter attributes
- */
- export async function getFilterAttributes() {
- const {data: filterData } = await cachedGraphQLRequest<{
- color: any;
- size: any;
- brand: any;
- }>("static", GET_FILTER_ATTRIBUTES, { locale: "en" });
- const attributes = [filterData?.color, filterData?.size, filterData?.brand];
- return attributes.filter(Boolean).map((attr) => ({
- id: attr.id,
- code: attr.code,
- adminName: attr.code.toUpperCase(),
- options: attr.options.edges.map((o: any) => ({
- id: o.node.id,
- adminName: o.node.adminName,
- })),
- }));
- }
|