I have an admin panel in which I can add images for products. Here is the action that allows me to do this:
export const createProduct = async (state, formData) => {
const result = productSchema.safeParse(
Object.fromEntries(formData.entries())
);
console.log(result.error);
if (result.success === false) return result.error.formErrors.fieldErrors;
const {
title,
vendorCode,
image,
categoryId,
characteristics,
mainCategoryId,
price,
} = result.data;
const data = result.data;
await fs.mkdir("public/products", { recursive: true });
const imagePath = `/products/${crypto.randomUUID()}-${data.image.name}`;
await fs.writeFile(
`public${imagePath}`,
Buffer.from(await data.image.arrayBuffer())
);
const product = await db.product.create({
data: {
title,
slug: slugify(`${title}-${vendorCode}`, {
locale: "ru",
lower: true,
}),
image: imagePath,
categoryId,
vendorCode,
mainCategoryId,
price,
},
});
await db.productCharacteristic.createMany({
data: characteristics.map((char) => ({
title: char.title,
variants: char.values.map(({ value }) => value),
productId: product.id,
})),
});
revalidatePath("/");
redirect("/admin/catalog");
This example shows how I use the product image:
<Image
className="absolute top-0 left-0 w-full h-full object-cover"
src={product.image}
width={800}
height={800}
alt={product.title}
/>
It works fine in development mode, but the problem occurs on my cloud hosting in production mode. The images are taken from the /_next/image folder. When adding a new product in the admin, the image doesn’t go to /_next/image until you do an npm run build.
How to fix this so that when adding a new product, the image goes into this folder and is displayed on the site?
I tried to use minimumCacheTTL but it didn’t work
/** @type {import('next').NextConfig} */
const nextConfig = {
eslint: {
ignoreDuringBuilds: true,
},
env: {
SERVER_URL: process.env.SERVER_URL,
},
images: {
minimumCacheTTL: 60,
},
};
export default nextConfig;