If you want to display images, one option is to upload them to a cloud storage before showing them, or you can use an internal Next API route to proxy image requests. Here's an example of how you can achieve this:
// pages/api/image_proxy.ts
// Remove type definitions if not using TypeScript
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { source } = req.query;
if (!source || typeof source !== "string") {
return res
.status(400)
.json({ message: "Image source missing in request-query" });
}
const rStream = (await fetch(decodeURIComponent(source)).then(
(resp) => resp.body
)) as NodeJS.ReadableStream;
if (!rStream) {
// Failed to fetch
// Options include redirecting to source URL or displaying error message
return res.status(500).json({ message: "Internal server error" });
}
const pt = new Stream.PassThrough();
stream.pipeline(rStream, pt, (err) => {
if (err) return res.status(500).json({ message: "Internal server error" });
});
pt.pipe(res);
}
Simply route all URL sources through the specified route like this:
<Image
src={`/api/image_proxy?source=${encodeURIComponent(YOUR_IMAGE_SRC_URL)}`}
/>