Image faled to parse src
Answered
Madeiran sardinella posted this in #help-forum
Madeiran sardinellaOP
Hello guys, just started learning NextJS and i'm following an youtube tutorial on creating a blog and I wanted to display the blog post's author profile image using the <Image> tag but I'm getting an error and I'm using next-auth wth Discord's oAuth
next.config.js
Error: Failed to parse src "4d1c8cdeb79bf9689c1b899ece04dca6" on `next/image`, if using relative image it must start with a leading slash "/" or be an absolute URL (http:// or https://)next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'cdn.discordapp.com'
}
],
}
}
module.exports = nextConfig<Image
src={post.creator.image}
alt={post.creator.name}
width={40}
height={40}
className="object-contain rounded-full"
/>const PromptSchema = new Schema({
creator: {
type: Schema.Types.ObjectId,
ref: "User",
},
prompt: {
type: String,
required: [true, "Prompt is required!"],
},
tag: {
type: String,
required: [true, "Tag is required!"],
}
});Answered by riský
you should look at the discord docs on how to format the url with the image hash: https://discord.com/developers/docs/reference#image-formatting-cdn-endpoints
12 Replies
@Madeiran sardinella Hello guys, just started learning NextJS and i'm following an youtube tutorial on creating a blog and I wanted to display the blog post's author profile image using the <Image> tag but I'm getting an error and I'm using next-auth wth Discord's oAuth
Error: Failed to parse src "4d1c8cdeb79bf9689c1b899ece04dca6" on `next/image`, if using relative image it must start with a leading slash "/" or be an absolute URL (http:// or https://)
next.config.js
json
/** @type {import('next').NextConfig} */
const nextConfig = {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'cdn.discordapp.com'
}
],
}
}
module.exports = nextConfig
jsx
<Image
src={post.creator.image}
alt={post.creator.name}
width={40}
height={40}
className="object-contain rounded-full"
/>
js
const PromptSchema = new Schema({
creator: {
type: Schema.Types.ObjectId,
ref: "User",
},
prompt: {
type: String,
required: [true, "Prompt is required!"],
},
tag: {
type: String,
required: [true, "Tag is required!"],
}
});
you should look at the discord docs on how to format the url with the image hash: https://discord.com/developers/docs/reference#image-formatting-cdn-endpoints
Answer
https://cdn.discordapp.com/avatars/user_id/user_avatar.png should be what you want
Madeiran sardinellaOP
Should I change this in the next.config.js file? Like add
pathname:in the src of Image
Madeiran sardinellaOP
Interesting, the image is showing in the navbar however and it's done the same way except the src is just
session?.user?.imagenext-auth is probably constructing the url for you there
Madeiran sardinellaOP
Oooh makes sense
Thank you
Madeiran sardinellaOP
@riský sorry for tagging, but how can I construct the url 🥲
you can use template string
something like:
https://cdn.discordapp.com/avatars/${post.creator.id}/${post.creator.image}.pngMadeiran sardinellaOP
Okay, I'll give it a try, thanks!
p.s. things in php and laravel are a whole lot different from here :D
p.s. things in php and laravel are a whole lot different from here :D