Skip to content

Commit

Permalink
fix(dev-server): initialize Miniflare only on the first run (#77)
Browse files Browse the repository at this point in the history
* fix(dev-server): initialize Miniflare only on the first run

* add changeset
  • Loading branch information
rutan authored Feb 13, 2024
1 parent a8f4742 commit b397fdd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-emus-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hono/vite-dev-server": patch
---

fix: initialize Miniflare only on the first run
17 changes: 10 additions & 7 deletions packages/dev-server/src/cloudflare-pages/cloudflare-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ const nullScript = 'export default { fetch: () => new Response(null, { status: 4
let mf: Miniflare | undefined = undefined

export const getEnv: GetEnv<Options> = (options) => async () => {
// Dynamic import Miniflare for environments like Bun.
const { Miniflare } = await import('miniflare')
mf = new Miniflare({
modules: true,
script: nullScript,
...options,
})
if (!mf) {
// Dynamic import Miniflare for environments like Bun.
const { Miniflare } = await import('miniflare')
mf = new Miniflare({
modules: true,
script: nullScript,
...options,
})
}

const env = {
...(await mf.getBindings()),
Expand All @@ -48,6 +50,7 @@ export const getEnv: GetEnv<Options> = (options) => async () => {

export const disposeMf = async () => {
mf?.dispose()
mf = undefined
}

const plugin = (options?: Options): Plugin => {
Expand Down

0 comments on commit b397fdd

Please sign in to comment.