-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: resume overlay when reload page
- Loading branch information
Showing
12 changed files
with
141 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,63 @@ | ||
import type { ErrorPayload } from 'vite' | ||
import { ErrorOverlay, overlayId } from './overlay' | ||
import { ErrorOverlay, overlayId, OverlayPayload, OverlayData, WsReconnectPayload } from './overlay' | ||
let enableOverlay = true | ||
|
||
const WS_CHECKER_ERROR_TYPE = 'vite-plugin-checker-error' | ||
const WS_CHECKER_ERROR_EVENT = 'vite-plugin-checker:error' | ||
const WS_CHECKER_RECONNECT_EVENT = 'vite-plugin-checker:reconnect' | ||
|
||
export function inject() { | ||
const socketProtocol = null || (location.protocol === 'https:' ? 'wss' : 'ws') | ||
const socketHost = `${null || location.hostname}:${'3000'}` | ||
const socket = new WebSocket(`${socketProtocol}://${socketHost}`, 'vite-hmr') | ||
|
||
socket.addEventListener('message', async ({ data: dataStr }) => { | ||
const data = JSON.parse(dataStr) | ||
const data: OverlayPayload = JSON.parse(dataStr) | ||
switch (data.type) { | ||
case 'update': | ||
clearErrorOverlay() | ||
// clearErrorOverlay() | ||
break | ||
case 'full-reload': | ||
break | ||
default: | ||
break | ||
} | ||
|
||
if (data.event === WS_CHECKER_ERROR_TYPE) { | ||
if (data.data.errors.length > 0) { | ||
createErrorOverlay(data.data.errors) | ||
if (data.type === 'custom') { | ||
if (data.event === WS_CHECKER_ERROR_EVENT) { | ||
updateErrorOverlay(data.data) | ||
} | ||
|
||
if (data.event === WS_CHECKER_RECONNECT_EVENT) { | ||
resumeErrorOverlay(data.data) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
function createErrorOverlay(err: ErrorPayload['err'][]) { | ||
let overlayEle: ErrorOverlay | null = null | ||
|
||
function resumeErrorOverlay(data: WsReconnectPayload['data']) { | ||
const payloadsToResume = data.map((d) => d.data) | ||
if (payloadsToResume.some((p) => p.errors.length)) { | ||
overlayEle = new ErrorOverlay(data.map((d) => d.data)) | ||
document.body.appendChild(overlayEle) | ||
} | ||
} | ||
|
||
function updateErrorOverlay(err: OverlayData) { | ||
if (!enableOverlay) return | ||
clearErrorOverlay() | ||
document.body.appendChild(new ErrorOverlay(err)) | ||
|
||
if (overlayEle) { | ||
overlayEle.updateErrors(err) | ||
if (!overlayEle.getErrorCount()) { | ||
clearErrorOverlay() | ||
} | ||
} else { | ||
overlayEle = new ErrorOverlay(err) | ||
document.body.appendChild(overlayEle) | ||
} | ||
} | ||
|
||
function clearErrorOverlay() { | ||
document.querySelectorAll(overlayId).forEach((n) => (n as ErrorOverlay).close()) | ||
overlayEle = null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.