我有以下用例场景。我有一个网络 worker ,我需要在其中获取位于 NextJS public
文件夹中的图像,以便将其转换为 blob。
现在执行 fetch('public/images/myImage.png');
或 fetch('/images/myImage.png');
会导致错误:
Error: TypeError: Failed to execute 'fetch' on 'WorkerGlobalScope': Failed to parse URL from /images/ui/background_fire.jpg
所以我认为它没有像图像的 src
那样被正确解析?
最佳答案
@NasiruddinSaiyed 的答案有点过时,所以这是 2021 年的答案:
NextJS server-side-polyfills docs
Server-Side Polyfills
In addition to fetch() on the client side, Next.js polyfills fetch() in the Node.js environment. You can use fetch() on your server code (such as getStaticProps) without using polyfills such as isomorphic-unfetch or node-fetch.
所以它应该开箱即用
关于javascript - 如何从 NextJS 中的公共(public)文件夹中 fetch() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59049454/