Skip to content

Commit

Permalink
fix: __vite_hmr race condition. Fixes #32
Browse files Browse the repository at this point in the history
  • Loading branch information
magne4000 committed Dec 11, 2024
1 parent 034fb16 commit 1b07c47
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/vike-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@
],
"repository": "github:vikejs/vike-node",
"license": "MIT"
}
}
5 changes: 3 additions & 2 deletions packages/vike-node/src/plugin/plugins/devServerPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fork } from 'child_process'
import { createServer, type IncomingMessage, type Server } from 'http'
import { type IncomingMessage, type Server, createServer } from 'http'
import type { Plugin, ViteDevServer } from 'vite'
import { globalStore } from '../../runtime/globalStore.js'
import type { ConfigVikeNodeResolved } from '../../types.js'
Expand Down Expand Up @@ -102,7 +102,7 @@ export function devServerPlugin(): Plugin {

function setupHMRProxy(req: IncomingMessage) {
if (setupHMRProxyDone || isBun) {
return
return false
}

setupHMRProxyDone = true
Expand All @@ -113,6 +113,7 @@ export function devServerPlugin(): Plugin {
HMRServer.emit('upgrade', clientReq, clientSocket, wsHead)
}
})
return true
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vike-node/src/runtime/globalStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export const globalStore = (globalThis.__vikeNode ||= {
}) as {
isDev: boolean
viteDevServer?: ViteDevServer
setupHMRProxy: (req: IncomingMessage) => void
setupHMRProxy: (req: IncomingMessage) => boolean
}
8 changes: 7 additions & 1 deletion packages/vike-node/src/runtime/vike-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ export const renderPageHandler = ((options?) => async (request, context, runtime
let staticMiddleware: ConnectMiddleware | undefined

if (nodeReq) {
globalStore.setupHMRProxy(nodeReq)
const needsUpgrade = globalStore.setupHMRProxy(nodeReq)

if (needsUpgrade) {
// Early response for HTTP connection upgrade
return new Response(null)
}

const { resolveStaticConfig } = await import('./utils/resolve-static-config.js')
staticConfig = resolveStaticConfig(options?.static)
}
Expand Down

0 comments on commit 1b07c47

Please sign in to comment.