workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

🐛 BUG: Cloudflare pages not deploying functions

Open jaystacey opened this issue 2 years ago • 5 comments

What version of Wrangler are you using?

2

What operating system are you using?

Cloudflare Pages

Describe the Bug

Wrangler is not detecting functions routes, when deploying with Cloudflare Pgaes from github repo.

output below, when '/functions/api/contact.ts' function file exists.

11:19:55.381 🚧 'wrangler pages ' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
11:19:55.389  
11:19:55.490 [31m✘ [41;31m[[41;97mERROR[41;31m][0m [1mNo routes found when building Functions directory: /opt/buildhome/repo/functions[0m
11:19:55.491  
11:19:55.491  
11:19:55.491 [32mIf you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose[0m
11:19:55.502 Warning: Wrangler did not find routes when building functions. Skipping.

jaystacey avatar Sep 15 '22 01:09 jaystacey

What does your functions/api/contact.ts file look like? I suspect you aren't correctly exporting any onRequest* handlers.

GregBrimble avatar Sep 15 '22 14:09 GregBrimble

contact.ts exports a onRequestPost() function, previously been working however after a recent merge (not relating to that file) wrangler no longer detects any functions routes.

I did a test with a 'test.js' function at the base functions directory (/functions/test.js) using example code:

export async function onRequest() { return new Response("Hello, world!"); }

and it doens't detect that as a route either...

also the garbled log output.

image

jaystacey avatar Sep 16 '22 00:09 jaystacey

Same problem but deployed from Gitlab repo (not really significant I suppose) image

A previous commit works with the same code in /functions directory. If I redeploy this working commit all is fine.

chri70 avatar Sep 16 '22 06:09 chri70

Do you have a link to the repository (or one that is similarly configured) and copy of your root directory/build command/output directory settings?

Note that the functions/ directory should be in the root directory of your repo (usually that's just the top-level folder).

GregBrimble avatar Sep 21 '22 14:09 GregBrimble

I will send you the complete directory. If you want I can invite you to the Gitlab repo

chri70 avatar Sep 21 '22 15:09 chri70

Any news about this problem ? @jaystacey I see in you deploy log "You can now deploy .output/public to any static hosting!", is it a Nuxt 3 project ?

chri70 avatar Oct 04 '22 16:10 chri70

yep, using nuxt3 3.0.0-rc.10, coincidence that we are both using nuxt and facing this issue?

jaystacey avatar Oct 05 '22 00:10 jaystacey

I'm using nuxt3 3.0.0-rc.11. I made a little test by generating with nuxi generate in my local environment, then add .output/public folder to my git repo, I used npm ci instead of nuxt genreate in the build script in CF deployment settings to get dependencies, and guess what ... the scripts of functions folder was correctly compiled and the routes are available.

chri70 avatar Oct 05 '22 10:10 chri70

I restore the generation on CF side, and I list files of functions folder before and after generation. Before nuxt generate (----ls-before line in log), functions folder is ok, but after generation it does not exist anymore (----ls-after line in log). I think it's not a wrangler problem but a Nuxt or Nitro issue instead

<html>
<body>
<!--StartFragment-->

16:14:24.368 | > cfgenerate
-- | --
16:14:24.368 | > echo ----ls-before && ls functions -l -R && nuxt generate && echo ----ls-after && ls functions -l -R
16:14:24.368 |  
16:14:24.386 | ----ls-before
16:14:24.399 | functions:
16:14:24.399 | total 5
16:14:24.399 | drwxr-xr-x 2 buildbot nogroup 4096 Oct  5 14:13 api
16:14:24.399 | -rw-r--r-- 1 buildbot nogroup  158 Oct  5 14:13 tsconfig.json
16:14:24.399 |  
16:14:24.399 | functions/api:
16:14:24.400 | total 5
16:14:24.400 | -rw-r--r-- 1 buildbot nogroup 3729 Oct  5 14:13 contact.ts
16:14:24.400 | -rw-r--r-- 1 buildbot nogroup  325 Oct  5 14:13 index.ts
16:14:24.499 | Nuxi 3.0.0-rc.11
16:14:24.519 | Nuxt 3.0.0-rc.11 with Nitro 0.5.4
16:14:31.237 | ℹ Client built in 3614ms
16:14:31.239 | ℹ Building server...
16:14:32.888 | ✔ Server built in 1649ms
16:14:32.953 | ✔ Generated public .output/public
16:14:32.954 | ℹ Initializing prerenderer
16:14:34.029 | ℹ Prerendering 3 initial routes with crawler
16:14:34.169 | ├─ / (140ms)
16:14:34.180 | ├─ /mentions-legales (11ms)
16:14:34.183 | ├─ /200.html (2ms)
16:14:34.184 | ├─ /_payload.js (1ms)
16:14:34.186 | ├─ /mentions-legales/_payload.js (2ms)
16:14:34.188 | ✔ You can now deploy .output/public to any static hosting!
16:14:34.209 | ----ls-after
16:14:34.223 | functions:
16:14:34.224 | total 0
16:14:34.237 | Finished
16:14:34.817 | 🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
16:14:34.825 |  
16:14:34.929 | [31m✘ [41;31m[[41;97mERROR[41;31m][0m [1mNo routes found when building Functions directory: /opt/buildhome/repo/functions[0m

<!--EndFragment-->
</body>
</html>

chri70 avatar Oct 05 '22 14:10 chri70

Finally, Nitro seems to detect the deployment environment (see Zero-Config Providers doc), and select the right preset (in this case cloudflare_pages I presume). In the Nitro clouldflare.ts, we can see :

...
output: {
    serverDir: '{{ rootDir }}/functions'
},
...

The server directory /functions folder is emptied before each Nitro prerender. I suppose that Nuxt 3 /server folder should be used as source of functions folder but there's some issues.

I fixed the problem by setting the environment variable NITRO_PRESET=node-server in CF settings.

chri70 avatar Oct 06 '22 08:10 chri70

@chri70 great find, can confirm that NITRO_PRESET=node-server resolves issue with no obvious drawbacks.

jaystacey avatar Oct 06 '22 11:10 jaystacey

@chri70 great find, can confirm that NITRO_PRESET=node-server resolves issue with no obvious drawbacks.

@jaystacey seems fine, but if that does not work with the preset "cloudflare_pages" I'd consider the bug still unresolved - so let's re-open this?

fago avatar Oct 10 '22 20:10 fago

uhm, this should be a nitro bug though

fago avatar Oct 10 '22 20:10 fago

Hi, sorry to jump on an old issue, but I had this same problem and came across a different solution. I thought it would be worth sharing for anyone else stumbling across this topic.

Rather than setting the envrionment variable, I added the following to my nuxt.config.ts:

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  nitro: {
    preset: "cloudflare_pages",
  },
});

Also, it's important that you run nuxt build rather than nuxt generate. When you choose the Nuxt preset, it chooses npm run generate as the default build command and /dist as the output directory.

After setting the build command to yarn build and the output directory to .output/public and adding the config above, it built successfully and I could see the SSR activity thanks to the new real-time logs.

luc122c avatar Nov 23 '22 17:11 luc122c

@luc122c

Also, it's important that you run nuxt build rather than nuxt generate. When you choose the Nuxt preset, it chooses npm run generate as the default build command and /dist as the output directory.

Unfortunately that does not help those of us that that DO NOT want SSR, but still want the Cloudflare functions. With is preset you should be able to do either a SPA or SSR. nuxt build is for SSR and nuxt generate is for static/SPA. Currently it will not create the Cloudflare functions if you use nuxt generate

PizzaConsole avatar Nov 24 '22 06:11 PizzaConsole

@luc122c You could however use the prerender config to generate the static html and then run nuxt build, works for me because I only have one page... would not be great with 10+

	nitro: {
		preset: 'cloudflare-pages',
		prerender: {
			ignore: ['/404'],
			routes: ['/']
		},
		serveStatic: true
	}

Plus, this bug still needs to get fixed for server routes to work properly... https://github.com/unjs/nitro/issues/497

PizzaConsole avatar Nov 25 '22 03:11 PizzaConsole

(Edit: Forgot to add that I intended to ask @luc122c about this - thanks for any input!)

I am a bit confused by this issue: Is the intended behaviour that both the native /functions folder from Cloudflare Pages and the /server/api/ folder from Nuxt 3 are supposed to work in parallel. Or will the /server folder from Nuxt be "transformed" into the Cloudflare Pages /functions folder?

By setting the build step to nuxt build && ls functions and setting the Nitro preset to "cloudflare_pages" I have success with putting my API functions in /server/api, and I can see that the functions folder is then created and populated with two files during deployment on Cloudflare Pages:

03:19:44.892 | functions:
03:19:44.892 | total 702
03:19:44.892 | -rw-r--r-- 1 buildbot nogroup 305165 Feb 26 02:19 [[path]].js
03:19:44.893 | -rw-r--r-- 1 buildbot nogroup 412466 Feb 26 02:19 [[path]].js.map

However, I cannot get it to work by simply creating the /functions folder in my Nuxt project and adding a HelloWorld function as described on https://developers.cloudflare.com/pages/platform/functions/get-started/ - when I deploy, I get this:

03:03:00.842 | Found Functions directory at /functions. Uploading.
03:03:01.419 |  
03:03:01.523 | ✘ [ERROR] No routes found when building Functions directory: /opt/buildhome/repo/functions
03:03:01.523 |  
03:03:01.523 |  
03:03:01.524 | If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose
03:03:01.535 | Warning: Wrangler did not find routes when building functions. Skipping.

I am mostly curious if I am misunderstanding how this is supposed to work. Thanks in advance!

cjsinnbeck avatar Feb 26 '23 02:02 cjsinnbeck

I am facing this error. Any solution yet?

trapcodeio avatar May 20 '23 18:05 trapcodeio

Appears that there is mroe to this issue, reopening. @trapcodeio can you please post nuxt version and reproduction code.

jaystacey avatar May 20 '23 23:05 jaystacey

I can do nothing...

 ⛅️ wrangler 3.0.1
------------------
wrangler dev now uses local mode by default, powered by 🔥 Miniflare and 👷 workerd.
To run an edge preview session for your Worker, use wrangler dev --remote
⎔ Starting local server...
service core:user:worker: Uncaught TypeError: Cannot read properties of undefined (reading 'split')
  at ivsvnonyb4d.js:4962:32
✘ [ERROR] MiniflareCoreError [ERR_RUNTIME_FAILURE]: The Workers runtime failed to start. There is likely additional logging output above.

Skmt3P avatar Jun 02 '23 16:06 Skmt3P

(Edit: Forgot to add that I intended to ask @luc122c about this - thanks for any input!)

I am a bit confused by this issue: Is the intended behaviour that both the native /functions folder from Cloudflare Pages and the /server/api/ folder from Nuxt 3 are supposed to work in parallel. Or will the /server folder from Nuxt be "transformed" into the Cloudflare Pages /functions folder?

By setting the build step to nuxt build && ls functions and setting the Nitro preset to "cloudflare_pages" I have success with putting my API functions in /server/api, and I can see that the functions folder is then created and populated with two files during deployment on Cloudflare Pages:

03:19:44.892 | functions:
03:19:44.892 | total 702
03:19:44.892 | -rw-r--r-- 1 buildbot nogroup 305165 Feb 26 02:19 [[path]].js
03:19:44.893 | -rw-r--r-- 1 buildbot nogroup 412466 Feb 26 02:19 [[path]].js.map

However, I cannot get it to work by simply creating the /functions folder in my Nuxt project and adding a HelloWorld function as described on https://developers.cloudflare.com/pages/platform/functions/get-started/ - when I deploy, I get this:

03:03:00.842 | Found Functions directory at /functions. Uploading.
03:03:01.419 |  
03:03:01.523 | ✘ [ERROR] No routes found when building Functions directory: /opt/buildhome/repo/functions
03:03:01.523 |  
03:03:01.523 |  
03:03:01.524 | If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose
03:03:01.535 | Warning: Wrangler did not find routes when building functions. Skipping.

I am mostly curious if I am misunderstanding how this is supposed to work. Thanks in advance!

Would really like some clarity regarding this as well.

jadechip avatar Jun 04 '23 14:06 jadechip

hi folks, can anyone confirm whether you're still experiencing this issue with latest wrangler version? And if so, a repro repository would be much appreciated. Thank you <3

CarmenPopoviciu avatar Apr 26 '24 15:04 CarmenPopoviciu