Edit : about the possible duplicate !
I would like to mention that this answer does not answer my question, if someone sees that it is the same thing, I invite him to answer me instead of saying that he is duplicated and we will see if it solves my problem.
I think that when looking for something, the first reflex is to search if there was already the same question as mine that was asked before, and I saw this post and it does not answer my problem! and even more there is not a reliable answer or accepted !
Does anyone ever work or still work with the liveReload
via grunt ?
-
I have some problem, i don't know why but the liveReload dont refresh th content changed automatically.
-
I have grunt installed and it works fine i can
clean
,exec
, etc but, when i enter:grunt watch
he watch well the changes, example: if i changemycustom.css
, he display :
File "pub/static/frontend/Package/theme/fr_FR/css/mycustom.css" changed. Completed in 0.001s at Wed Mar 21 2018 16:08:58 GMT+0100
(CET) – Waiting…
But he doesn't refresh the page,(when i refresh it manually, i see well my changes) is there a configuration to do in grunt
or in the server
?
- I also wanted to mention that the
liveReload browser extension
is well installed but does not work, ie when I press on a logo, it shows me an error message:
Could not connect to LiveReload server. Please make sure that
LiveReload 2.3 (or later) or another compatible server is running.
Best Answer
It's been two days since the post is open and no one has found the answer to my question, and working hard on this topic I finally found the solution. So I decided to publish the complete solution, the installation of
Grunt
and thenLivereload
and how to configure both together because I found that they are very useful tools for Magento2 and even more for the frontend developers###Let's start:
What is LiveReload ?
For those who don't know what is LiveReload, LiveReload monitors changes in the file system. As soon as you save a file, it is preprocessed as needed, and the browser is refreshed.
Even cooler, when you change a CSS file or an image, the browser is updated instantly without reloading the page.
I find this beautiful because for a developer Magento when it is in full development or integration he is obliged to refresh the page manually to see the result, then he goes back on his code he update, he saves, he clean the cache, then he returns to the browser , he refresh the browser and so on, while with the liveReload it's over all that, it is just enough to save the update and 'Hoop' magically the content that you have changed instantaneously appear without refreshing the browser and without cleaning the cache, deploying the static-content either but for the cache, you also need Grunt but I explain in detail how to put this.
What is Grunt ?
Grunt is a JavaScript Task Runner, a tool that allows you to create automated tasks in JavaScript,
it is very useful for
Magento 2
because by its configuration, it allows us to removes the theme related static files in thepub/static
andvar
directories, set up the.less
files, and especially thewatch
that observes the changes of the files and which it is indispensable for the ourliveReload
Now the installation :
###Grunt:
We have to install npm node.js on your machine :
sudo apt-get install npm
Install Grunt CLI :
npm install -g grunt-cli
Refresh nodejs :
npm install
To make sure that the npm is well installed the
npm -v
should output something like:5.7.1
, for Gruntgrunt --version
should output something like:grunt-cli v1.2.0
Grunt theme configuration: go to :
dev/tools/grunt/configs/themes.js
then add your current theme name that you are used, assuming that your theme name is :Prince/amir
in :app/design/frontend/Prince/amir
so you add the following inmodule.exports
like this:You can use
grunt clean:prince
to removes the theme related static files in thepub/static
andvar
directories.Save . More informations
###LiveReload:
npm install -g livereload
To make sure that the liveReload is well installed,
livereload -v
should output the version something like :0.7.0
Install the
LiveReload browser extension
, available for Chrome and Firfox.Add the script line bellow inside
<head>
tag indefault.xml
ordefault_head_blocks.xml
of your current theme example :app/design/frontend/Prince/amir/Magento_Theme/layout/default_head_blocks.xml
Here as you see there is an IP(200.120.00.01) address plus the port(35729) plus livereload.js file
So what to do: replace this
IP
with your IP project, theport
remains the same dont change it35729
and thelivereload.js
you copy it in your root project, you can get it from the livereload installation(remember we installed it before) the path is:/usr/lib/node_modules/livereload/ext/livereload.js
, or from this github.###it's time to practice:
Run
grunt clean
,grunt less
,grunt execute
(sometimes magento doesn't work well with livereload if it has not been executed before) and thenWe launch the Grunt watch to monitor our files changes.
Check if the LiveReload browser extension icon is well activated, in Firefox the icon comes green, in Chrome the small dot inside the circle becomes full black
important : if everything is good, if you enter in your browser your
IP:35729
example :200.120.00.01:35729
, you'll see some welcome message like:grunt watch
some new line appears tells that this file has been changed something like:and look now your browser, Hoop the item that you just changed appeared and updated instantly without reloading the page, also without cleaning the cache or deploying the static-content, everything is done automatically.
here I am finished, I hope you will enjoy this, it is really useful tool.
Good luck.