Skip to content

Commit 58ed886

Browse files
committed
small fix to curriculum
1 parent bf7246a commit 58ed886

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

app-react-router-framework/LESSONS/03-performance/lecture/NOTES.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ MUST READ: https://remix.run/docs/en/main/guides/single-fetch
66

77
## Caching
88

9+
https://reactrouter.com/how-to/headers#from-loaders-and-actions
910
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
1011
https://gist.github.com/kentcdodds/0c6f183531beeafe771eb48a3586707b
1112

@@ -20,8 +21,8 @@ export async function loader() {
2021
})
2122
}
2223

23-
export const headers = () => {
24-
// Page Caching
25-
return { 'Cache-Control': `public, max-age=${ONE_HOUR}, s-maxage=${ONE_HOUR}` }
24+
// REQUIRED to be here if we're doing loader or action caching
25+
export function headers({ loaderHeaders }: HeadersArgs) {
26+
return loaderHeaders
2627
}
2728
```

app-react-router-framework/LESSONS/03-performance/lecture/routes/home.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default function Index({ loaderData: { products } }: Route.ComponentProps
4141
{/* prefetch="intent" */}
4242
{/* prefetch="viewport" */}
4343
{/* prefetch="render" */}
44-
<Link prefetch="intent" to={product.id.toString()} className="button">
44+
<Link prefetch="none" to={product.id.toString()} className="button">
4545
View
4646
</Link>
4747
</div>

app-react-router-framework/LESSONS/03-performance/lecture/routes/product-profile.tsx

+48-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import { Await } from 'react-router'
1+
import { data, Await, type LoaderFunctionArgs } from 'react-router'
22
import { getBrands, getCategories, getProduct, getRelatedProducts } from '~/utils/db.server'
33
import { sleep, sortLabel } from '~/utils/helpers'
44
import { ProductProfile } from '~/components/ProductProfile'
55
import { Tiles } from '~/components/Tiles'
66
import { Suspense } from 'react'
77
import { BrowseProductItem } from '~/components/BrowseProducts'
88
import { useCart } from '~/state/CartContext'
9-
import type { LoaderFunctionArgs } from 'react-router'
109
import type { Route } from './+types/product-profile'
1110

11+
import type { HeadersArgs } from 'react-router'
12+
13+
const CACHE_TIME = 1000 + 10 // 10 seconds
14+
1215
export const loader = async ({ params }: LoaderFunctionArgs) => {
1316
const productId = parseInt(params.productId!)
1417
if (!productId) throw new Response('Invalid Product ID', { status: 404 })
@@ -20,21 +23,54 @@ export const loader = async ({ params }: LoaderFunctionArgs) => {
2023
])
2124

2225
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
2726
const limit = 3
2827
const relatedProductsPromise = getRelatedProducts(product.brand, limit, [productId]).then(sleep())
2928

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
3646
}
3747

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+
3874
export default function Page({ loaderData }: Route.ComponentProps) {
3975
const { product, brands, categories, relatedProductsPromise } = loaderData
4076

0 commit comments

Comments
 (0)