edge-runtime icon indicating copy to clipboard operation
edge-runtime copied to clipboard

Expose a method for hot reloading

Open gffuma opened this issue 2 years ago • 0 comments

I think having a public method to expose "hot reloading" is useful. Generally speaking this kind of sandboxes are great to implement great developer experience where the developer simply write code and have it executed and hot reloaded in the same "context" of an edge runtime.

So i think edge runtime is missing a method to hot reload the developer code.

Now i'm using this workaround but rely on internals and isn't so good:

import { EdgeRuntime, runServer } from 'edge-runtime'

async function main() {
  const initialCode = `
addEventListener('fetch', event => {
  console.log('Call Fetch')
  return event.respondWith(new Response('Hello'))
})`

  const runtime = new EdgeRuntime({ initialCode })

  const server = await runServer({ runtime, port: 9000 })
  console.log('Listeing on', server.url)

  let i = 0
  // In real world this is an hook from a compilation step
  setInterval(() => {
    // WORKAROUND
    runtime.evaluate(`
      delete self.__listeners['fetch'];
    `)
    runtime.evaluate(`
      addEventListener('fetch', event => {
        console.log('Call Fetch ' + ${i})
        return event.respondWith(new Response('Hello ${i}'))
      })`)
    i++
  }, 1000)
}

main().catch((err) => {
  console.error(err)
})

gffuma avatar Jul 12 '22 17:07 gffuma