nitro: 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)

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 <quote>

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.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 12
  • Comments: 23 (7 by maintainers)

Most upvoted comments

This is blocking the adoption of Cloudflare pages for me too. Any news?

I Just encountered this bug, which only happens with the Cloudflare pages. Works well in development.

For now, we had to move to vercel. But I would really love to have a solution.

sorry hoping to get some eyes on this issue

Any updates on this?

I appreciate the debug efforts by folks, and there is even a PR up (#796, which I actually recommend closing, see below), and I don’t know how at the time of adding the preset, I didn’t notice this - I think I was testing with a simple h3 return "test" handler.

Instead of relying on error codes to check if an asset is available as static, we should take advantage of Pages’ _routes.json now that support has been added. Ref: https://developers.cloudflare.com/pages/platform/functions/routing/#creating-a-_routesjson-file

Question to @danielroe (I couldn’t find anything related in the repo): is there perhaps a function to get all declared static routes? In a nuxt app, I’d use something like

{
   "version": 1,
   "include": ["/*"],
   "exclude": ["/_nuxt/*"]
}

as _routes.json, which gets me close to the goal already (Cloudflare doesn’t charge anything for static assets) - adding it manually should in fact be a better workaround than messing with HTTP request methods and response codes.

I did a bit of digging on this, the error might be somewhere here

export async function onRequest (ctx: CFRequestContext) {
  try {
    // const asset = await env.ASSETS.fetch(request, { cacheControl: assetsCacheControl })
    const asset = await ctx.next()
    if (asset.status !== 404) { // GET returns 404; Other methods return 405
      return asset
    }
  } catch (_err) {
    // Ignore
  }

The GET method will return 404 every other method will return 405 Ultimately the execution will end up in wrangler > miniflare-dist > index.mjs > generateHandler > generateResponse and it calls this code:

    if (!request.method.match(/^(get|head)$/i)) {
      return new MethodNotAllowedResponse();
    }

That is the cause of this behavior.

Not sure what the recommended fix would be… @DaniFoldi ?

So the main issue with using __routes.json I see here is that in Nuxt, paths don’t actually coorospond to the path in the filesystem.

Consider all static assets which are under the path /_nuxt/*. In the filesystem itself, they’re under .nuxt/dist/client, so if you used __routes.json to try and serve them without getting billed (a good idea!) then they would all 404…

However the solution isn’t to stop relying on __routes.json. All Functions requests are now being billed on Cloudflare Pages, with the exception of static assets from __routes.json, so not relying on that behavior is actually making sites more expensive for users, especially compared to basically every other competitor framework (SvelteKit, SolidStart, Astro, and Remix).

I’m the author of #796. I disagree to close my PR. Because:

  1. Nitro is helping you to build a js server. It’s hard for Nitro to tell which route is static. Maybe you can write a get handler that returns the current time. And for static assets, I think it’s out of the scope of Nitro.
  2. __routes.json has a limit of 100 rules. It’s hard to reach the limit, but it means there will still be an edge case that you need to use Nitro to serve static assets.

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.