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

Add LiveReload #663

Merged
merged 13 commits into from
May 17, 2018
Merged

Add LiveReload #663

merged 13 commits into from
May 17, 2018

Conversation

amyrlam
Copy link
Contributor

@amyrlam amyrlam commented May 15, 2018

Motivation

Resolves: #234

Have you read the Contributing Guidelines on pull requests?

yes!

Test Plan

LiveReload now works on save. LiveReload is watching docs/, blog/, pages/ and static/.

livereload

I QA'd the process.env.NODE_ENV === 'production' case locally. In website/package.json, I changed yarn start to "start": "NODE_ENV=production node ../lib/start-server.js". I then confirmed with a fresh yarn start that LiveReload wasn't running in the terminal, or present in the browser.

Related PRs

Updated the docs admin/testing-changes-on-Docusaurus-itself.md in this PR.


cc @yangshun please let me know if any feedback, can make edits!

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label May 15, 2018
@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented May 15, 2018

Deploy preview for docusaurus-preview ready!

Built with commit 5584173

https://deploy-preview-663--docusaurus-preview.netlify.com

@JoelMarcey
Copy link
Contributor

😮 👍 ❤️ 🎉

I wake up to see this! This is so exciting!

Copy link
Contributor

@yangshun yangshun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excited about the PR! I'll let @JoelMarcey have a final look through before merging.

lib/core/Site.js Outdated
@@ -159,6 +159,9 @@ class Site extends React.Component {
}}
/>
))}
{process.env.NODE_ENV !== 'production' && (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of checking whether it's production, check whether it's development.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok 👍. I went with it this way cause when debugging locally I did a console.log of process.env.NODE_ENV and it was undefined. But could be development or !process.env.NODE_ENV?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's because it's not being set yet. I set it programmatically in server.js so that the user does not have to do anything in addition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(To clarify, still confused why it was undefined locally...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh I see, thanks for the clarification 👍. Sorry typed my latest before refreshing the page to see your comment about it not being set.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, process.env looks at your process environmental variables. To set it outside of your code, you could do:

$ NODE_ENV=development npm run start

That would require users to have to change the way they start the server, which isn't ideal. I was thinking of passing in a prop developmentMode to all the top-level components like Site, BlogLayout, and read from it when determining whether to inject LiveReload. But there are too many places to add so I don't think it's a good idea to do so. Hence we set the variable in server/server.js.

lib/core/Site.js Outdated
@@ -159,6 +159,9 @@ class Site extends React.Component {
}}
/>
))}
{process.env.NODE_ENV !== 'production' && (
<script src="http://localhost:35729/livereload.js" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This port number needs to be consistent with the server configuration. Hence extract the port out into a constant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍thanks, this is a good improvement, makes sense


// Gaze watches some specified files and triggers a callback when they change.
gaze(
['../docs/**/*', 'blog/**/*', 'pages/**/*', 'static/**/*'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to listen on the entire website dir.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

gaze(
[
'../docs/**/*', // docs
'./**/*', // website
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't quite grok why the dot syntax doesn't work in https://github.com/shama/gaze and minimatch, especially isaacs/minimatch#30. (LiveReload doesn't "fire up" in the terminal like the gif with this syntax here.)

Switched it to just star syntax in the latest commit. Let me know what you think...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok let's switch it to **/*

@@ -559,7 +559,7 @@ function execute(port) {
gaze(
[
'../docs/**/*', // docs
'./**/*', // website
'*/*.*', // website
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, maybe this approach is flawed re: https://stackoverflow.com/a/21835063/2628398. If some file was in root of website it wouldn't be watched.

Off to sleep, will revisit this tomorrow...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last thing, it's confusing that **/*.* doesn't work...

Copy link
Contributor

@yangshun yangshun May 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.* specifies any extension and is redundant because * would do the same (in most cases).

If some file was in root of website it wouldn't be watched.

Correct. */*.* only looks at the files directly website/<dir>, files which are deeper such as pages/en/index.js would not be watched.

One last thing, it's confusing that **/. doesn't work...

**/*.* works for me.

I think **/* is the safest and is what we need. What's the rationale for changing to */*.*?

Copy link
Contributor Author

@amyrlam amyrlam May 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the confusion!

With the ./**/* syntax there LiveReload doesn't start up. It seems to be because of the starting .. It hangs like the gif at the end.

HEAD is now at 11bf350 Tweak code a little
alam@tesla website ((HEAD detached at 11bf350)) $ yarn start
yarn run v1.6.0
warning package.json: No license field
$ node ../lib/start-server.js

Delete the .* syntax locally and everything is fine:

alam@tesla website (amy/add-livereload-final) $ git co 2c264e898efda48ccd6bca9ad7e5f8225fce1c82
Note: checking out '2c264e898efda48ccd6bca9ad7e5f8225fce1c82'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 2c264e8 Simplify process.env call
alam@tesla website ((HEAD detached at 2c264e8)) $ yarn start
yarn run v1.6.0
warning package.json: No license field
$ node ../lib/start-server.js
Starting Docusaurus server on http://localhost:3000
Livereload listening on 35729...

So now how to fix watching all of website/? Locally, change to **/*.*. It also doesn't work.

docu

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even delete docs (which is wrong) and just [ '**/*', // website ], doesn't run, like the gif?

Something else I'm missing? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's super odd. Is working fine for me. Could you reinstall the dependencies or at worst, fetch a clean copy of the repository?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change it back to **/* for now. I'm pretty confident that's the right path to specify.

Copy link
Contributor

@yangshun yangshun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path for recursive files under website is now incorrect IMO. @amyrlam could you verify?

@amyrlam
Copy link
Contributor Author

amyrlam commented May 16, 2018

I changed it to `**/*. But it still doesn't work for me, same behavior as the gif in the thread.

I rm -rf'd the directory, re-cloned and did a yarn install in root and website. master works fine for me locally.

Stumped rn re: what's wrong with my local setup

@yangshun
Copy link
Contributor

@JoelMarcey could you help give it a go locally?

@JoelMarcey
Copy link
Contributor

Let me check this out - I wonder if the local package-lock.json is clashing with the one on GitHub or something.

@JoelMarcey
Copy link
Contributor

It looks like I got it running. I see the site locally and this at the command line.

screenshot 2018-05-16 17 15 51

But there was big change to package-lock.json locally after I ran npm install. And a smaller change to package.json

I wonder if the React 16 move, is causing trouble.

screenshot 2018-05-16 17 18 37

@JoelMarcey
Copy link
Contributor

I can push an update to package-lock.json via a PR and then we can rebase this on that change to see if that makes a difference for @amyrlam ?

@amyrlam
Copy link
Contributor Author

amyrlam commented May 17, 2018

@JoelMarcey sure, I can also debug my issue separately from this PR, whatever is easier...

Hm I've been using yarn install. Just noticed there is a package-lock.json and a yarn.lock in Docusaurus? I only committed the yarn.lock here: https://github.com/facebook/Docusaurus/pull/665/files

@JoelMarcey
Copy link
Contributor

We have had both. (whether we need both, that may be another question?). And you also changed yarn.lock in #665 as well.

My initial feeling here is that there is some conflict going on with you locally. But I am not 100% sure yet.

@amyrlam
Copy link
Contributor Author

amyrlam commented May 17, 2018

I have it working again finally! 😅

I "nombom'd" and blew away node_modules in root and website/, redid npm installs, which basically brought in your change to the package-lock.json in #669.

I'm not quite sure what happened, but I'm seeing reliable results with npm now.

@yangshun
Copy link
Contributor

Tested on another MacBook of mine and it works. @JoelMarcey I think it's good to ship. Would be great if you got to try it out too!

@JoelMarcey
Copy link
Contributor

@amyrlam @yangshun I tested on my machine and it works! I tested changing some docs, some pages (e.g. help.js). I can even change some things in siteConfig.js -- like images.

This is great!

The only things that didn't live reload were some changes to siteConfig.js. e.g., headerLinks and colors primaryColor, etc. Those don't work for you, do they?

Not sure we can do much about that, but it could be something we try to update later. The colors would probably be most useful to live reload, I think.

@endiliey
Copy link
Contributor

endiliey commented May 17, 2018

Hi, sorry if I interfered in this discussion. This is awesome. Waiting this to be shipped. I've tried it locally and it works
But there are some part that didn't work, especially colors in siteConfig.js like what @JoelMarcey mentioned. I agree that colors would probably be most useful to live reload

Human are curious by nature, so I digged a little bit to see what's wrong. Modified amy's lib/server/server.js gaze part to debug

// Listen for all kinds of file changes - modified/added/deleted.
this.on('all', function(event, filepath) {
   console.log(filepath + ' was ' + event);

Then, I went to edit primaryColor in siteconfig.js
test3

LiveReload happens but the css is not changed. I had to yarn start again

Notice that main.css is not re-built on reload. It's only built once on yarn start.
Relevant codes:
https://github.com/amyrlam/Docusaurus/blob/amy/add-livereload-final/lib/server/server.js#L459-L514

Maybe we could trigger the css generation & sent it to user if siteConfig.js is changed

Hopefully this help !

@yangshun
Copy link
Contributor

yangshun commented May 17, 2018

siteConfig will not be reloaded because it's require()-ed during initialization and not on each page load. I think it would require a huge change to support livereload on config changes. It's not typical for processes to reload config after the initial start up. For reference, webpack, Flow and Jest also do not reload the config after the server starts running.

I'm aware of this limitation in this PR and I think it's fine to ship as-is. The part about supporting siteConfig can be found in this task #267

@endiliey
Copy link
Contributor

That makes sense. Awesome
17pmxu

@JoelMarcey
Copy link
Contributor

I 100% agree that we can ship this as-is.

I will say one thing -- I changed the image path in siteConfig.js and it picked something up, because the image changed when I did it. Maybe because the server still looks in siteConfig.js directly for path information?

@amyrlam
Copy link
Contributor Author

amyrlam commented May 17, 2018

@endiliey not interfering at all, appreciate you chiming in! 👍

Yes I'm seeing the same thing as @JoelMarcey re: colors. I'm not sure why this is happening yet, but probably that the colors are only loaded on start.

Copy link
Contributor

@JoelMarcey JoelMarcey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ 😍

@JoelMarcey JoelMarcey merged commit f9a0907 into facebook:master May 17, 2018
@amyrlam amyrlam deleted the amy/add-livereload-final branch October 2, 2018 05:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants