Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
feat: Add product model caching
Browse files Browse the repository at this point in the history
  • Loading branch information
dhmacs committed Jan 27, 2023
1 parent 77c39e8 commit f13399d
Show file tree
Hide file tree
Showing 29 changed files with 65 additions and 38 deletions.
7 changes: 5 additions & 2 deletions frontend/.env.production
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

NEXT_PUBLIC_ALGOLIA_APP_ID=XQEP3AD9ZT
# Overridden in prod
NEXT_PUBLIC_IFIXIT_ORIGIN=https://www.cominor.com # IFIXIT_ORIGIN should point to cominor in dev (e.g. use for iFixit PHP API)
NEXT_PUBLIC_APP_ORIGIN=https://${VERCEL_URL} # APP_ORIGIN should point to localhost:3000 in dev (e.g. use for Next.js API)

# IFIXIT_ORIGIN should point to cominor in dev (e.g. use for iFixit PHP API)
NEXT_PUBLIC_IFIXIT_ORIGIN=https://www.cominor.com
# APP_ORIGIN should point to localhost:3000 in dev (e.g. use for Next.js API)
NEXT_PUBLIC_APP_ORIGIN=https://${VERCEL_URL}

# Overridden in prod
NEXT_PUBLIC_STRAPI_ORIGIN=https://$NEXT_PUBLIC_VERCEL_GIT_COMMIT_REF.govinor.com
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/common/CompatibleDevice.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Flex, Img, Text } from '@chakra-ui/react';
import type { Product } from '@models/product/server';
import type { Product } from '@pages/api/nextjs/cache/product';
import * as React from 'react';

export type CompatibleDeviceProps = {
Expand Down
9 changes: 5 additions & 4 deletions frontend/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ const moduleExports = {
});
return config;
},
...(!SENTRY_AUTH_TOKEN && {
sentry: {
sentry: {
...(!SENTRY_AUTH_TOKEN && {
disableServerWebpackPlugin: true,
disableClientWebpackPlugin: true,
},
}),
}),
autoInstrumentServerFunctions: false,
},
};

// Make sure adding Sentry options is the last code to run before exporting, to
Expand Down
33 changes: 28 additions & 5 deletions frontend/pages/api/nextjs/cache/product.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import type { NextApiHandler } from 'next';
import { Duration } from '@lib/duration';
import { withCache } from '@lib/swr-cache';
import { ProductSchema } from '@models/product/schema';
import { findProduct } from '@models/product/server';
import { z } from 'zod';

const handler: NextApiHandler = async (req, res) => {
res.status(200).json({ model: 'product' });
};
export type {
Product,
ProductImage,
ProductVariant,
ProductVariantImage,
} from '@models/product/schema';

export default handler;
export default withCache({
endpoint: 'api/nextjs/cache/product',
variablesSchema: z.object({
handle: z.string(),
storeCode: z.string(),
}),
valueSchema: ProductSchema.nullable(),
async getFreshValue({ handle, storeCode }) {
return findProduct({
handle,
storeCode,
});
},
ttl: Duration(1).minute,
staleWhileRevalidate: Duration(1).day,
log: true,
});
2 changes: 1 addition & 1 deletion frontend/templates/product/MetaTags.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useAppContext } from '@ifixit/app';
import { parseItemcode } from '@ifixit/helpers';
import type { Product, ProductVariant } from '@models/product/server';
import type { Product, ProductVariant } from '@pages/api/nextjs/cache/product';
import Head from 'next/head';
import React from 'react';
import { jsonLdScriptProps } from 'react-schemaorg';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useIFixitApiClient } from '@ifixit/ifixit-api-client';
import { useExpiringLocalPreference } from '@ifixit/ui';
import { getBuyBoxForProduct } from '@lib/ifixit-api/international-buy-box';
import type { Product } from '@models/product/server';
import type { Product } from '@pages/api/nextjs/cache/product';
import { useQuery } from '@tanstack/react-query';
import { useSelectedVariant } from './useSelectedVariant';

Expand Down
2 changes: 1 addition & 1 deletion frontend/templates/product/hooks/useIsProductForSale.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useAuthenticatedUser } from '@ifixit/auth-sdk';
import type { Product } from '@models/product/server';
import type { Product } from '@pages/api/nextjs/cache/product';

export function useIsProductForSale(product: Product): boolean {
const user = useAuthenticatedUser();
Expand Down
2 changes: 1 addition & 1 deletion frontend/templates/product/hooks/useProductReviews.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useIFixitApiClient } from '@ifixit/ifixit-api-client';
import { fetchProductReviews } from '@models/product/reviews';
import type { Product } from '@models/product/server';
import type { Product } from '@pages/api/nextjs/cache/product';
import { useQuery } from '@tanstack/react-query';

const productReviewsKeys = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { WithProvidersProps } from '@components/common';
import type { WithLayoutProps } from '@layouts/default/server';
import { useServerSideProps } from '@lib/server-side-props';
import type { Product } from '@models/product/server';
import type { Product } from '@pages/api/nextjs/cache/product';

export type ProductTemplateProps = WithProvidersProps<
WithLayoutProps<{
Expand Down
2 changes: 1 addition & 1 deletion frontend/templates/product/hooks/useSelectedVariant.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { invariant } from '@ifixit/helpers';
import type { Product, ProductVariant } from '@models/product/server';
import type { Product, ProductVariant } from '@pages/api/nextjs/cache/product';
import { useRouter } from 'next/router';
import { useCallback } from 'react';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box, chakra, Heading, SimpleGrid } from '@chakra-ui/react';
import { CompatibleDevice } from '@components/common/CompatibleDevice';
import { PageContentWrapper } from '@ifixit/ui';
import type { Product } from '@models/product/server';
import type { Product } from '@pages/api/nextjs/cache/product';
import NextLink from 'next/link';

export type CompatibilitySectionProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
useCartDrawer,
useGetUserPrice,
} from '@ifixit/ui';
import type { Product, ProductVariant } from '@models/product/server';
import type { Product, ProductVariant } from '@pages/api/nextjs/cache/product';
import NextLink from 'next/link';
import React from 'react';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
ProductVariantPrice,
ResponsiveImage,
} from '@ifixit/ui';
import type { Product } from '@models/product/server';
import type { Product } from '@pages/api/nextjs/cache/product';
import { ImagePlaceholder } from '@templates/product/components/ImagePlaceholder';
import NextLink from 'next/link';
import * as React from 'react';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Box, Button, Flex, Heading, Icon, Text } from '@chakra-ui/react';
import { useAppContext } from '@ifixit/app';
import { isLifetimeWarranty } from '@ifixit/helpers';
import { PageContentWrapper } from '@ifixit/ui';
import type { ProductVariant } from '@models/product/server';
import type { ProductVariant } from '@pages/api/nextjs/cache/product';
import backgroundImage from '@public/images/lifetime-guarantee-background.jpg';
import Image from 'next/image';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { useAddToCart, useCartLineItem } from '@ifixit/cart-sdk';
import { FaIcon } from '@ifixit/icons';
import { useCartDrawer, useUserPrice } from '@ifixit/ui';
import type { Product, ProductVariant } from '@models/product/server';
import type { Product, ProductVariant } from '@pages/api/nextjs/cache/product';
import * as React from 'react';
import { NotifyMeForm } from './NotifyMeForm';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { chakra, Link } from '@chakra-ui/react';
import { CompatibleDevice } from '@components/common';
import type { Product } from '@models/product/server';
import type { Product } from '@pages/api/nextjs/cache/product';
import NextLink from 'next/link';

export type CompatibleDevicesProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
Vive,
} from '@assets/svg/files/partners';
import { Flex, Link, Text, useTheme } from '@chakra-ui/react';
import type { Product } from '@models/product/server';
import type { Product } from '@pages/api/nextjs/cache/product';
import React from 'react';

export type GenuinePartBannerProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
faTriangleExclamation,
} from '@fortawesome/pro-solid-svg-icons';
import { FaIcon } from '@ifixit/icons';
import type { Product, ProductVariant } from '@models/product/server';
import type { Product, ProductVariant } from '@pages/api/nextjs/cache/product';

export type ProductDescriptionProps = {
product: Product;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
Product,
ProductImage,
ProductVariant,
} from '@models/product/server';
} from '@pages/api/nextjs/cache/product';
import { useSwiper } from '@templates/product/hooks/useSwiper';
import * as React from 'react';
import ReactDOM from 'react-dom';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { faImageSlash } from '@fortawesome/pro-duotone-svg-icons';
import { FaIcon } from '@ifixit/icons';
import { ResponsiveImage } from '@ifixit/ui';
import type { Product } from '@models/product/server';
import type { Product } from '@pages/api/nextjs/cache/product';
import * as React from 'react';

export type ProductOptionsProps = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box, HStack, Link, Text } from '@chakra-ui/react';
import { Rating } from '@components/ui';
import { shouldShowProductRating } from '@ifixit/helpers';
import type { Product } from '@models/product/server';
import type { Product } from '@pages/api/nextjs/cache/product';

type ProductRatingProps = {
product: Product;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Box } from '@chakra-ui/react';
import type { Product } from '@models/product/server';
import type { Product } from '@pages/api/nextjs/cache/product';
import { LiteYoutubeEmbed } from 'react-lite-yt-embed';

export function ProductVideos({ product }: { product: Product }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { useAppContext } from '@ifixit/app';
import { isLifetimeWarranty } from '@ifixit/helpers';
import { FaIcon } from '@ifixit/icons';
import { PageContentWrapper, ProductVariantPrice } from '@ifixit/ui';
import type { Product, ProductVariant } from '@models/product/server';
import type { Product, ProductVariant } from '@pages/api/nextjs/cache/product';
import { useIsProductForSale } from '@templates/product/hooks/useIsProductForSale';
import * as React from 'react';
import { BuyBoxPropositionSection } from '../ServiceValuePropositionSection';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
faGaugeMin,
} from '@fortawesome/pro-solid-svg-icons';
import { IconBadge, PageContentWrapper, ResponsiveImage } from '@ifixit/ui';
import type { Product } from '@models/product/server';
import type { Product } from '@pages/api/nextjs/cache/product';

export type ReplacementGuidesSectionProps = {
product: Product;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { useAppContext } from '@ifixit/app';
import { FaIcon } from '@ifixit/icons';
import { PageContentWrapper } from '@ifixit/ui';
import type { ProductReview } from '@models/product/reviews';
import type { Product, ProductVariant } from '@models/product/server';
import type { Product, ProductVariant } from '@pages/api/nextjs/cache/product';
import { useProductReviews } from '@templates/product/hooks/useProductReviews';
import React from 'react';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ProductVariant } from '@models/product/server';
import type { ProductVariant } from '@pages/api/nextjs/cache/product';
import { Heading, Stack, Text, VStack } from '@chakra-ui/react';
import React from 'react';
import { List, ListIcon, ListItem } from '@chakra-ui/react';
Expand Down
4 changes: 2 additions & 2 deletions frontend/templates/product/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ifixitOriginFromHost } from '@helpers/path-helpers';
import { invariant } from '@ifixit/helpers';
import { urlFromContext } from '@ifixit/helpers/nextjs';
import { getLayoutServerSideProps } from '@layouts/default/server';
import { findProduct } from '@models/product/server';
import Product from '@pages/api/nextjs/cache/product';
import compose from 'lodash/flowRight';
import { GetServerSideProps } from 'next';
import { ProductTemplateProps } from './hooks/useProductTemplateProps';
Expand All @@ -23,7 +23,7 @@ export const getServerSideProps: GetServerSideProps<ProductTemplateProps> =
const { stores, ...otherLayoutProps } = await getLayoutServerSideProps({
storeCode: DEFAULT_STORE_CODE,
});
const product = await findProduct({
const product = await Product.get({
handle,
storeCode: DEFAULT_STORE_CODE,
});
Expand Down
2 changes: 1 addition & 1 deletion frontend/tests/jest/__mocks__/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
Product,
ProductVariant,
ProductVariantImage,
} from '@models/product/server';
} from '@pages/api/nextjs/cache/product';
import { ProductTemplateProps } from '@templates/product/hooks/useProductTemplateProps';

const productImages: ProductVariantImage[] = [
Expand Down
2 changes: 1 addition & 1 deletion frontend/tests/jest/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AppProviders } from '@components/common';
import { CurrencyCode } from '@lib/shopify-storefront-sdk';
import { ProductReview, ProductReviewData } from '@models/product/reviews';
import { ProductSearchHit } from '@models/product-list';
import type { Product, ProductVariant } from '@models/product/server';
import type { Product, ProductVariant } from '@pages/api/nextjs/cache/product';
import { render, RenderOptions, RenderResult } from '@testing-library/react';
import {
mockedBatteryProduct,
Expand Down

0 comments on commit f13399d

Please sign in to comment.