Skip to content

Commit 480aa3a

Browse files
authored
Merge pull request #153 from medusajs/151-infinite-loop-in-collection-page
fix: infinite loop in collection pages
2 parents 8afbbc8 + ece5fe5 commit 480aa3a

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/app/(main)/collections/route.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const runtime = "edge"
55

66
export async function GET(request: NextRequest) {
77
const searchParams = Object.fromEntries(request.nextUrl.searchParams)
8-
const { handle, pageParam, limit, cart_id } = searchParams
8+
const { handle, page, limit, cart_id } = searchParams
99

1010
const collection = await fetchCollection(handle)
1111
.then((res) => res)
@@ -14,7 +14,7 @@ export async function GET(request: NextRequest) {
1414
})
1515

1616
const { products, count, nextPage } = await fetchCollectionProducts({
17-
pageParam,
17+
page,
1818
id: collection.id,
1919
limit,
2020
cart_id,
@@ -41,12 +41,12 @@ async function fetchCollection(handle: string) {
4141
}
4242

4343
async function fetchCollectionProducts({
44-
pageParam = "0",
44+
page = "0",
4545
id,
4646
limit = "12",
4747
cart_id,
4848
}: {
49-
pageParam: string
49+
page: string
5050
id: string
5151
limit: string
5252
cart_id: string
@@ -56,17 +56,16 @@ async function fetchCollectionProducts({
5656
collection_id: [id],
5757
cart_id,
5858
limit,
59-
offset: pageParam,
59+
offset: page,
6060
expand: "variants,variants.prices",
6161
},
6262
}).then((res) => res.body)
6363

64+
const nextPage = parseInt(offset) + parseInt(limit)
65+
6466
return {
6567
products,
6668
count,
67-
nextPage:
68-
count > parseInt(offset) + parseInt(limit)
69-
? parseInt(offset) + parseInt(limit)
70-
: null,
69+
nextPage: count > nextPage ? nextPage : null,
7170
}
7271
}

0 commit comments

Comments
 (0)