11

Situation: I am running some free trials of Magento 2 and have limited HD space on the VPS. For this reason to cut down on HD space I want to have Magento symlink the files in default or production mode. I have noticed that a default Magento 2 install is about 420mb once the site first runs it goes to around 770mb so 350mb of this is copied files.

I have modified: app/etc/di.xml line:

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

to

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

This works ok and the site will properly create the symlinks even when in default mode. However the issue is that the css is not created or the js-translation.json the files it should create but does not are:

/pub/static/version1488209436/frontend/Magento/luma/en_US/css/styles-m.css

/pub/static/version1488209436/frontend/Magento/luma/en_US/css/styles-l.css

/pub/static/version1488209436/frontend/Magento/luma/en_US/css/print.css

/pub/static/version1488209436/frontend/Magento/luma/en_US/js-translation.json

So the question is: how do I make Magento also generate these files when in default or production mode and using symlinks?

7
  • Perhaps you could use Gulp or Grunt to for the CSS issue: magento.stackexchange.com/questions/162906/… and look into the different modes for the js-translation.json file? gist.github.com/antonmakarenko/7538216
    – B00MER
    Commented Mar 6, 2017 at 23:53
  • Thanks, I will up this to +100 if someone can give me step by step and verify it works. Commented Mar 7, 2017 at 16:45
  • inside pub/static folder there is a .htaccess file, which is responsible to rewrite the url and remove 'version1488209436' from url and provide an user friendly url. If .htaccess not working properly then only this type of issues occurred.
    – Tonmoy
    Commented Mar 9, 2017 at 7:47
  • thank you @Tonmoy but it is not related to this at all. The issue is that the files don't generate because I'm using symlinks rather than copy while in default / production mode. Commented Mar 9, 2017 at 16:55
  • Since there are 4 files than you can create those symlinks manually or in a .sh script. Commented Mar 12, 2017 at 16:17

2 Answers 2

6
+200

app/etc/di.xml: add new item to stategiesList:

<virtualType name="developerMaterialization" type="Magento\Framework\App\View\Asset\MaterializationStrategy\Factory">
    <arguments>
        <argument name="strategiesList" xsi:type="array">
            <item name="view_preprocessed" xsi:type="object">Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink</item>
            <item name="default" xsi:type="object">Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink</item>
/* ++ */    <item name="asset" xsi:type="object">Magento\Framework\App\View\Asset\MaterializationStrategy\Copy</item>
        </argument>
    </arguments>
</virtualType>

Assuming you are in developer mode just delete the contents of pub/static and go to your page in the browser - magento will regenerate the static content.

worked for me in Magento 2.1.4 (styles-m.css got generated and other files got symlinked).

all the magic happens in vendor/magento/framework/App/View/Asset/MaterializationStrategy/Factory.php:

public function create(Asset\LocalInterface $asset)
{   
    if (empty($this->strategiesList)) {
        $this->strategiesList[] = $this->objectManager->get(self::DEFAULT_STRATEGY);
    }   

    foreach ($this->strategiesList as $strategy) {
        if ($strategy->isSupported($asset)) {
            return $strategy;
        }   
    }   

    throw new \LogicException('No materialization strategy is supported');
}   

Magento just loops through stategiesList items and use the first stategy that supports the asset.

How to make it work in production mode?

Disclaimer: this hack contains core file editing. beware.

all tested on magento 2.1.4

  1. remove version number from static files in Stores > Configuration > Advanced > Developer > Static Files Settings > No
  2. edit vendor/magento/framework/App/StaticResource.php and make launch function look like this:

    public function launch()
    {   
    // disabling profiling when retrieving static resource
    \Magento\Framework\Profiler::reset();
    $appMode = $this->state->getMode();
    /*if ($appMode == \Magento\Framework\App\State::MODE_PRODUCTION) {
        $this->response->setHttpResponseCode(404);
    } else {*/
        $path = $this->request->get('resource');
        $params = $this->parsePath($path);
        $this->state->setAreaCode($params['area']);
        $this->objectManager->configure($this->configLoader->load($params['area']));
        $file = $params['file'];
        unset($params['file']);
        $asset = $this->assetRepo->createAsset($file, $params);
        $this->response->setFilePath($asset->getSourceFile());
        $this->publisher->publish($asset);
    /*}*/
    return $this->response;
    }   
    
  3. delete contents of pub/static and visit your store url in a browser.

3
  • Thank you this worked perfectly! Just that one line in the di.xml! Also I appreciate your explanation of how it works. Now the install is 482mb instead of 770mb which will save me money not having to upgrade my VPS free trial server. Commented Mar 13, 2017 at 17:25
  • you are very welcome sir I am glad it worked for you. Commented Mar 13, 2017 at 17:26
  • is it not just enough modify only StaticResource.php and keep the original app/etc/di.xml without adding <item name="asset" xsi:type="object">Magento\Framework\App\View\Asset\MaterializationStrategy\Copy</item> ?
    – LucScu
    Commented Oct 12, 2017 at 13:04
0

When not in production mode Magento 2 will try to create symlinks for some static resources. You can change that behavior by doing the following.

  1. Open up app/etc/di.xml and find the virtualType name="developerMaterialization" section. In that section you'll find an item name="view_preprocessed" that needs to be modified or deleted. You can modify it by changing the contents from Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink to Magento\Framework\App\View\Asset\MaterializationStrategy\Copy

  2. Delete the files under pub/static. Please dont delete the .htaccess file.

1
  • 1
    I don't think you understood my question, I don't want to "copy" I want to "symlink" to save space with the limited server resources. Commented Mar 12, 2017 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.