Jump to content

Media server - images ok but no css/js


Recommended Posts

Hello All,

 

I have problem regarding media server functionality within Prestashop.

When I set media server to subdomain, I see only images served from subdomain, but no css/js.

 

Domain is configured as CNAME to original server, domain and subdomain point to the same location on server.

 

Any ideas how to solve this issue? Many thanks in advance.

Link to comment
Share on other sites

  • 7 months later...

Hi Dobre

 

What version were you using....we have had this running successfully in 1.6 but in 1.7 we have the issue that images are cached on the media server (maxcdn) but CSS and JS files are not. Were you able to resolve this in your version and if so, how?

 

Thanks

AJ

Link to comment
Share on other sites

Thanks for that. We have tried that but it is almost like Prestashop 1.7.0.4 does not use the media url as the base for the js and css files in the same way as the previous versions did. I am hoping that it is a misconfiguration problem and not a problem with PS. We are using the default theme and default data at the moment so it is OOTB.

Link to comment
Share on other sites

Well after some digging on PS 1.7.0.4 we found that the JavascriptManager, StyleSheetManager and CccReducer classes completely ignore the media server settings. We are not sure if this is by design but if it is it seems odd. We have updated these files and we now have our JS and CSS being served from our CDN when the CCC options are on and off. 

 

I am not sure if I should submit these changes anywhere so others can benefit from them (if the PS team agrees the changes should be made obviously). 

 

Also sorry to the OP for highjacking this thread :-)

Link to comment
Share on other sites

  • 4 weeks later...

 

Well after some digging on PS 1.7.0.4 we found that the JavascriptManager, StyleSheetManager and CccReducer classes completely ignore the media server settings. We are not sure if this is by design but if it is it seems odd. We have updated these files and we now have our JS and CSS being served from our CDN when the CCC options are on and off. 

 

I am not sure if I should submit these changes anywhere so others can benefit from them (if the PS team agrees the changes should be made obviously). 

 

Also sorry to the OP for highjacking this thread :-)

Can you share it?

Link to comment
Share on other sites

In classes\assets\AbstractAssetManager.php

 

protected $directories;

    protected $configuration;

    protected $list = array();

    protected $server;

    const DEFAULT_MEDIA = 'all';

    const DEFAULT_PRIORITY = 50;

    const DEFAULT_JS_POSITION = 'bottom';

 

    use PrestaShop\PrestaShop\Adapter\Assets\AssetUrlGeneratorTrait;

 

    public function __construct(array $directories, ConfigurationInterface $configuration, $server = 'local')

    {

        $this->directories = $directories;

        $this->configuration = $configuration;

        $this->server = $server;

        $this->list = $this->getDefaultList();

    }

 

In classes\assets\CccReducer.php

 

 

class CccReducerCore

{

    private $cacheDir;

    protected $filesystem;

    protected $server;

    use PrestaShop\PrestaShop\Adapter\Assets\AssetUrlGeneratorTrait;

 

    public function __construct($cacheDir, ConfigurationInterface $configuration, Filesystem $filesystem, $server = 'local')

    {

        $this->cacheDir = $cacheDir;

        $this->configuration = $configuration;

        $this->filesystem = $filesystem;

        $this->server = $server;

        if (!is_dir($this->cacheDir)) {

            $this->filesystem->mkdir($this->cacheDir);

        }

    }

 

    public function reduceCss($cssFileList)

    {

        $files = array();

        foreach ($cssFileList['external'] as $key => &$css) {

            if ('all' === $css['media'] && ('local' === $css['server'] || $this->server === $css['server'])) {

                $files[] = $css['path'];

                unset($cssFileList['external'][$key]);

            }

        }

 

        $cccFilename = 'theme-'.$this->getFileNameIdentifierFromList($files).'.css';

        $destinationPath = $this->cacheDir.$cccFilename;

 

        if (!$this->filesystem->exists($destinationPath)) {

            CssMinifier::minify($files, $destinationPath);

        }

 

        if ('local' === $this->server) {

            $uri = $this->getFQDN().$this->getUriFromPath($destinationPath);

        }

        else {

            $uri = $this->server.$this->getUriFromPath($destinationPath);

        }

 

        $cssFileList['external']['theme-ccc'] = [

            "id" => "theme-ccc",

            "type" => "external",

            "path" => $destinationPath,

            "uri" => $uri,

            "media" => "all",

            "priority" => StylesheetManager::DEFAULT_PRIORITY,

        ];

 

        return $cssFileList;

    }

 

    public function reduceJs($jsFileList)

    {

        foreach ($jsFileList as $position => &$list) {

            $files = array();

            foreach ($list['external'] as $key => $js) {

                // We only CCC the file without 'refer' or 'async'

                if ('' === $js['attribute'] && ('local' === $js['server'] || $this->server === $js['server'])) {

                    $files[] = $js['path'];

                    unset($list['external'][$key]);

                }

 

            }

 

            if (empty($files)) {

                // No file to CCC

                continue;

            }

 

            $cccFilename = $position.'-'.$this->getFileNameIdentifierFromList($files).'.js';

            $destinationPath = $this->cacheDir.$cccFilename;

 

            if (!$this->filesystem->exists($destinationPath)) {

                JsMinifier::minify($files, $destinationPath);

            }

            if ('local' === $this->server) {

                $uri = $this->getFQDN().$this->getUriFromPath($destinationPath);

            }

            else {

                $uri = $this->server.$this->getUriFromPath($destinationPath);

            }

 

            $cccItem = [];

            $cccItem[$position.'-js-ccc'] = [

                'id' => $position.'-js-ccc',

                'type' => 'external',

                'path' => $destinationPath,

                'uri' => $uri,

                'priority' => JavascriptManager::DEFAULT_PRIORITY,

                'attribute' => '',

            ];

            $list['external'] = array_merge($cccItem, $list['external']);

        }

 

        return $jsFileList;

    }

 

    private function getFileNameIdentifierFromList(array $files)

    {

        return substr(sha1(implode('|', $files)), 0, 6);

    }

}

 

In classes\assets\StylesheetManager.php

 

public function register(

        $id,

        $relativePath,

        $media = self::DEFAULT_MEDIA,

        $priority = self::DEFAULT_PRIORITY,

        $inline = false,

        $server = 'local'

    ) {

        if('' === $server){

            $server = $this->server;

        }

        if ('remote' === $server) {

            $this->add($id, $relativePath, $media, $priority, $inline, $server);

        } else if ($fullPath = $this->getFullPath($relativePath)) {

            $this->add($id, $fullPath, $media, $priority, $inline, $server);

        }

    }

 

 

 

 protected function add($id, $fullPath, $media, $priority, $inline, $server)

    {

        if ('remote' !== $server && filesize($fullPath) === 0) {

            return;

        }

 

        $priority = is_int($priority) ? $priority : self::DEFAULT_PRIORITY;

        $media = $this->getSanitizedMedia($media);

 

        if ('remote' === $server) {

            $uri = $fullPath;

            $type = 'external';

        } else if ('local' === $server) {

            $uri = $this->getFQDN().$this->getUriFromPath($fullPath);

            $type = ($inline) ? 'inline' : 'external';

        }

        else {

            $uri = $server.$this->getUriFromPath($fullPath);

            $type = ($inline) ? 'inline' : 'external';

        }

 

        

 

        $this->list[$type][$id] = array(

            'id' => $id,

            'type' => $type,

            'path' => $fullPath,

            'uri' => $uri,

            'media' => $media,

            'priority' => $priority,

            'server' => $server,

        );

    }

 

 

    In classes\assets\JavascriptManager.php

 

     public function register(

        $id,

        $relativePath,

        $position = self::DEFAULT_JS_POSITION,

        $priority = self::DEFAULT_PRIORITY,

        $inline = false,

        $attribute = null,

        $server = 'local'

    ) {

        if('' === $server){

            $server = $this->server;

        }

        if ('remote' === $server) {

            $this->add($id, $relativePath, $position, $priority, $inline, $attribute, $server);

        } else if ($fullPath = $this->getFullPath($relativePath)) {

            $this->add($id, $fullPath, $position, $priority, $inline, $attribute, $server);

        }

    }

 

     protected function add($id, $fullPath, $position, $priority, $inline, $attribute, $server)

    {

        if ('remote' !== $server && filesize($fullPath) === 0) {

            return;

        }

 

        $priority = is_int($priority) ? $priority : self::DEFAULT_PRIORITY;

        $position = $this->getSanitizedPosition($position);

        $attribute = $this->getSanitizedAttribute($attribute);

 

        if ('remote' === $server) {

            $uri = $fullPath;

            $type = 'external';

        } else if ('local' === $server) {

            $uri = $this->getFQDN().$this->getUriFromPath($fullPath);

            $type = ($inline) ? 'inline' : 'external';

        }

        else {

            $uri = $server.$this->getUriFromPath($fullPath);

            $type = ($inline) ? 'inline' : 'external';

        }

 

        $this->list[$position][$type][$id] = array(

            'id' => $id,

            'type' => $type,

            'path' => $fullPath,

            'uri' => $uri,

            'priority' => $priority,

            'attribute' => $attribute,

            'server' => $server,

        );

    }

 

 

    In classes\controllers\FrontController.php

 

    public function __construct()

    {

        $this->controller_type = 'front';

 

        global $useSSL;

 

        parent::__construct();

 

        if (Configuration::get('PS_SSL_ENABLED') && Configuration::get('PS_SSL_ENABLED_EVERYWHERE')) {

            $this->ssl = true;

        }

 

        $this->guestAllowed = Configuration::get('PS_GUEST_CHECKOUT_ENABLED');

 

        if (isset($useSSL)) {

            $this->ssl = $useSSL;

        } else {

            $useSSL = $this->ssl;

        }

        $mediaUrl = Tools::getMediaServer("/");

        if(substr($mediaUrl,0, 4) != "http"){

            $mediaUrl = Tools::getCurrentUrlProtocolPrefix() . $mediaUrl;

        }

        $this->objectPresenter = new ObjectPresenter();

        $this->cart_presenter = new CartPresenter();

        $this->templateFinder = new TemplateFinder($this->context->smarty->getTemplateDir(), '.tpl');

        $this->stylesheetManager = new StylesheetManager(

            array(_PS_THEME_DIR_, _PS_PARENT_THEME_DIR_, _PS_ROOT_DIR_),

            new ConfigurationAdapter(),

            $mediaUrl

        );

        $this->javascriptManager = new JavascriptManager(

            array(_PS_THEME_DIR_, _PS_PARENT_THEME_DIR_, _PS_ROOT_DIR_),

            new ConfigurationAdapter(),

            $mediaUrl

        );

        $this->cccReducer = new CccReducer(

            _PS_THEME_DIR_.'assets/cache/',

            new ConfigurationAdapter(),

            new Filesystem(),

            $mediaUrl

        );

    }

 

Link to comment
Share on other sites

  • 1 month later...

Media Servers and CDN module is an old technique, not working nowadays. Take a good hosting instead. You will not make it work. And therefore Prestashop removed this function from latest version (1.7) totally.

 

See also here the same discussion from post #7 on: https://www.prestashop.com/forums/topic/318759-performance-and-media-servers-setup-subdomains/?do=findComment&comment=2553622

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...