Skip to content
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

Open
imdineshkumar13 opened this issue Jun 13, 2024 · 5 comments
Open

Cross Origin issue occuring #466

imdineshkumar13 opened this issue Jun 13, 2024 · 5 comments

Comments

@imdineshkumar13
Copy link

imdineshkumar13 commented Jun 13, 2024

Screenshot 2024-06-13 at 6 45 00 PM Screenshot 2024-06-13 at 6 46 12 PM
@biiibooo
Copy link
Contributor

biiibooo bot commented Jun 13, 2024

👋 @imdineshkumar13

Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you include steps to reproduce it.
To help make it easier for us to investigate your issue, please follow the contributing guidelines.

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.

@imdineshkumar13
Copy link
Author

👋 @imdineshkumar13

Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you include steps to reproduce it. To help make it easier for us to investigate your issue, please follow the contributing guidelines.

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

@cyyjs
Copy link

cyyjs commented Aug 12, 2024

useCORS is not mentioned in the documentation

@cyyjs
Copy link

cyyjs commented Aug 12, 2024

可以通过filter参数,修改图片的src为代理地址,绕过跨域问题:
代码示例:

const blob = await toBlob(el, {
    filter:(node) => {
      if (node.nodeName === 'IMG') {
        node.src = `/proxy?uri=${encodeURIComponent(node.src)}`
      }
      return true
    }
})

自己在服务端实现/proxy接口,来响应图片请求

@SimRunBot
Copy link

SimRunBot commented Aug 15, 2024

in case you are lost in this CORS error depth ,
The only thing that worked for me was translating the other comments and implementing the proxy api route from my own nextjs application
the code is the following

// 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants