trpc-nuxt icon indicating copy to clipboard operation
trpc-nuxt copied to clipboard

Not working on Cloudflare Workers

Open kansson opened this issue 1 year ago • 5 comments

It's not possible to run this when deployed on Cloudflare Workers. I'm getting the error global is not defined. Might have something to do with how the fetch workers there. Maybe this two repos could be of help to solve the problem.

https://github.com/esamattis/trpc-cloudflare-worker https://github.com/valu-digital/npm-packages/tree/master/packages/trpc-fetch-api-adapter

kansson avatar Jul 15 '22 15:07 kansson

Need to polyfill the global object

wobsoriano avatar Jul 15 '22 15:07 wobsoriano

Need to polyfill the global object

Thanks got it working using https://www.npmjs.com/package/@ungap/global-this.

Now I'm having another problem with CORS. The endpoint can be called directly (example /trpc/hello) but not with useAsyncQuery in a page. Is this a limitation of Workers or how should I add the correct headers for this?

Thank you for the help!

kansson avatar Jul 15 '22 18:07 kansson

Actually I'm dumb.. the worker tried calling the localhost trpc server in production. Everything works now, thanks for the awesome Nuxt TRPC implementation!

kansson avatar Jul 15 '22 18:07 kansson

@wobsoriano Do have time for another question regarding running on Cloudflare Workers?

It seems that using useAsyncQuery doesn't run on the server during SSR. Instead it runs on the client side and there is not data in the initial HTML sent from the server. Note that everything is working as expected when using the node preset.

kansson avatar Jul 16 '22 12:07 kansson

@wobsoriano hi, do you have any update on my second issue?

kansson avatar Jul 23 '22 23:07 kansson

Hey, just released v3. Probably fixes your issue @essarn

wobsoriano avatar Sep 24 '22 15:09 wobsoriano

Yes it should work, looking forward to using the library with v10 support.

kansson avatar Sep 24 '22 15:09 kansson