useCache.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { type DocumentNode, type OperationVariables } from "@apollo/client";
  2. import { graphqlRequest, type CacheLifeOption } from "@/lib/graphql-fetch";
  3. import {GraphqlRequestResult} from "@/types/graphqlFetch/type";
  4. import { GET_FILTER_ATTRIBUTES } from "@/graphql";
  5. export interface PageCacheConfig {
  6. tags: string[];
  7. life: CacheLifeOption;
  8. }
  9. /**
  10. * Cache configuration for different pages and queries
  11. * Centralized management of cache tags and revalidation times
  12. */
  13. export const PAGE_CACHE_CONFIG: Record<string, PageCacheConfig> = {
  14. // Home page
  15. home: {
  16. tags: ["home-page"],
  17. life: "hours",
  18. },
  19. // Product pages
  20. product: {
  21. tags: ["all-products"],
  22. life: "hours",
  23. },
  24. // Category/Collection pages
  25. category: {
  26. tags: ["categories"],
  27. life: "hours",
  28. },
  29. // Static content
  30. static: {
  31. tags: ["static-content"],
  32. life: "days",
  33. },
  34. // Search results
  35. search: {
  36. tags: ["search-results"],
  37. life: "hours",
  38. },
  39. };
  40. /**
  41. * Helper to get cache config for a specific page
  42. */
  43. export function getPageCacheConfig(
  44. page: keyof typeof PAGE_CACHE_CONFIG,
  45. ): PageCacheConfig {
  46. return PAGE_CACHE_CONFIG[page];
  47. }
  48. /**
  49. * Helper to create dynamic product cache config with specific product identifier
  50. */
  51. // export function getProductCacheConfig(productId: string): PageCacheConfig {
  52. // return {
  53. // tags: ["products", `product-${productId}`],
  54. // life: "hours",
  55. // };
  56. // }
  57. export function getProductCacheConfig(): {noCache: boolean} {
  58. return {noCache: true};
  59. }
  60. /**
  61. * Helper to create dynamic category cache config with specific category identifier
  62. */
  63. export function getCategoryCacheConfig(categoryId: string): PageCacheConfig {
  64. return {
  65. tags: ["categories", `category-${categoryId}`],
  66. life: "hours",
  67. };
  68. }
  69. /**
  70. * Wrapper hook for graphqlRequest with automatic cache management
  71. * Usage: const data = await cachedGraphQLRequest('home', query, variables);
  72. */
  73. export async function cachedGraphQLRequest<
  74. TData = unknown,
  75. TVariables extends OperationVariables = OperationVariables,
  76. >(
  77. page: keyof typeof PAGE_CACHE_CONFIG,
  78. query: DocumentNode,
  79. variables?: TVariables,
  80. ): Promise<GraphqlRequestResult<TData>> {
  81. const config = getPageCacheConfig(page);
  82. return graphqlRequest<TData, TVariables>(query, variables, config);
  83. }
  84. /**
  85. * Wrapper for product-specific queries with dynamic cache tags
  86. */
  87. export async function cachedProductRequest<
  88. TData = unknown,
  89. TVariables extends OperationVariables = OperationVariables,
  90. >(
  91. // productId: string,
  92. query: DocumentNode,
  93. variables?: TVariables,
  94. ): Promise<GraphqlRequestResult<TData>> {
  95. const config = getProductCacheConfig();
  96. return graphqlRequest<TData, TVariables>(query, variables, config);
  97. }
  98. /**
  99. * Wrapper for category-specific queries with dynamic cache tags
  100. */
  101. export async function cachedCategoryRequest<
  102. TData = unknown,
  103. TVariables extends OperationVariables = OperationVariables,
  104. >(
  105. categoryId: string,
  106. query: DocumentNode,
  107. variables?: TVariables,
  108. ): Promise<GraphqlRequestResult<TData>> {
  109. const config = getCategoryCacheConfig(categoryId);
  110. return graphqlRequest<TData, TVariables>(query, variables, config);
  111. }
  112. /**
  113. * Fetches filter attributes (color, size, brand) for product filtering
  114. *
  115. * @returns Promise with formatted filter attributes
  116. */
  117. export async function getFilterAttributes() {
  118. const {data: filterData } = await cachedGraphQLRequest<{
  119. color: any;
  120. size: any;
  121. brand: any;
  122. }>("static", GET_FILTER_ATTRIBUTES, { locale: "en" });
  123. const attributes = [filterData?.color, filterData?.size, filterData?.brand];
  124. return attributes.filter(Boolean).map((attr) => ({
  125. id: attr.id,
  126. code: attr.code,
  127. adminName: attr.code.toUpperCase(),
  128. options: attr.options.edges.map((o: any) => ({
  129. id: o.node.id,
  130. adminName: o.node.adminName,
  131. })),
  132. }));
  133. }