h3
h3 copied to clipboard
Documentation/example of how to run h3 in a worker environment
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) => {
// ???
})
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:
- First, we need to convert an h3 app
(req, res)
into abstract function without depending on node internals (using unenv/createCall - Then we need to use caller function to implement cloudflare API (see nitro entry for cloudflare)
@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 thanks for this, helped me out a lot!
@pi0 can we expect docs anytime soon on this?
Pending until we have full Response/Request support #401
Workers support with h3 itself will be much more straight forward with upcoming fetch adapter. Please track via #458