Skip to content

Commit

Permalink
鉁忥笍 add suspense boundaries when using useSearchParams
Browse files Browse the repository at this point in the history
  • Loading branch information
Azanniel committed Dec 27, 2023
1 parent bbeb76e commit 014cba9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
16 changes: 8 additions & 8 deletions src/app/(store)/product/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ export async function generateMetadata({
}
}

// export async function generateStaticParams() {
// const response = await api('/products/featured')
// const products: Product[] = await response.json()

// return products.map((product) => {
// return { slug: product.slug }
// })
// }
export async function generateStaticParams() {
const response = await api('/products/featured')
const products: Product[] = await response.json()

return products.map((product) => {
return { slug: product.slug }
})
}

export default async function ProductPage({ params }: ProductProps) {
const product = await getProduct(params.slug)
Expand Down
15 changes: 15 additions & 0 deletions src/app/(store)/search/current-search.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use client'

import { useSearchParams } from 'next/navigation'

export function CurrentSearch() {
const searchParams = useSearchParams()

const query = searchParams.get('q')

return (
<p className="text-sm">
Resultados para: <span className="font-semibold">{query}</span>
</p>
)
}
15 changes: 5 additions & 10 deletions src/app/(store)/search/loading.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
'use client'

import { Suspense } from 'react'
import { Skeleton } from '@/components/skeleton'
import { useSearchParams } from 'next/navigation'
import { CurrentSearch } from './current-search'

export default function SearchLoading() {
const searchParams = useSearchParams()

const query = searchParams.get('q')

return (
<div className="flex flex-col gap-4">
<p className="text-sm">
Resultados para: <span className="font-semibold">{query}</span>
</p>
<Suspense fallback={null}>
<CurrentSearch />
</Suspense>

<div className="grid grid-cols-3 gap-6">
<Skeleton className="h-[400px]" />
Expand Down
5 changes: 4 additions & 1 deletion src/components/header.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Suspense } from 'react'
import Link from 'next/link'
import Image from 'next/image'
import { CartWidget } from './cart-widget'
Expand All @@ -11,7 +12,9 @@ export function Header() {
devstore
</Link>

<SearchForm />
<Suspense fallback={null}>
<SearchForm />
</Suspense>
</div>

<div className="flex items-center gap-4">
Expand Down

0 comments on commit 014cba9

Please sign in to comment.