Skip to content

Commit 2bfc721

Browse files
committed
Simplify remote-cache snippet and add await to db.product.find
Based on Sam's feedback: the readme code snippet only needs to show getProductPrice, not the ProductPrice component. Also adds the missing await in front of db.product.find. Made-with: Cursor
1 parent 82c3973 commit 2bfc721

2 files changed

Lines changed: 4 additions & 12 deletions

File tree

app/remote-cache/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ async function getProductPrice(productId: string) {
117117
await new Promise((resolve) => setTimeout(resolve, 1000));
118118

119119
// DEMO: Query the database for the product price
120-
const product = db.product.find({ where: { id: productId } });
120+
const product = await db.product.find({ where: { id: productId } });
121121

122122
if (!product) {
123123
throw new Error(`Product not found: ${productId}`);

app/remote-cache/readme.mdx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const demo = db.demo.find({ where: { slug: 'remote-cache' } });
1111

1212
- Use `use cache: remote` to store cache entries in a remote handler (e.g. Redis or KV), providing **durable, shared caching** across all server instances.
1313
- Regular `use cache` stores entries in-memory, which works for static shell content but may have low hit rates at runtime in serverless environments.
14-
- Most useful for data accessed at runtime in dynamic components (e.g. those reading `cookies()`, `headers()`, or `connection()`), where remote caching reduces load on rate-limited APIs, slow backends, or expensive operations.
14+
- Most useful for data that would otherwise be re-fetched on every request, where remote caching reduces load on rate-limited APIs, slow backends, or expensive operations.
1515

1616
# !!col
1717

@@ -20,25 +20,17 @@ async function getProductPrice(productId: string) {
2020
// !mark
2121
'use cache: remote';
2222
cacheTag(`product-price-${productId}`);
23-
const product = db.product.find({ where: { id: productId } });
23+
const product = await db.product.find({ where: { id: productId } });
2424
return product.price;
2525
}
26-
27-
// !mark
28-
// cookies() makes this component dynamic
29-
async function ProductPrice({ productId }: { productId: string }) {
30-
const currency = validateCurrency((await cookies()).get('currency')?.value);
31-
const price = await getProductPrice(productId);
32-
return <div>Price: {format(price, currency)}</div>;
33-
}
3426
```
3527

3628
</Grid>
3729

3830
### Demo
3931

4032
- **Product data** is fetched with `use cache` in `getData()` - cached in the static shell.
41-
- **Price data** is fetched with `use cache: remote` in `getProductPrice()` - the component reads `cookies()` making it dynamic, so the price is cached in a remote cache at runtime.
33+
- **Price data** is fetched with `use cache: remote` in `getProductPrice()` - stored durably in a remote cache, shared across all server instances.
4234
- The price data function uses `cacheTag()` for targeted cache invalidation.
4335
- Artificial delays (1s for products, 1s for prices) make caching behavior visible.
4436
- Notice how product data loads once, while price data streams in per product, but is cached for subsequent requests.

0 commit comments

Comments
 (0)