Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(sm): cleanup #83

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
SHOPIFY_STOREFRONT_ACCESS_TOKEN: ${{ secrets.SHOPIFY_STOREFRONT_ACCESS_TOKEN }}
SHOPIFY_ADMIN_ACCESS_TOKEN: ${{ secrets.SHOPIFY_ADMIN_ACCESS_TOKEN }}
SHOPIFY_STORE_DOMAIN: ${{ secrets.SHOPIFY_STORE_DOMAIN }}
MEILISEARCH_MASTER_KEY: ${{ secrets.MEILISEARCH_MASTER_KEY }}
MEILISEARCH_ADMIN_KEY: ${{ secrets.MEILISEARCH_ADMIN_KEY }}
MEILISEARCH_HOST: ${{ secrets.MEILISEARCH_HOST }}
SHOPIFY_APP_API_SECRET_KEY: ${{ secrets.SHOPIFY_APP_API_SECRET_KEY }}
REPLICATE_API_KEY: ${{ secrets.REPLICATE_API_KEY }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nextjs_bundle_analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
SHOPIFY_STOREFRONT_ACCESS_TOKEN: ${{ secrets.SHOPIFY_STOREFRONT_ACCESS_TOKEN }}
SHOPIFY_ADMIN_ACCESS_TOKEN: ${{ secrets.SHOPIFY_ADMIN_ACCESS_TOKEN }}
SHOPIFY_STORE_DOMAIN: ${{ secrets.SHOPIFY_STORE_DOMAIN }}
MEILISEARCH_MASTER_KEY: ${{ secrets.MEILISEARCH_MASTER_KEY }}
MEILISEARCH_ADMIN_KEY: ${{ secrets.MEILISEARCH_ADMIN_KEY }}
MEILISEARCH_HOST: ${{ secrets.MEILISEARCH_HOST }}
SHOPIFY_APP_API_SECRET_KEY: ${{ secrets.SHOPIFY_APP_API_SECRET_KEY }}
REPLICATE_API_KEY: ${{ secrets.REPLICATE_API_KEY }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
SHOPIFY_STOREFRONT_ACCESS_TOKEN: ${{ secrets.SHOPIFY_STOREFRONT_ACCESS_TOKEN }}
SHOPIFY_ADMIN_ACCESS_TOKEN: ${{ secrets.SHOPIFY_ADMIN_ACCESS_TOKEN }}
SHOPIFY_STORE_DOMAIN: ${{ secrets.SHOPIFY_STORE_DOMAIN }}
MEILISEARCH_MASTER_KEY: ${{ secrets.MEILISEARCH_MASTER_KEY }}
MEILISEARCH_ADMIN_KEY: ${{ secrets.MEILISEARCH_ADMIN_KEY }}
MEILISEARCH_HOST: ${{ secrets.MEILISEARCH_HOST }}
SHOPIFY_APP_API_SECRET_KEY: ${{ secrets.SHOPIFY_APP_API_SECRET_KEY }}
REPLICATE_API_KEY: ${{ secrets.REPLICATE_API_KEY }}
Expand Down
27 changes: 0 additions & 27 deletions starters/shopify-meilisearch/app/actions/collection.actions.ts

This file was deleted.

8 changes: 0 additions & 8 deletions starters/shopify-meilisearch/app/actions/page.actions.ts

This file was deleted.

60 changes: 3 additions & 57 deletions starters/shopify-meilisearch/app/actions/product.actions.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
"use server"

import { unstable_cache } from "next/cache"
import { env } from "env.mjs"

import { meilisearch } from "clients/search"
import type { Review } from "lib/reviews/types"

import { ComparisonOperators, FilterBuilder } from "lib/meilisearch/filter-builder"
import { getDemoProductReviews, getDemoSingleProduct, isDemoMode } from "utils/demo-utils"
import { env } from "env.mjs"
import { unstable_cache } from "next/cache"
import type { CommerceProduct } from "types"
import { notifyOptIn } from "utils/opt-in"
import { isDemoMode } from "utils/demo-utils"

export const searchProducts = unstable_cache(
async (query: string, limit: number = 4) => {
Expand All @@ -33,52 +28,3 @@ export const searchProducts = unstable_cache(
["autocomplete-search"],
{ revalidate: 3600 }
)

export const getProduct = unstable_cache(
async (handle: string) => {
if (isDemoMode()) return getDemoSingleProduct(handle)

const { results } = await meilisearch.getDocuments<CommerceProduct>({
indexName: env.MEILISEARCH_PRODUCTS_INDEX,
options: {
filter: new FilterBuilder().where("handle", ComparisonOperators.Equal, handle).build(),
limit: 1,
},
})

return results.find(Boolean) || null
},
["product-by-handle"],
{ revalidate: 3600 }
)

export const getProductReviews = unstable_cache(
async (handle: string, { page = 1, limit = 10 } = { page: 1, limit: 10 }) => {
if (isDemoMode()) return getDemoProductReviews()

if (!env.MEILISEARCH_REVIEWS_INDEX) {
notifyOptIn({ feature: "reviews", source: "product.actions.ts" })
return { reviews: [], total: 0 }
}

const { results, total } = await meilisearch.getDocuments<Review>({
indexName: env.MEILISEARCH_REVIEWS_INDEX,
options: {
filter: new FilterBuilder()
.where("product_handle", ComparisonOperators.Equal, handle)
.and()
.where("published", ComparisonOperators.Equal, "true")
.and()
.where("hidden", ComparisonOperators.Equal, "false")
.build(),
limit,
offset: (page - 1) * limit,
fields: ["body", "rating", "verified", "reviewer", "published", "created_at", "hidden", "featured"],
},
})

return { reviews: results.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()), total }
},
["product-reviews-by-handle"],
{ revalidate: 3600 }
)
2 changes: 0 additions & 2 deletions starters/shopify-meilisearch/app/api/redirects/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ type RedirectEntry = {
permanent: boolean
}

export const runtime = "edge"

export function GET(request: NextRequest) {
const pathname = request.nextUrl.searchParams.get("pathname")

Expand Down
2 changes: 1 addition & 1 deletion starters/shopify-meilisearch/app/favorites/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { cookies } from "next/headers"
import { Suspense } from "react"
import { getProduct } from "app/actions/product.actions"
import { ProductCard } from "components/product-card"
import { Skeleton } from "components/ui/skeleton"
import { COOKIE_FAVORITES } from "constants/index"
import { getProduct } from "clients/search"

export const revalidate = 86400

Expand Down
8 changes: 2 additions & 6 deletions starters/shopify-meilisearch/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import "./globals.css"

import nextDynamic from "next/dynamic"
import Script from "next/script"
import { Suspense } from "react"
import { Toaster } from "sonner"
Expand All @@ -16,8 +15,7 @@ import { NavigationBar } from "components/navigation-bar/navigation-bar"
import { mobileInlineScript } from "components/navigation-bar/mobile-inline-script"
import { Footer } from "components/footer"
import { Modals } from "components/modals/modals"

const DraftToolbar = nextDynamic(() => import("views/draft-toolbar"), { ssr: false })
import DraftToolbar from "views/draft-toolbar"

export const revalidate = 86400

Expand Down Expand Up @@ -257,9 +255,7 @@ export default async function RootLayout({ children }: { children: React.ReactNo
<FlagValues />
</Suspense>

<Suspense>
<ThirdParties />
</Suspense>
<ThirdParties />

<GithubBadge />
<DemoModeAlert />
Expand Down
2 changes: 1 addition & 1 deletion starters/shopify-meilisearch/app/pages/[slug]/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getPage } from "clients/storefrontClient"
import { Metadata } from "next"
import { getPage } from "app/actions/page.actions"

export async function generateMetadata({ params: { slug } }: { params: { slug: string } }): Promise<Metadata> {
const page = await getPage(slug)
Expand Down
2 changes: 1 addition & 1 deletion starters/shopify-meilisearch/app/pages/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { format } from "date-fns/format"
import { getAllPages, getPage } from "app/actions/page.actions"
import { getAllPages, getPage } from "clients/storefrontClient"

export const revalidate = 86400
export const dynamic = "force-static"
Expand Down
6 changes: 3 additions & 3 deletions starters/shopify-meilisearch/app/product/[slug]/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getProduct } from "clients/search"
import { env } from "env.mjs"
import { Metadata } from "next"
import { Product, WithContext } from "schema-dts"
import { getProduct } from "app/actions/product.actions"
import { env } from "env.mjs"
import type { CommerceProduct } from "types"
import { makeKeywords } from "utils/make-keywords"
import { removeOptionsFromUrl } from "utils/product-options-utils"
import type { CommerceProduct } from "types"
import { slugToName } from "utils/slug-name"

interface ProductProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
/* eslint-disable jsx-a11y/alt-text */

import { ImageResponse } from "next/og"
import { getProduct } from "app/actions/product.actions"
import { removeOptionsFromUrl } from "utils/product-options-utils"
import { env } from "env.mjs"
import { getProduct } from "clients/search"

export const revalidate = 86400

Expand Down
42 changes: 18 additions & 24 deletions starters/shopify-meilisearch/app/product/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
import { notFound } from "next/navigation"
import { Suspense } from "react"
import { getProduct, getProductReviews } from "app/actions/product.actions"
import { Breadcrumbs } from "components/breadcrumbs"
import { notFound } from "next/navigation"

import { getProduct, meilisearch } from "clients/search"

import { env } from "env.mjs"

import { isDemoMode } from "utils/demo-utils"
import { slugToName } from "utils/slug-name"
import { CurrencyType, mapCurrencyToSign } from "utils/map-currency-to-sign"
import { getCombination, getOptionsFromUrl, hasValidOption, removeOptionsFromUrl } from "utils/product-options-utils"

import { Breadcrumbs } from "components/breadcrumbs"

import { BackButton } from "views/product/back-button"
import { FavoriteMarker } from "views/product/favorite-marker"
import { SimilarProductsSection } from "views/product/similar-products-section"
import { SimilarProductsSectionSkeleton } from "views/product/similar-product-section-skeleton"
import { VariantsSection } from "views/product/variants-section"
import { slugToName } from "utils/slug-name"

import { generateJsonLd } from "./metadata"
import { ReviewsSection } from "views/product/reviews-section"

import type { CommerceProduct } from "types"
import { isDemoMode } from "utils/demo-utils"
import { meilisearch } from "clients/search"
import { env } from "env.mjs"
import { ProductTitle } from "views/product/product-title"
import { CurrencyType, mapCurrencyToSign } from "utils/map-currency-to-sign"
import { ProductImages } from "views/product/product-images"
import { RightSection } from "views/product/right-section"
import { FaqSection } from "views/product/faq-section"
import { AddToCartButton } from "views/product/add-to-cart-button"
import { ReviewsSection } from "views/product/reviews-section"

import type { CommerceProduct } from "types"

import { generateJsonLd } from "./metadata"

export const revalidate = 86400
export const dynamic = "force-static"
Expand All @@ -33,8 +36,6 @@ interface ProductProps {
params: { slug: string }
}

export { generateMetadata } from "./metadata"

export async function generateStaticParams() {
if (isDemoMode()) return []

Expand All @@ -50,7 +51,7 @@ export async function generateStaticParams() {
}

export default async function Product({ params: { slug } }: ProductProps) {
const [product, { reviews, total: totalReviews }] = await Promise.all([getProduct(removeOptionsFromUrl(slug)), getProductReviews(removeOptionsFromUrl(slug), { limit: 16 })])
const product = await getProduct(removeOptionsFromUrl(slug))

const { color } = getOptionsFromUrl(slug)
const hasInvalidOptions = !hasValidOption(product?.variants, "color", color)
Expand Down Expand Up @@ -97,14 +98,7 @@ export default async function Product({ params: { slug } }: ProductProps) {
</RightSection>
</div>
<Suspense>
<ReviewsSection
avgRating={product.avgRating}
productHandle={product.handle}
productId={product.id}
reviews={reviews?.map((review) => ({ ...review, author: review.reviewer.name })) || []}
total={totalReviews}
summary={product.reviewsSummary}
/>
<ReviewsSection avgRating={product.avgRating} productHandle={product.handle} productId={product.id} slug={slug} summary={product.reviewsSummary} />
</Suspense>
<Suspense fallback={<SimilarProductsSectionSkeleton />}>
<SimilarProductsSection collectionHandle={lastCollection?.handle} slug={slug} />
Expand Down
4 changes: 2 additions & 2 deletions starters/shopify-meilisearch/app/reviews/[slug]/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getProduct } from "app/actions/product.actions"
import { env } from "process"
import { makeKeywords } from "utils/make-keywords"
import { removeOptionsFromUrl } from "utils/product-options-utils"
import type { ProductReviewsPageProps } from "./page"
import { Metadata } from "next"
import { getProduct } from "clients/search"
import { env } from "env.mjs"

export async function generateMetadata({ params: { slug } }: ProductReviewsPageProps): Promise<Metadata> {
const product = await getProduct(removeOptionsFromUrl(slug))
Expand Down
27 changes: 13 additions & 14 deletions starters/shopify-meilisearch/app/reviews/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { notFound, redirect } from "next/navigation"
import { getProduct, getProductReviews } from "app/actions/product.actions"

import { getProduct, getProductReviews } from "clients/search"

import { Breadcrumbs } from "components/breadcrumbs"

import { BackButton } from "views/product/back-button"
Expand All @@ -8,6 +10,7 @@ import { PaginationSection } from "views/listing/pagination-section"

import { removeOptionsFromUrl } from "utils/product-options-utils"
import type { CommerceProduct } from "types"
import { HITS_PER_PAGE } from "constants/index"

export { generateMetadata } from "./metadata"

Expand All @@ -19,29 +22,29 @@ export interface ProductReviewsPageProps {
}

export default async function ProductReviews({ params: { slug }, searchParams }: ProductReviewsPageProps) {
return <ProductReviewsView searchParams={searchParams} slug={slug} />
}

async function ProductReviewsView({ slug, searchParams }: { slug: string; searchParams: ProductReviewsPageProps["searchParams"] }) {
const limit = 20
const page = searchParams.page ? parseInt(searchParams.page as string) : 1

const product = await getProduct(removeOptionsFromUrl(slug))
const { reviews, total: totalReviews } = await getProductReviews(removeOptionsFromUrl(slug), { limit, page })
const [product, { reviews, total: totalReviews }] = await Promise.all([
getProduct(removeOptionsFromUrl(slug)),
getProductReviews(removeOptionsFromUrl(slug), { HITS_PER_PAGE, page }),
])

const totalPages = Math.ceil(totalReviews / limit)
const totalPages = Math.ceil(totalReviews / HITS_PER_PAGE)

if (!product) {
return notFound()
}

if (page > Math.ceil(totalReviews / HITS_PER_PAGE)) {
redirect(`/reviews/${slug}`)
}

if (totalReviews <= 0) {
return (
<div className="relative mx-auto mb-20 max-w-container-md px-4 xl:px-0">
<div className="relative w-fit py-4 md:pt-12">
<BackButton href={`/product/${product.handle}`} className="mb-8 hidden md:block" />
</div>

<main className="container mx-auto max-w-5xl px-4 md:px-6">
<Breadcrumbs className="mb-8" items={makeBreadcrumbs(product)} />
<div className="my-20 text-center">
Expand All @@ -53,10 +56,6 @@ async function ProductReviewsView({ slug, searchParams }: { slug: string; search
)
}

if (page > Math.ceil(totalReviews / limit)) {
redirect(`/reviews/${slug}`)
}

return (
<div className="relative mx-auto mb-20 max-w-container-md px-4 xl:px-0">
<div className="relative w-fit py-4 md:pt-12">
Expand Down
Loading
Loading