-
Notifications
You must be signed in to change notification settings - Fork 555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cross Origin issue occuring #466
Comments
Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you include steps to reproduce it. We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can. |
I used htmlToImage npm. whenever I tried to get the asset url from the UnLayer, it threw CORS origin issue, I wasn't able to add the useCORS: true, in the parameter |
|
可以通过 const blob = await toBlob(el, {
filter:(node) => {
if (node.nodeName === 'IMG') {
node.src = `/proxy?uri=${encodeURIComponent(node.src)}`
}
return true
}
}) 自己在服务端实现 |
in case you are lost in this CORS error depth , // app/api/proxy-image/route.ts
import { NextRequest, NextResponse } from "next/server";
export async function GET(request: NextRequest) {
const url = request.nextUrl.searchParams.get("url");
if (!url) {
return NextResponse.json(
{ error: "Missing URL parameter" },
{ status: 400 }
);
}
try {
const imageResponse = await fetch(url);
if (!imageResponse.ok) {
return NextResponse.json(
{ error: "Failed to fetch image" },
{ status: imageResponse.status }
);
}
const contentType = imageResponse.headers.get("Content-Type");
const arrayBuffer = await imageResponse.arrayBuffer();
return new NextResponse(arrayBuffer, {
headers: {
"Content-Type": contentType || "application/octet-stream",
"Cache-Control": "public, max-age=3600",
},
});
} catch (error) {
console.error("Error proxying image:", error);
return NextResponse.json(
{ error: "Internal server error" },
{ status: 500 }
);
}
} then i went ahead and filtered all the img tags and reset their src during the toPng function call like so: toPng(node, {
includeQueryParams: true,
filter: (node) => {
if (node.nodeName === "IMG") {
(node as HTMLImageElement).src = `/api/proxy-image?url=${
(node as HTMLImageElement).src
}`;
}
return true;
}, ...
```
hope this helps anyone stumbling on this issue like i did for the last couple of hours of frustrating debugging |
The text was updated successfully, but these errors were encountered: