Skip to content

Commit

Permalink
Debugging Upstash #6
Browse files Browse the repository at this point in the history
  • Loading branch information
david emioma committed Apr 3, 2024
1 parent c29b053 commit 713d315
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const ProductContent = ({ product }: Props) => {
});

return (
<div className="w-full grid md:grid-cols-2" data-testId="product-content">
<div className="w-full grid md:grid-cols-2" data-testid="product-content">
<ProductSlider images={curProductItem?.images || []} />

<div className="py-5 md:py-0 md:pl-10">
Expand Down
8 changes: 2 additions & 6 deletions app/api/cart/[cartItemId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ export async function PATCH(
},
});

if (process.env.VERCEL_ENV !== "preview") {
await redis.set(`${user.id}-cart`, newCart);
}
await redis.set(`${user.id}-cart`, newCart);

return NextResponse.json({ message: "Cart item updated!" });
} catch (err) {
Expand Down Expand Up @@ -216,9 +214,7 @@ export async function DELETE(
},
});

if (process.env.VERCEL_ENV !== "preview") {
await redis.set(`${user.id}-cart`, newCart);
}
await redis.set(`${user.id}-cart`, newCart);

return NextResponse.json({ message: "Cart item deleted!" });
} catch (err) {
Expand Down
12 changes: 4 additions & 8 deletions app/api/cart/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,10 @@ export async function GET(request: Request) {
return NextResponse.json({ cart: null });
}

if (process.env.VERCEL_ENV !== "preview") {
const cachedCart = await redis.get(`${user.id}-cart`);
const cachedCart = await redis.get(`${user.id}-cart`);

if (cachedCart) {
return NextResponse.json(cachedCart);
}
if (cachedCart) {
return NextResponse.json(cachedCart);
}

const cart = await prismadb.cart.findUnique({
Expand Down Expand Up @@ -227,9 +225,7 @@ export async function GET(request: Request) {
},
});

if (process.env.VERCEL_ENV !== "preview") {
await redis.set(`${user.id}-cart`, cart);
}
await redis.set(`${user.id}-cart`, cart);

return NextResponse.json(cart);
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions lib/redis.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
import { Ratelimit } from "@upstash/ratelimit";

export const redis = new Redis({
url: process.env.UPSTASH_REDIS_REST_URL as string,
token: process.env.UPSTASH_REDIS_REST_TOKEN as string,
});

export const apiRatelimit = new Ratelimit({
redis: redis ?? Redis.fromEnv(),
redis,
limiter: Ratelimit.slidingWindow(10, "1 m"),
});
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@react-email/render": "^0.0.12",
"@tanstack/react-query": "^5.28.6",
"@tanstack/react-table": "^8.11.8",
"@upstash/ratelimit": "^1.0.1",
"@upstash/ratelimit": "^1.0.3",
"@upstash/redis": "^1.29.0",
"axios": "^1.6.7",
"bcryptjs": "^2.4.3",
Expand Down

0 comments on commit 713d315

Please sign in to comment.