Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Twig filter for resolve path to cache #1348

Merged
merged 1 commit into from
Oct 5, 2021
Merged

Conversation

peter-gribanov
Copy link
Contributor

@peter-gribanov peter-gribanov commented Jan 13, 2021

Q A
Branch? 2.0
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #1346
License MIT
Doc PR

Add a imagine_filter_cache new Twig filter what always return URL to cached image, even if there is no cache. Proper use of this filter can improve performance.

If you always warm up the cache, then you do not need to check the existence of the cached image every time when rendering templates. You can immediately use the link to the cached version of the image.

Also, you can avoid unnecessary controller calls if you use the local filesystem to store the image cache and use the same cache path as in the controller.

# /etc/nginx/nginx.conf
map $http_accept $webp_suffix {
    "~*webp"  ".webp";
}

server {
    # ...

    location ~ ^/media/cache {
        expires 365d;
        access_log off;
        add_header Cache-Control "public";
        try_files $uri$webp_suffix $uri /index.php$is_args$args;
    }

    # ...
}
# config/packages/liip_imagine.yaml
liip_imagine:
    resolvers:
       profile_photos:
          web_path:
            web_root: "%kernel.root_dir%/../public"
            cache_prefix: "media/cache"
    webp:
        generate: true
# config/routes/liip_imagine.yaml
liip_imagine_filter:
    path: /media/cache/{filter}/{path}
    defaults:
        _controller: '%liip_imagine.controller.filter_action%'
    methods:
        - GET
    requirements:
        filter: '[A-z0-9_-]*'
        path: .+

@coveralls
Copy link

coveralls commented Jan 13, 2021

Coverage Status

Coverage increased (+0.04%) to 84.104% when pulling 0e71655 on peter-gribanov:twig_filter into 7a1779b on liip:2.x.

@peter-gribanov peter-gribanov force-pushed the twig_filter branch 4 times, most recently from 61970c8 to 931072d Compare June 15, 2021 12:48
@peter-gribanov peter-gribanov changed the title Add Twig filters for generate URL and resolve path to cache Add Twig filter for resolve path to cache Jun 15, 2021
@peter-gribanov peter-gribanov force-pushed the twig_filter branch 3 times, most recently from ad0a182 to 524d17a Compare June 15, 2021 14:07
@dbu
Copy link
Member

dbu commented Oct 5, 2021

thanks a lot!

do you want to add the nginx trick to use this even without async cache warming to the documentation? (it would also prevent race conditions when a new image is uploaded and immediately looked at)

@peter-gribanov peter-gribanov deleted the twig_filter branch October 7, 2021 13:27
@peter-gribanov peter-gribanov mentioned this pull request Mar 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants