Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Livereload reconnect #41

Merged
merged 1 commit into from
Feb 11, 2020
Merged

Livereload reconnect #41

merged 1 commit into from
Feb 11, 2020

Conversation

lukejacksonn
Copy link
Owner

Currently if a browser tab connects to a servor instance with livereload enabled and the instance process is then terminated.. the following errors appear in the browser console:

image

Its seems that the first error occurs because the keep-alive connection is not closed properly. This can be fixed by adding some cleanup logic on SIGINT:

process.on('SIGINT', () => {
  while (clients.length > 0) clients.pop().end();
  process.exit();
});

That then just leave this error in the console which appears to be coming from chrome trying to reconnect to the closed keep-alive connection at 1 second intervals (not sure where to find documentation on this behaviour):

image

If a server is then started again on the same port then chrome stops erroring as the connection is re-established which means the next time files change, the browser will reload.

My issue with this implementation is that if the developer terminates the process, then makes changes, then restarts the server.. then the connection will be re-established but what is displayed in the browser window is now not representitive of the current state of the file system until the next file change.

The proposal here is to reload the page upon the livereload connection being re-established:

const livereload = `
  <script>
    const source = new EventSource('/livereload');
    const reload = () => location.reload(true);
    source.onmessage = reload;
    source.onerror = () => (source.onopen = reload);
    console.log('[servor] listening for file changes');
  </script>
`;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant