Skip to content

API route as Netlify function 502s #296

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

Closed
omarryhan opened this issue May 12, 2021 · 25 comments
Closed

API route as Netlify function 502s #296

omarryhan opened this issue May 12, 2021 · 25 comments
Labels
priority: high type: bug code to address defects in shipped code

Comments

@omarryhan
Copy link

omarryhan commented May 12, 2021

Describe the bug
Plugin internal error when deploying a serverless Next.js app on Netlify.
The error is: Error: No static assets found in .next dist (aka no /.next/static).

Full error message:

Plugin "@netlify/plugin-nextjs" internal error

Error: No static assets found in .next dist (aka no /.next/static). Please check your project configuration. Your next.config.js must be one of serverless or experimental-serverless-trace. Your build command should include next build.

In "onBuild" event in "@netlify/plugin-nextjs" from Netlify app
at copyNextAssets (/opt/build/repo/.netlify/plugins/node_modules/@netlify/plugin-nextjs/src/lib/steps/copyNextAssets.js:14:11)
at async build (/opt/build/repo/.netlify/plugins/node_modules/@netlify/plugin-nextjs/src/index.js:25:3)
at async nextOnNetlify (/opt/build/repo/.netlify/plugins/node_modules/@netlify/plugin-nextjs/src/index.js:75:5)
at async onBuild (/opt/build/repo/.netlify/plugins/node_modules/@netlify/plugin-nextjs/index.js:67:5)
at async Object.run (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins/child/run.js:18:3)
at async handleEvent (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins/child/main.js:36:38)
at async process. (/opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins/ipc.js:103:16)

To Reproduce
I first did nothing but install the plugin from Netlify's UI.
Then it showed me an info message in the deployment logs that I should set the target property in next.config.js to be either serverless or experimental-serverless-trace. I set it to serverless.
It then showed me the error posted above.

Link to the app: https://github.com/omarryhan/trendzz/
Link to the failed deployment: https://app.netlify.com/sites/trendzz/deploys/609b8568b7933100072efaad

Expected behavior
Deploy without an error.

Versions

  • Next.js: ^10.2.0
@vitorcamachoo
Copy link

Please check this solution: #115 (comment)

@omarryhan
Copy link
Author

Sorry, not sure what this has to do with nx.
Also, his build dir is set to "dist/apps/basic". Mine is the default, so it should work without manually changing the build dir.

@lindsaylevine lindsaylevine added priority: high type: bug code to address defects in shipped code labels May 12, 2021
@lindsaylevine
Copy link

@omarryhan do you have a build command set in the UI? i see the github repo you linked doesnt have a netlify.toml aka the other place where it could be set, like so:

[build]
  command = "npm run build"

@vitorcamachoo
Copy link

@omarryhan do you have a build command set in the UI? i see the github repo you linked doesnt have a netlify.toml aka the other place where it could be set, like so:

[build]
  command = "npm run build"

Under a NX workspace, how I should define build command?

@lindsaylevine
Copy link

@vitorcamachoo under the article you linked on your own issue (we should prob move this conversation there):
(https://www.netlify.com/blog/2020/04/21/deploying-nx-monorepos-to-netlify/)
Screen Shot 2021-05-12 at 7 15 06 PM

@omarryhan
Copy link
Author

omarryhan commented May 13, 2021

Do you have a build command set in the UI?

No @lindsaylevine. The only thing I have set in the UI is that I added the plugin.

Screenshot_20210513-061730__01.jpg

i see the github repo you linked doesnt have a netlify.toml

There used to be one, but I removed it.

I thought npm run build is ran by default, or atleast added by the Next.js essential plugin. I'll try adding it now.

@omarryhan
Copy link
Author

omarryhan commented May 13, 2021

I added it. Now the static files are deployed correctly but the functions aren't.

Link to the most recent deployment: https://app.netlify.com/sites/trendzz/deploys/609ca9a36003565c0101970a

Link to the website: https://trendzz.netlify.app

@lindsaylevine
Copy link

@omarryhan ah that should fix it for you! i'd love feedback for our docs/README about how to make it clearer that every netlify site needs a build command specified somewhere!

@lindsaylevine
Copy link

oh oops i didn't see your second comment one sec

@lindsaylevine
Copy link

i'm confused what exactly is the problem with your functions?

@omarryhan
Copy link
Author

This is from my dev console:

image
image

I'm confused too. Not sure what's causing the Bad Gateway error. Maybe because the Netlify's Edge servers aren't actually running the function?

The same function runs fine on Vercel (https://trendzz.vercel.app/api/repositories?language=)

@omarryhan
Copy link
Author

@omarryhan ah that should fix it for you! i'd love feedback for our docs/README about how to make it clearer that every netlify site needs a build command specified somewhere!

@lindsaylevine I think it'd be better if users didn't need to to manually add the build command in either the UI or netlify.toml. However, I'll be happy to make a PR highlighting this caveat :)

@lindsaylevine
Copy link

do you see the function in your site's functions tab? if so what do the logs say? :)

@lindsaylevine
Copy link

quickest way for me to diagnose is to set up a repo that reproduces it tho!

@omarryhan
Copy link
Author

Yes, it's there. It also picked up my request when I tried to trigger a function call from the website.

Here are the logs:

7:55:21 AM: ca9141fc INFO   [request] /api/repositories
7:55:22 AM: ca9141fc Duration: 1063.07 ms	

And this is the endpoint: https://trendzz.netlify.app/.netlify/functions/next_api_repositories

@omarryhan
Copy link
Author

Screenshot_20210513-080042__01.jpg

@lindsaylevine
Copy link

can you get me a repo that reproduces this? i have no idea why this'd work on vercel but effectively time out on netlify 🤔

@omarryhan
Copy link
Author

The repo is open source: https://github.com/omarryhan/trendzz

Or would you like me to recreate the issue in a new repo?

@lindsaylevine
Copy link

omg it was in the original post hahaha sorry i need to sleep lmao. will look asap!!!

@omarryhan
Copy link
Author

I have no idea why this'd work on vercel but effectively time out on netlify

I speculate that the problem has to do with how the functions are being deployed by Netlify. Netlify is probably expecting the functions to be in the functions dir instead of the app/api dir.

Should I try and see if changing that fixes it?

@omarryhan
Copy link
Author

No problem 😄. Tyt

@lindsaylevine
Copy link

@omarryhan if you take out the res.end() in api/repositories does it work for you? 🤔

@lindsaylevine
Copy link

i can confirm now this definitely works for me. why? i'm not quite sure at the moment 😄 but definitely something to do with our compat layer and possibly related to #190

@lindsaylevine lindsaylevine changed the title No static assets found in .next dist (aka no /.next/static) API route as Netlify function 502s May 13, 2021
@omarryhan
Copy link
Author

It worked!
I was thinking there's no way I could've figured this bug out. Then I looked at your commit history in your fork 😄. You almost stripped the whole repo out.
Kudos to you.

Thanks a lot for the help.

@lindsaylevine
Copy link

@omarryhan hahah i was sure it was something unique to your config but nope! hope you enjoyed my commit names :D. such a random thing. if/when i figure out the res.end weirdness ill try to remember to report back here. thanks for your patience and help!!

serhalp pushed a commit that referenced this issue Apr 5, 2024
* chore: cache fixtures

* chore: also rebuild on changes to prepare.mjs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: high type: bug code to address defects in shipped code
Projects
None yet
Development

No branches or pull requests

3 participants