Skip to content

Commit

Permalink
fix: add a small delay before triggering browser refreshes to address…
Browse files Browse the repository at this point in the history
… 500 errors related to index.html not being found
  • Loading branch information
Shakeskeyboarde committed Jun 13, 2024
1 parent 808f726 commit 2ddf9dc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-live-preview",
"version": "0.1.5",
"version": "0.1.6",
"description": "Vite preview watch mode.",
"license": "ISC",
"repository": {
Expand Down
10 changes: 7 additions & 3 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default ({ reload = true, enable }: PreviewModeOptions = {}): Plugin => {
let resolvedConfig: ResolvedConfig | undefined;
let previewServer: PreviewServer | undefined;
let error: Error | undefined;
let sendTimeout: NodeJS.Timeout | undefined;

const sockets = new Set<WebSocket>();

Expand Down Expand Up @@ -98,9 +99,12 @@ export default ({ reload = true, enable }: PreviewModeOptions = {}): Plugin => {
if (previewServer) {
if (reload) {
previewServer.config.logger.info(chalk.green('page-reload'), { timestamp: true });
sockets.forEach((socket) => {
socket.send(JSON.stringify({ type: 'page-reload' }));
});
clearTimeout(sendTimeout);
sendTimeout = setTimeout(() => {
sockets.forEach((socket) => {
socket.send(JSON.stringify({ type: 'page-reload' }));
});
}, 250).unref();
}

if (resolvedConfig?.clearScreen !== false) {
Expand Down

0 comments on commit 2ddf9dc

Please sign in to comment.