If you prefer not to rely on third-party image proxy servers, you can set up your own! By hosting your own proxy server, you can access images from pixiv by simply changing the Referer to https://www.pixiv.net/.
By hosting your own pixiv image proxy server, you can have more control over the caching and access to pixiv images without relying on external services.
addEventListener("fetch",event=>{event.respondWith(handleRequest(event.request));});asyncfunctionhandleRequest(originalRequest){try{leturl=newURL(originalRequest.url);url.hostname="i.pximg.net";letmodifiedRequest=newRequest(url,originalRequest);letresponse=awaitfetch(modifiedRequest,{headers:{'Referer':'https://www.pixiv.net/','User-Agent':'Mozilla/5.0 (Windows NT 10.0; rv:122.0) Gecko/20100101 Firefox/122.0'}});if(!response.ok){returnnewResponse("Error fetching the resource.",{status:response.status});}returnresponse;}catch(error){console.error("Failed to fetch resource: ",error.message);returnnewResponse("An error occurred while fetching the resource.",{status:500});}}
asyncfunctionhandleRequest(request:Request):Promise<Response>{try{consturl=newURL(request.url);url.hostname="i.pximg.net";constmodifiedRequest=newRequest(url,request);constresponse=awaitfetch(modifiedRequest,{headers:{'Referer':'https://www.pixiv.net/','User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.3'}});if(!response.ok){returnnewResponse("Error fetching the Pixiv image.",{status:response.status});}constnewHeaders=newHeaders(response.headers);//? You can replace the origin with your own PixivFE domain if you prefer a dedicated proxy.newHeaders.set('Access-Control-Allow-Origin','*');returnnewResponse(response.body,{status:response.status,statusText:response.statusText,headers:newHeaders});}catch(error){console.error("Failed to fetch Pixiv image: ",error);returnnewResponse("An error occurred while fetching the Pixiv image.",{status:500});}}Deno.serve({port:4242},handleRequest);
If you prefer Deno Deploy for better code maintenance, you can export the code to a GitHub repo (in project settings) or just deploy a repo that contains the main.ts file and set the endpoint file.
While Deno is easy to use, there are several limitations to consider:
Free tier is far less generous than Cloudflare Workers (1,000,000 requests and 100GB bandwidth per month). So, please take a thorough consideration if you plan to use it on a large public instance.
It maybe requires GitHub account for deployment.
However, Deno may perform better in regions where Cloudflare Workers' speed is not so ideal.
For more information, you can refer to this article by pixiv.cat, which also serves as an image proxy server. You can try an example image through their proxy.