1
- import { Await } from 'react-router'
1
+ import { data , Await , type LoaderFunctionArgs } from 'react-router'
2
2
import { getBrands , getCategories , getProduct , getRelatedProducts } from '~/utils/db.server'
3
3
import { sleep , sortLabel } from '~/utils/helpers'
4
4
import { ProductProfile } from '~/components/ProductProfile'
5
5
import { Tiles } from '~/components/Tiles'
6
6
import { Suspense } from 'react'
7
7
import { BrowseProductItem } from '~/components/BrowseProducts'
8
8
import { useCart } from '~/state/CartContext'
9
- import type { LoaderFunctionArgs } from 'react-router'
10
9
import type { Route } from './+types/product-profile'
11
10
11
+ import type { HeadersArgs } from 'react-router'
12
+
13
+ const CACHE_TIME = 1000 + 10 // 10 seconds
14
+
12
15
export const loader = async ( { params } : LoaderFunctionArgs ) => {
13
16
const productId = parseInt ( params . productId ! )
14
17
if ( ! productId ) throw new Response ( 'Invalid Product ID' , { status : 404 } )
@@ -20,21 +23,54 @@ export const loader = async ({ params }: LoaderFunctionArgs) => {
20
23
] )
21
24
22
25
if ( ! product ) throw new Response ( 'Not found' , { status : 404 } )
23
-
24
- // NOTICE: This is an unresolved promise. The lack of await means that
25
- // `relatedProductsPromise` is a promise variable being returned from
26
- // the loader to the client
27
26
const limit = 3
28
27
const relatedProductsPromise = getRelatedProducts ( product . brand , limit , [ productId ] ) . then ( sleep ( ) )
29
28
30
- return {
31
- product,
32
- brands : brands . sort ( sortLabel ) ,
33
- categories : categories . sort ( sortLabel ) ,
34
- relatedProductsPromise,
35
- }
29
+ return data (
30
+ {
31
+ product,
32
+ brands : brands . sort ( sortLabel ) ,
33
+ categories : categories . sort ( sortLabel ) ,
34
+ relatedProductsPromise,
35
+ } ,
36
+ {
37
+ headers : {
38
+ 'Cache-Control' : `public, max-age=${ CACHE_TIME } , s-maxage=${ CACHE_TIME } ` ,
39
+ } ,
40
+ }
41
+ )
42
+ }
43
+
44
+ export function headers ( { loaderHeaders } : HeadersArgs ) {
45
+ return loaderHeaders
36
46
}
37
47
48
+ // export const loader = async ({ params }: LoaderFunctionArgs) => {
49
+ // const productId = parseInt(params.productId!)
50
+ // if (!productId) throw new Response('Invalid Product ID', { status: 404 })
51
+
52
+ // const [product, brands, categories] = await Promise.all([
53
+ // getProduct(productId),
54
+ // getBrands(),
55
+ // getCategories(),
56
+ // ])
57
+
58
+ // if (!product) throw new Response('Not found', { status: 404 })
59
+
60
+ // // NOTICE: This is an unresolved promise. The lack of await means that
61
+ // // `relatedProductsPromise` is a promise variable being returned from
62
+ // // the loader to the client
63
+ // const limit = 3
64
+ // const relatedProductsPromise = getRelatedProducts(product.brand, limit, [productId]).then(sleep())
65
+
66
+ // return {
67
+ // product,
68
+ // brands: brands.sort(sortLabel),
69
+ // categories: categories.sort(sortLabel),
70
+ // relatedProductsPromise,
71
+ // }
72
+ // }
73
+
38
74
export default function Page ( { loaderData } : Route . ComponentProps ) {
39
75
const { product, brands, categories, relatedProductsPromise } = loaderData
40
76
0 commit comments