My goal is to create bookmarks section in NextJS application, where user can click "Add bookmark", type website address, and then favicon is fetched from that website, saved in local storage and displayed on website.
Favicon is saved in local storage properly, but when I try to display it on website it doesn't work and there are errors in console.
I tried different ways of creating image element and changing state but every time it shows similar result. I also tried different settings for CSP but without success.
In console it's either this
Security Error: Content at http://localhost:3000/ may not load data from blob:http://localhost:3000/fa614f8e-0ec6-48ef-ad52-0cce4f514d0e.
or this
GET blob:http://localhost:3000/4df97d52-2946-4389-97d5-7c734b706224 net::ERR_FILE_NOT_FOUND
File: SingleBookmark.tsx
const [faviconSource, setFaviconSource] = useState(null);
useEffect(() => {
if (!localStorage.getItem(name)) {
fetchFaviconToStorage();
} else {
setFaviconSource(JSON.parse(localStorage.getItem(name)));
}
}, []);
const fetchFaviconToStorage = () => {
const imgurl =
`https://thingproxy.freeboard.io/fetch/http://${name}/favicon.ico`;
fetch(imgurl)
.then((response) => response.blob())
.then((myBlob) => {
const imageObjectURL = URL.createObjectURL(myBlob);
localStorage.setItem(name, JSON.stringify(imageObjectURL));
setFaviconSource(imageObjectURL);
});
};
return (
<>
{faviconSource &&
<Image
loader={(props) => imgixLoader(props, { fit: "crop", ar: "1:1" })}
src={faviconSource}
/>
}
</>
);
File: next.config.js
const ContentSecurityPolicy = `
img-src 'self' data: blob:;
`;
const securityHeaders = [
{
key: "Content-Security-Policy",
value: ContentSecurityPolicy.replace(/\s{2,}/g, " ").trim(),
},
];
module.exports = {
async headers() {
return [
{
// Apply these headers to all routes in your application.
source: "/:path*",
headers: securityHeaders,
},
];
},
};