serverless-nextjs / serverless-next.js

⚡ Deploy your Next.js apps on AWS Lambda@Edge via Serverless Components
MIT License
4.47k stars 457 forks source link

getServerSideProps not working #1226

Closed mikias21 closed 3 years ago

mikias21 commented 3 years ago

Hello there,

I was trying to make api request to tmdb on a next js project, using the getServerSideProps function. How ever everytime i try to make request it show time out error. I thought maybe its because of my url string or API KEY, but the same request string works fine on postman. Could you please help me to fix it.

// My code export async function getServerSideProps(context) { const genre = context.query.genre; const request = await fetch( https://api.themoviedb.org/3${ requests[genre]?.url || requests.fetchTreading.url } ).then((res) => res.json()); return { props: { results: request.results, }, };

// The error FetchError: request to https://api.themoviedb.org/3/discover/movie?api_key=API_KEY&with_genres=28 failed, reason: connect ETIMEDOUT 31.13.91.6:443 at ClientRequest. (C:\Users\mikia\Desktop\hulu-clone\node_modules\node-fetch\lib\index.js:1461:11) at ClientRequest.emit (events.js:315:20) at TLSSocket.socketErrorListener (_http_client.js:469:9) at TLSSocket.emit (events.js:315:20) at emitErrorNT (internal/streams/destroy.js:106:8) at emitErrorCloseNT (internal/streams/destroy.js:74:3) at processTicksAndRejections (internal/process/task_queues.js:80:21) { type: 'system', errno: 'ETIMEDOUT', code: 'ETIMEDOUT' }

dphang commented 3 years ago

Hm, I don't know what their policy is as I don't use that at all, but if it's working locally, I feel that most likely themoviedb.org might think your app is a bot and is blocking connections from AWS datacenter IPs (which is where Lambda@Edge code runs on). Probably you need to check their policies and whether there is some IP range you need to allow, or you could use a proxy to make the connection? (Not legal advice, please do your own research).

mikias21 commented 3 years ago

@dphang Thanks a lot, I checked some tutorials and I saw that they are all using it locally. I am using Psiphon VPN do you think that's the problem. I am in china and I can't access the service with out VPN but postman works fine.

dphang commented 3 years ago

Oh that could be a possibility, that is common across many APIs (to prevent bots). I did actually miss that you are running Next.js locally on Windows? So both of the requests go through the VPN on your local machine, but just Postman works?

Not sure but maybe the difference is Next.js/fetch is setting some headers that make it look like a bot. You'd have to debug and compare the requests (you can follow https://nextjs.org/docs/advanced-features/debugging) and/or use Fiddler or other network debugging tools to see how the requests are being made.

mikias21 commented 3 years ago

Thank you very much, I will try that and hope it works. Thank you for your kind help

On Tue, Jun 15, 2021 at 11:55 AM Daniel Phang @.***> wrote:

Oh that could be a possibility, that is common across many APIs (to prevent bots). I did actually miss that you are running Next.js locally on Windows? So both of the requests go through the VPN, but just Postman works?

Not sure but maybe the difference is Next.js is setting some headers that make it look like a bot. You'd have to debug the requests in your IDE and/or use Fiddler or other network debugging tools to see how the requests are being made.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/serverless-nextjs/serverless-next.js/issues/1226#issuecomment-861752520, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJOBECLX67GIURKPIL3Q6VDTS6OYTANCNFSM46XX4DGQ .

dphang commented 3 years ago

Thanks, I will close this then since it's not exactly related to this component/AWS. Feel free to recomment if there are issues with this component itself.