سؤال

I have installed Magento 2 and set developer mode, then I have installed one module that contains css and js files. The module works fine and css and js files are rendering from pub/static folder. Now I have made css changes in module directory css file and run command

php bin/magento setup:static-content:deploy

but Magento is not making my new changes in pub/static css files so I'm getting old css file contents. Does anyone know how to regenerate the whole css file in pub/static folder?

هل كانت مفيدة؟

المحلول

Before deleting pub/static make a backup of pub/static/.htaccess and restore it again. otherwise your all css and js will show 404 error!

  1. Remove pub/static (keep the .htaccess backed up and copy it again)
  2. Remove var/cache
  3. Remove var/composer_home
  4. Remove var/generation
  5. Remove var/page_cache
  6. Remove var/view_preprocessed
  7. Run php bin/magento setup:static-content:deploy

نصائح أخرى

answers here don't mention that you shouldn't delete the .htaccess in the pub/static folder.

to delete all files except the .htacces enter the pub/static directory and run

find . -depth -name .htaccess -prune -o -delete

then you can run

rm -rf var/cache/ var/generation/ var/page_cache/ var/view_preprocessed/ 

I have just delete the css files from pub/static folder and run the command:

php bin/magento setup:static-content:deploy

It will re generate the same file with latest changes.

As the question is for js and css update in Magento 2 in developer mode.

  1. Updating the JS in your respected module,before refreshing page the same file from pub/static/frontend/namespace/theme/ ... module/js/... delete it so after page refresh fresh JS file will generate in pub folder.
  2. Updating the CSS in your respected module, do the pt 1.
  3. Updating Less file

Setup theme in magento ...\dev\tools\grunt\configs\themes.js

Clone any one theme example luma theme to your theme and assign a name suppose xyz

xyz: {
        area: 'frontend',
        name: 'Magento/luma',
        locale: 'en_US',
        files: [
            'css/styles-m',
            'css/styles-l'
        ],
        dsl: 'less'
    }

In root folder change file name : Gruntfile.js.sample to Gruntfile.js

In root folder change file name : package.json.sample to package.json

Need to have grunt in you site folder else install via npm

Open command prompt with administrator permission

Go to site folder in CMD & hit command grunt exec:theme for first time after deploy the theme

Then grunt less:theme every time if we do any changes in less

If you are doing frequently changes in less use grunt watch so it will automatic hit grunt less:theme

Note : You might get Symlink error so go to file .../app/etc/di.xml and comment out or remove code (below code ) in you local till development done, don't push chnaged code of this file to server, it only required in local if need.

<item name="view_preprocessed" xsi:type="object">Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink</item>

There is one important note to make about this question that most of the answers don't specifically point out. In your case (with the set up you are using) the reason your CSS changes aren't getting picked up when you regenerate the CSS into the pub/static folder is you aren't removing the content of the var/view_preprocessed director. Inside this directory there are cached version of your CSS that are pulling into the pub/static folder when you run the php bin/magento setup:static-content:deploy command.

So when you recompile, Magento will first look to the var/view_preprocessed folder for the cached CSS. If this folder is empty, it will look to the theme files and pull that CSS for compilation.

These setting are configurable, so there are a lot of way to get to your destination that will change the path you need to take. But for the solution to your specific setup:

  1. Remove the files from the pub/static folder: rm -rf pub/static/*

  2. Remove the files from the var/view_preprocessed folder: rm -rf var/view_preprocessed/*

  3. Recompile the pub static folder: php bin/magento setup:static-content:deploy

  4. Clear cache is it's turned on: php bin/magento cache:clean

  5. Refresh the browser.

Simple way 1

In the Magento Admin. Go to System > Tools > Cache Management and click Flush Static Files Cache.

Simple way 2

If you just changed the css file which already existing, what you need to do is just delete the corresponding file in pub/static, then when you refresh the page, it will generate the new version css file. It works in the developer mode.


Notice

  1. When you create new file, rather than modifying an existing file, you may also need to clear the cache, php bin/magento cache:flush.

  2. Default mode and developer mode doesn't need deploy static view files, because of static files being dynamically generated rather than materialized. (Reference: About Magento modes)

  3. In production mode, you need deploy static files and clean cache by running bin/magento setup:static-content:deploy and bin/magento cache:flush.

do not forget to run the grunt exec:theme, grunt less:theme commands where theme is the theme you declared in the themes.js file from the config folder of grunt ( all this is in the developer guide ) and when you have magento in developer mode you shouldn't run the static deploy command, Magento2 works with Symlinks

Ok so based on what I have read in various other posts and my personal experience here is the process to regenerate the CSS or any Static Content:

php bin/magento setup:upgrade

This will wipe out all the necessary cache/generation/etc files without deleting anything you actually need.

php bin/magento setup:di:compile

This will then re-compile the files. Lastly:

php bin/magento setup:static-content:deploy

After the last command everything should be new and fresh, just refresh your page.

----- Notes ------

1) Make sure your file permissions and owner:user group is set correctly before doing this, you may need to speak about these with your sysadmin.

2) If your setup:upgrade command fails it might delete the files and be unable to write the new ones, as was my case due to file permissions. This can break your site, just iron out the permissions, then re-run the command.

3) I have not found (so far) a way to only re-compile a specific theme or file (would be super helpful if anyone finds a way) without writing a completely custom script

If anyone knows of something better let me know, because 12 minutes of compiling files because I had to make some css changes is extremely poor design in my eyes.

--- Edit ---

Magento 2 has Grunt tied into it and this makes refreshing CSS/LESS files infinitely easier if you take the time to configure it. Most the files are there as .sample files just remove the .sample file extension, configure the themes.js file in /dev/tools/grunt/configs folder. Then when you need to refresh your LESS files you simply run grunt less:--theme-- from the command line.

Resource: http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/css-topics/css_debug.html

Run these commands on your Magento root folder:

alias mage="php -d memory_limit=-1 -f bin/magento"

chmod -R 775 pub/static/ var/ pub/media/ &&
rm -rf var/view_preprocessed/ var/cache/ var/page_cache/ var/tmp/ var/generation/ pub/static/frontend/ ;
mage cache:flush &
mage indexer:reindex &
mage setup:upgrade &&
mage setup:static-content:deploy ; 
mage setup:db-data:upgrade &&
mage dev:source-theme:deploy &&
chmod -R 775 pub/static/ pub/media/ var/

Remove folder that contain your theme in pub/static/frontend
clear cache
You can use grunt to watch apply stylesheets. Read here

  1. Remove pub static.
  2. Give pub/static 777 permission
  3. Clear cache.
  4. Run: php bin/magento setup:static-content:deploy

You will see new css.

NOTE:

Give proper folder and file permission.

I hope this will help you.

Couldn't you just flush static cache in the admin panel and then

run php bin/magento setup:static-content:deploy

Make sure you are using developer mode.

Then execute following code:

chmod -R 777 var generated pub/static
grunt exec:default
grunt less:your_theme_name

Refresh the frontend page, These steps solved my problem.

However, you may add the -f or --force flag to the setup command.

Example:

php bin/magento setup:static-content:deploy -f

I'm gonna drop my wisdom droplet here.

I use a cache_fly.sh bash script which basically does:

#!/bin/bash
start=`date +%s%N`
./magento setup:upgrade && \ 
./magento cache:flush && \ 
./magento setup:static-content:deploy -f
end=`date +%s%N`
runtime=$(bc <<< "scale=9;($end-$start)/1000000000")
echo "" && echo "" && echo -e "\e[34mTOTAL EXECUTION TIME: \e[1m\e[93m$runtime\e[0m"

NOTE: I've added an echo to show me how many time of my life I've wasted waiting for it to run. Also ./magento is a script to do php bin/magento or call the same thing inside the docker container, if is docker used you could make yours or point to php bin/magento but if you're reading this you should know what I'm talking about, if not, please run asking for help.

Little adaptation for my case

#!/bin/bash
start=`date +%s%N`
chmod -R 775 pub/static/ var/ pub/media/ && \
rm -rf var/view_preprocessed/* var/cache/* var/page_cache/* var/tmp/* var/generation/* pub/static/frontend/* && \
./magento cache:flush && \
./magento setup:upgrade && \
./magento cache:clean && \
./magento s:s:d es_ES en_US -f && \
./magento setup:di:compile && \
chmod -R 775 pub/static/ pub/media/ var/
end=`date +%s%N
runtime=$(bc <<< "scale=9;($end-$start)/1000000000")
echo "" && echo "" && echo -e "\e[34mTOTAL EXECUTION TIME: \e[1m\e[93m$runtime\e[0m"

in my case it was all folder permession , the easy fix is to just do the following:

1- change the /pub folder permission to 777:

sudo chmod 777 pub -Rf

2- redeploy the site static contents:

bin/magento setup:static-content:deploy -f

in case u have multi-lang site, write like this:

bin/magento setup:static-content:deploy -f en_US ar_SA

This is a Very Simple Way.

cd pub/static
find . -depth -name .htaccess -prune -o -delete
cd ../..
bin/magento setup:static-content:deploy

Please read the latest Magento document!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top