Skip to content

Commit

Permalink
fix: file not found error
Browse files Browse the repository at this point in the history
  • Loading branch information
sadmann7 committed Apr 11, 2024
1 parent 148cd5c commit cfc65d5
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 3 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"format:check": "prettier --check \"**/*.{ts,tsx,mdx}\" --cache",
"check": "pnpm lint && pnpm typecheck && pnpm format:check",
"shadcn:add": "pnpm dlx shadcn-ui@latest add",
"db:generate": "dotenv drizzle-kit generate:pg",
"db:introspect": "dotenv drizzle-kit introspect:pg",
"db:push": "dotenv drizzle-kit push:pg",
"db:migrate": "dotenv tsx src/db/migrate.ts",
Expand Down
139 changes: 139 additions & 0 deletions src/app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { type MetadataRoute } from "next"
import { db } from "@/db"
import { categories, products, stores, subcategories } from "@/db/schema"
import { allPages, allPosts } from "contentlayer/generated"
import { count, desc, eq, sql } from "drizzle-orm"

import { absoluteUrl } from "@/lib/utils"

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
async function getAllStores() {
try {
return await db
.select({
id: stores.id,
})
.from(stores)
.leftJoin(products, eq(products.storeId, stores.id))
// Google's limit is 50,000 URLs per sitemap
.limit(50000)
.offset(0)
.groupBy(stores.id)
.orderBy(desc(stores.active), desc(sql<number>`count(*)`))
} catch (err) {
return []
}
}

const storesRoutes = (await getAllStores()).map((store) => ({
url: absoluteUrl(`/products?store_ids=${store.id}`),
lastModified: new Date().toISOString(),
}))

async function getAllProducts() {
try {
return await db
.select({
id: products.id,
})
.from(products)
.leftJoin(stores, eq(products.storeId, stores.id))
// Google's limit is 50,000 URLs per sitemap
.limit(50000)
.offset(0)
.groupBy(products.id)
.orderBy(
desc(count(stores.stripeAccountId)),
desc(count(products.images)),
desc(products.createdAt)
)
} catch (err) {
return []
}
}

const productsRoutes = (await getAllProducts()).map((product) => ({
url: absoluteUrl(`/product/${product.id}`),
lastModified: new Date().toISOString(),
}))

async function getAllCategories() {
try {
return await db
.selectDistinct({
id: categories.id,
name: categories.name,
slug: categories.slug,
description: categories.description,
})
.from(categories)
.orderBy(desc(categories.name))
} catch (err) {
return []
}
}

const categoriesRoutes = (await getAllCategories()).map((category) => ({
url: absoluteUrl(`/collections/${category.slug}`),
lastModified: new Date().toISOString(),
}))

async function getAllSubcategories() {
try {
return await db
.selectDistinct({
id: subcategories.id,
name: subcategories.name,
slug: subcategories.slug,
description: subcategories.description,
})
.from(subcategories)
} catch (err) {
return []
}
}

const subcategoriesRoutes = (await getAllSubcategories())
.map((s) =>
categoriesRoutes.map((c) => ({
url: absoluteUrl(`/collections/${c.url.split("/").pop()}/${s.slug}`),
lastModified: new Date().toISOString(),
}))
)
.flat()

const pagesRoutes = allPages.map((page) => ({
url: absoluteUrl(page.slug),
lastModified: new Date().toISOString(),
}))

const postsRoutes = allPosts.map((post) => ({
url: absoluteUrl(post.slug),
lastModified: new Date().toISOString(),
}))

const routes = [
"",
"/products",
"/stores",
"/build-a-board",
"/blog",
"/dashboard/account",
"/dashboard/stores",
"/dashboard/billing",
"/dashboard/purchases",
].map((route) => ({
url: absoluteUrl(route),
lastModified: new Date().toISOString(),
}))

return [
...routes,
...storesRoutes,
...productsRoutes,
...categoriesRoutes,
...subcategoriesRoutes,
...pagesRoutes,
...postsRoutes,
]
}
12 changes: 10 additions & 2 deletions src/lib/validations/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export const addProductSchema = z.object({
message: "Must be a valid price",
}),
inventory: z.number(),
images: z.array(z.instanceof(File)).optional().nullable().default(null),
images: z
.custom<File[] | undefined | null>()
.optional()
.nullable()
.default(null),
})

export const updateProductSchema = z.object({
Expand All @@ -26,7 +30,11 @@ export const updateProductSchema = z.object({
message: "Must be a valid price",
}),
inventory: z.number(),
images: z.array(z.instanceof(File)).optional().nullable().default(null),
images: z
.custom<File[] | undefined | null>()
.optional()
.nullable()
.default(null),
})

export const filterProductsSchema = z.object({
Expand Down

0 comments on commit cfc65d5

Please sign in to comment.