RWOverdijk / AssetManager

AssetManager written for zf2. Managing assets for zend framework 2
BSD 2-Clause "Simplified" License
211 stars 83 forks source link

Caching/Processing assets without asset() view helper #209

Closed koseduhemak closed 5 years ago

koseduhemak commented 7 years ago

Hello,

I was wondering if Assetmanager can cache and apply filters on directly used URIs. F. e. I have an css file which contains a reference to an image (background-image). The image gets served and displayed, but it is not processed / cached via my configured filters. The image was placed in "module/Application/assets/images/404.jpg".

If I use the view helper $this->asset(path-to-image); in an src tag f. e. everything works fine.

Config:

'asset_manager' => array(
        'filter-config' => [
            'jpegtran' => [
                'copy' => JpegtranFilter::COPY_NONE,
                'optimize' => true,
                'progressive' => true
            ]
        ],
        'resolver_configs' => array(
            'paths' => array(
                __DIR__ . '/../assets',
            ),
        ),
        'view_helper' => array(
            //'cache'            => \Assetic\Cache\FilesystemCache::class, // You will need to require the factory used for the cache yourself.
            'append_timestamp' => true,                      // optional, if false never append a query param
            'query_string'     => '_ver',                       // optional
        ),
        'caching' => array(
            'default' => array(
                'cache' => \AssetManager\Cache\FilePathCache::class,  // Apc, FilePath, FileSystem etc.
                'options' => array(
                    'dir' => 'public', // path/to/cache
                ),
            ),
        ),
        'filters' => array(
            'image/jpeg' => [
                [
                    'service' => \Assetic\Filter\JpegtranFilter::class
                ]
            ],
            'js' => array(
                array(
                    'filter' => \Base\AssetFilter\JsMinFilter::class,
                ),
            ),
            'css' => [
                [
                    'filter' => \Base\AssetFilter\CssMinFilter::class
                ]
            ],
        ),
    ),

Any help would be appreciated.

wshafer commented 7 years ago

Using this config I was able to get the files to show up at http://myhost.com/myfile.js and it was stored correctly in the public folder but in the main folder (which in my opinion is bad).

What I might suggest instead of the paths resolver is to use an alias resolver. Just to keep naming collisions from happening as you grow.

    'asset_manager' => array(
        'filter-config' => [
            'jpegtran' => [
                'copy' => JpegtranFilter::COPY_NONE,
                'optimize' => true,
                'progressive' => true
            ]
        ],
        'resolver_configs' => array(
            'aliases' => array(
                'module/app/' => __DIR__ . '/../assets',
            ),
        ),
        'view_helper' => array(
            //'cache'            => \Assetic\Cache\FilesystemCache::class, // You will need to require the factory used for the cache yourself.
            'append_timestamp' => true,                      // optional, if false never append a query param
            'query_string'     => '_ver',                       // optional
        ),
        'caching' => array(
            'default' => array(
                'cache' => \AssetManager\Cache\FilePathCache::class,  // Apc, FilePath, FileSystem etc.
                'options' => array(
                    'dir' => 'public', // path/to/cache
                ),
            ),
        ),
        'filters' => array(
            'image/jpeg' => [
                [
                    'service' => \Assetic\Filter\JpegtranFilter::class
                ]
            ],
            'js' => array(
                array(
                    'filter' => \Base\AssetFilter\JsMinFilter::class,
                ),
            ),
            'css' => [
                [
                    'filter' => \Base\AssetFilter\CssMinFilter::class
                ]
            ],
        ),
    ),

This will get your file to resolve at http://myhost.com/module/app/myfile.js instead and get cached to pubic/module/app/myfile.js

Give that a try and see if it behaves more to your liking.

wshafer commented 7 years ago

Also don't forget to make sure the webserver has write permissions to the public folder. I'm sure you already checked that... but just making sure.

koseduhemak commented 7 years ago

Thank you sir. I will try your suggestions and use an alias resolver for each of my modules (to make sure naming conflicts do not happen).