Skip to content

Commit 0816f95

Browse files
committed
fix: avoid running twice in SSR mode
1 parent 4453a73 commit 0816f95

File tree

1 file changed

+24
-11
lines changed
  • packages/vite-plugin-checker/src

1 file changed

+24
-11
lines changed

packages/vite-plugin-checker/src/main.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ export function checker(userConfig: UserPluginConfig): Plugin {
5858
const enableOverlay = userConfig?.overlay !== false
5959
const enableTerminal = userConfig?.terminal !== false
6060
const overlayConfig = typeof userConfig?.overlay === 'object' ? userConfig?.overlay : {}
61+
let initialized = false
62+
let initializeCounter = 0
6163
let checkers: ServeAndBuildChecker[] = []
6264
let isProduction = true
6365
let skipRuntime = false
@@ -75,6 +77,13 @@ export function checker(userConfig: UserPluginConfig): Plugin {
7577
// for dev mode (1/2)
7678
// Initialize checker with config
7779
viteMode = env.command
80+
// avoid running twice when running in SSR
81+
if (initializeCounter === 0) {
82+
initializeCounter++
83+
} else {
84+
initialized = true
85+
return
86+
}
7887

7988
checkers = await createCheckers(userConfig || {}, env)
8089
if (viteMode !== 'serve') return
@@ -95,6 +104,8 @@ export function checker(userConfig: UserPluginConfig): Plugin {
95104
skipRuntime ||= isProduction || config.command === 'build'
96105
},
97106
buildEnd() {
107+
if (initialized) return
108+
98109
if (viteMode === 'serve') {
99110
checkers.forEach((checker) => {
100111
const { worker } = checker.serve
@@ -121,19 +132,19 @@ export function checker(userConfig: UserPluginConfig): Plugin {
121132
return
122133
},
123134
transformIndexHtml() {
124-
if (!skipRuntime) {
125-
return [
126-
{
127-
tag: 'script',
128-
attrs: { type: 'module' },
129-
children: composePreambleCode(resolvedConfig!.base, overlayConfig),
130-
},
131-
]
132-
}
133-
134-
return
135+
if (initialized) return
136+
if (skipRuntime) return
137+
138+
return [
139+
{
140+
tag: 'script',
141+
attrs: { type: 'module' },
142+
children: composePreambleCode(resolvedConfig!.base, overlayConfig),
143+
},
144+
]
135145
},
136146
buildStart: () => {
147+
if (initialized) return
137148
// only run in build mode
138149
// run a bin command in a separated process
139150
if (!skipRuntime || !enableBuild) return
@@ -157,6 +168,8 @@ export function checker(userConfig: UserPluginConfig): Plugin {
157168
})()
158169
},
159170
configureServer(server) {
171+
if (initialized) return
172+
160173
let latestOverlayErrors: ClientReconnectPayload['data'] = new Array(checkers.length)
161174
// for dev mode (2/2)
162175
// Get the server instance and keep reference in a closure

0 commit comments

Comments
 (0)