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

Any option for live-reloading? #271

Closed
ghost opened this issue Oct 1, 2017 · 7 comments
Closed

Any option for live-reloading? #271

ghost opened this issue Oct 1, 2017 · 7 comments

Comments

@ghost
Copy link

ghost commented Oct 1, 2017

Any option for live-reloading?

Something like this? https://github.com/tapio/live-server

@barinali
Copy link
Contributor

@leo what do you think about this feature request? I'm not sure having it in serve. Do you consider extending serve in this way? If so, I'd like to create a pull request for this.

@leo
Copy link
Contributor

leo commented Oct 24, 2017

I think this is too much for core. People can wrap serve if they want it... 😊

@leo leo closed this as completed Oct 24, 2017
@revelt
Copy link

revelt commented Nov 6, 2017

hi guys, I came here looking for live-reloading functionality. What CLI apps would you suggest as the best alternative that can do serve + livereload? I'm looking for turnkey CLI app, equivalent to serve but with live reloading because I hate clicking "refresh" button when it can be automated.
cheers

@ghost
Copy link
Author

ghost commented Mar 19, 2018

@revelt a bit late to the party but there's browser-sync

@tonypee
Copy link

tonypee commented Apr 3, 2018

i think reloading is important too. maybe a cli version of live reload

@shannonmoeller
Copy link

shannonmoeller commented Apr 5, 2018

There are several options out there, but none suited my purpose, so I made one.

https://github.com/shannonmoeller/livery

{
  "scripts": {
    "start": "serve"
    "watch": "lr | serve"
  }
}
{
  "scripts": {
    "dev": "run-p watch-*",
    "dev-serve": "serve web",
    "dev-watch": "lr 'web/assets/**/*.{css,js}'"
  }
}

@lukejacksonn
Copy link

I wanted to understand how live reloading actually worked once. So I tried rolling my own in a determined zero dependency fashion with the aim of learning as mush as possible. I hit a wall pretty quick at the web socket connection, implementing this myself was not something I wanted to do so I searched for an alternative method for the dev server to communicate with the browser.

I stumbled across connection-type: text/event-stream which creates a one way stream of data that the browser that can subscribe to using the EventStream API. Both these features are native to their respective platforms (node and the browser) so it barely adds anything to either codebase.

Now all that remains to be done by the server when --watch option is present is to:

  • Inject the subscriber code into the html document that is being served
file += `
  <script>
    const source = new EventSource('http://localhost:5000');
    source.onmessage = e => location.reload(true);
  </script>
`
  • Watch the file system and send a message to the browser on any change
fs.watch(path.join(cwd, root), { recursive: true }, () =>
  sendMessage(res, "message", "reloading page")
);

I ended up adding this feature to my own little dev server and it has been working well so far. Recently I came back to use zeit/serve for a project thinking that it had live reload but found it doesn't!

Would you be interested in me making a PR that includes the above implementation to see how it might look? I agree with @leo that a full blown web socket implementation would be too much for core.. but this approach offers quite a bit of bang for its buck.

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

No branches or pull requests

6 participants