nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Nuxt 3.0.0-rc.11 with Nitro 0.5.12> /Server/api functions fail on POST requests, GET methods are OK when publishing to Cloudflare pages functions)

Open ricky11 opened this issue 1 year ago • 3 comments

Environment

Nuxt 3.0.0-rc.10-27718444.40d0907 with Nitro 0.5.2-27718085.c24dbcf
Cloudflare pages with functions

Reproduction

https://stackblitz.com/edit/github-dxg5qg?file=server/api/customer.get.js

Describe the bug

Deployment of my Nuxt 3 app that contains following folder server folder structure

/server/api/customer.post.js <- POST fail with 405 method not allowed on cloudflare pages /server/api/customer.get.js <-- GET works. /server/api/test.js <-- should response to all routes

Nitro preset : nitro: { preset: 'cloudflare_pages', },

Upon pushing to github, cloudflare initiates a page build with the following successful log entries on cf pages:


Nuxt 3.0.0-rc.11-27719556.94377b1 with Nitro 0.5.3-27719444.ff99330
--
18:54:51.303 | ℹ Client built in 2421ms
18:54:51.304 | ℹ Building server...
18:54:52.513 | ✔ Server built in 1209ms
18:54:52.572 | ✔ Generated public .output/public
18:54:52.588 | ℹ Building Nitro Server (preset: cloudflare_pages)
18:54:59.396 | ✔ Nitro server built
18:54:59.419 | └─ functions/path.js (414 kB) (132 kB gzip)
...
18:55:16.586 | Success: Assets published!
18:55:18.083 | Success: Your site was deployed!

When visiting the domain all looks good and page is published! Now time to test the /functions folder

GET https://www.domain.com/api/customer HTTP/1.1

RESULT : 200 OK

POST https://www.domain.com/api/customer HTTP/1.1

RESULT : 405 NOT ALLOWED

POST https://www.domain/api/incorrectpath HTTP/1.1

RESULT : 405 NOT ALLOWED (should return 404 NOT FOUND)

Talking to Cloudflare discord support they say

Let them know that if they want to discuss it then feel free to pop on here. Cloudflare Pages never returns 405s, so this is Nitro

  • No POST handlers work, either in the .post.js file or the .js file
  • It works in dev (so in Vite), not prod
  • The command (make sure they know the command is build not generate)

Strangely when I POST to a route that does not exist like POST https://www.domain.com/api/blah, the result is also 405 NOT ALLOWED, hence it seems all POST requests are being block before the function is actually triggered, but for some reason all GET routes are OK.

to recreate the issue

  • use the stackblitz template above : https://stackblitz.com/edit/github-dxg5qg?file=server/api/customer.get.js
  • run npm run build , not generate, as generate will not create /functions folders
  • npx wrangler pages dev .output/public OR npx wrangler pages publish .output/public
  • visit xxx.com/api/customer -- this will be a GET and it will work
  • make a postman POST request to xx.com/api/customer - it will fail with 405 (if not pls let me know)

According to the nitro docs : it should just work.

Additional context

Cf discord channel has been informed of this issue and says they would be happy to connect with nuxt core team, please contact the discord community member @isaac-mcfadyen#4321 on cloudflare-pages discord channel.

ricky11 avatar Sep 14 '22 00:09 ricky11

Workarounds to get the cf /functions folder working correctly are: -change preset to 'node-server' -change cloudflare pages build command to 'npm run generate' instead of 'npm run build' <-- this itself does not port nuxt3 /server/api functions to cf pages functions.. -recreate /functions folder in ide and write functions as per cf api : https://developers.cloudflare.com/pages/platform/functions/

  • push to github and then everything just works GET, POST etc..

Above workaround bypasses NUXT3 /server folder and replies on cloudfloare / functions.. the downside of this is portability, we are tied down to cloudflare infra and loose all nuxt /server features.

ricky11 avatar Sep 16 '22 02:09 ricky11

Upgraded to 3.0.0-rc.11 , and still have the same problem.

ricky11 avatar Sep 22 '22 09:09 ricky11

sorry hoping to get some eyes on this issue

ricky11 avatar Oct 03 '22 17:10 ricky11