MCPcopy
hub / github.com/vitejs/vite / handleMessage

Function handleMessage

packages/vite/src/client/client.ts:206–338  ·  view source on GitHub ↗
(payload: HotPayload)

Source from the content-addressed store, hash-verified

204setupForwardConsoleHandler(transport, forwardConsole)
205
206async function handleMessage(payload: HotPayload) {
207 switch (payload.type) {
208 case 'connected':
209 console.debug(`[vite] connected.`)
210 break
211 case 'update':
212 await hmrClient.notifyListeners('vite:beforeUpdate', payload)
213 if (hasDocument) {
214 // if this is the first update and there's already an error overlay, it
215 // means the page opened with existing server compile error and the whole
216 // module script failed to load (since one of the nested imports is 500).
217 // in this case a normal update won't work and a full reload is needed.
218 if (isFirstUpdate && hasErrorOverlay()) {
219 location.reload()
220 return
221 } else {
222 if (enableOverlay) {
223 clearErrorOverlay()
224 }
225 isFirstUpdate = false
226 }
227 }
228 await Promise.all(
229 payload.updates.map(async (update): Promise<void> => {
230 if (update.type === 'js-update') {
231 return hmrClient.queueUpdate(update)
232 }
233
234 // css-update
235 // this is only sent when a css file referenced with <link> is updated
236 const { path, timestamp } = update
237 const searchUrl = cleanUrl(path)
238 // can't use querySelector with `[href*=]` here since the link may be
239 // using relative paths so we need to use link.href to grab the full
240 // URL for the include check.
241 const el = Array.from(
242 document.querySelectorAll<HTMLLinkElement>('link'),
243 ).find(
244 (e) =>
245 !outdatedLinkTags.has(e) && cleanUrl(e.href).includes(searchUrl),
246 )
247
248 if (!el) {
249 return
250 }
251
252 const newPath = `${base}${searchUrl.slice(1)}${
253 searchUrl.includes('?') ? '&' : '?'
254 }t=${timestamp}`
255
256 // rather than swapping the href on the existing tag, we will
257 // create a new link tag. Once the new stylesheet has loaded we
258 // will remove the existing link tag. This removes a Flash Of
259 // Unstyled Content that can occur when swapping out the tag href
260 // directly, as the new stylesheet has not yet been loaded.
261 return new Promise((resolve) => {
262 const newLinkTag = el.cloneNode() as HTMLLinkElement
263 newLinkTag.href = new URL(newPath, el.href).href

Callers

nothing calls this directly

Calls 13

hasErrorOverlayFunction · 0.85
clearErrorOverlayFunction · 0.85
waitForSuccessfulPingFunction · 0.85
createErrorOverlayFunction · 0.85
notifyListenersMethod · 0.80
queueUpdateMethod · 0.80
hasMethod · 0.80
addMethod · 0.80
logMethod · 0.80
prunePathsMethod · 0.80
cleanUrlFunction · 0.70
debugMethod · 0.65

Tested by

no test coverage detected