h3 icon indicating copy to clipboard operation
h3 copied to clipboard

Documentation/example of how to run h3 in a worker environment

Open JamieCurnow opened this issue 2 years ago • 3 comments

I'm trying to get started with h3 in a Cloudflare worker - are there any examples of how to get started with h3 in workers?

import { createApp } from 'h3'

const app = createApp()
app.use('/', () => 'Hello world')

addEventListener('fetch', (e) => {
  // ???
})

JamieCurnow avatar Oct 13 '21 16:10 JamieCurnow

Hi @JamieCurnow. It is not straightforward at the moment without using nuxt nitro but would definitely work on docs to explain usage without nuxt nitro.

In the meantime, here are overall steps hopefully can help you to implement:

pi0 avatar Oct 13 '21 16:10 pi0

@pi0 Thank you for pointing me in the right direction! You're right - it's not straightforward at the moment 😅

I managed to figure it out though and made a little helper package that wraps around h3's createApp and exposes a function to handle the fetch event from Cloudflare.

In case anyone else is trying to do this too check out: https://github.com/JamieCurnow/h3-worker#readme

Needs a good test, but is working for my use case. Suggestions/questions/pull requests are welcome as always :)

JamieCurnow avatar Oct 30 '21 11:10 JamieCurnow

@JamieCurnow thanks for this, helped me out a lot!

jontybrook avatar Jan 20 '22 18:01 jontybrook

@pi0 can we expect docs anytime soon on this?

renhiyama avatar Dec 02 '22 01:12 renhiyama

Pending until we have full Response/Request support #401

Hebilicious avatar Jul 04 '23 22:07 Hebilicious

Workers support with h3 itself will be much more straight forward with upcoming fetch adapter. Please track via #458

pi0 avatar Jul 27 '23 16:07 pi0