Skip to content

Commit ab620e5

Browse files
authored
Merge pull request #155 from medusajs/fix/404
fix: proper handling of 404 in routes
2 parents 480aa3a + e4f6097 commit ab620e5

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

src/app/(main)/collections/[handle]/page.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import CollectionTemplate from "@modules/collections/templates"
22
import { Metadata } from "next"
3+
import { notFound } from "next/navigation"
34

45
type Props = {
56
params: { handle: string }
@@ -11,7 +12,7 @@ async function getCollection(handle: string) {
1112
const res = await fetch(`${BASEURL}/collections?handle=${handle}`)
1213

1314
if (!res.ok) {
14-
throw new Error(`Failed to fetch collection: ${handle}`)
15+
notFound()
1516
}
1617

1718
return res.json()
@@ -20,6 +21,10 @@ async function getCollection(handle: string) {
2021
export async function generateMetadata({ params }: Props): Promise<Metadata> {
2122
const { collection } = await getCollection(params.handle)
2223

24+
if (!collection) {
25+
notFound()
26+
}
27+
2328
return {
2429
title: `${collection.title} | Acme Store`,
2530
description: `${collection.title} collection`,

src/app/(main)/products/[handle]/page.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import medusaRequest from "@lib/medusa-fetch"
22
import ProductTemplate from "@modules/products/templates"
33
import { Metadata } from "next"
4+
import { notFound } from "next/navigation"
45

56
type Props = {
67
params: { handle: string }
@@ -14,7 +15,7 @@ async function getProducts(handle: string) {
1415
})
1516

1617
if (!res.ok) {
17-
throw new Error(`Failed to fetch product: ${handle}`)
18+
notFound()
1819
}
1920

2021
return res.body
@@ -23,6 +24,10 @@ async function getProducts(handle: string) {
2324
export async function generateMetadata({ params }: Props): Promise<Metadata> {
2425
const { products } = await getProducts(params.handle)
2526

27+
if (!products.length) {
28+
notFound()
29+
}
30+
2631
const product = products[0]
2732

2833
return {
File renamed without changes.

0 commit comments

Comments
 (0)