Suong Liang
Suong Liang

Reputation: 75

Images are not converted to .webp automatically

I'm trying to convert all images of a website built with UmbracoCMS (v7.15.7) by using the ImageProcessor Library defined in this article: https://piotrbach.com/enable-webp-image-format-in-umbraco-cms/

Actual result: request on browser returned 404 for that image.

  1. image tag returned as HTML:

    <img src="/media/37260/banner-web.jpg?anchor=center&amp;mode=crop&amp;width=1920&amp;height=640&amp;format=webp&amp;quality=80&amp;rnd=133270881000000000" class="img-responsive" alt="" width="1920" height="640">
    
  2. Browsers show 404 response & extension of this image remains .jpg:
    enter image description here

Have anyone met this situation before? I'd like to know the root cause/solution if any.

Upvotes: 1

Views: 324

Answers (1)

Suong Liang
Suong Liang

Reputation: 75

Answer update: I needed to add a rewrite rule on the Web config

<rewrite>
          <rules>
              <rule name="webp">
                  <match url="(.+)\.(jpe?g|png)$" ignoreCase="false" />
                  <conditions logicalGrouping="MatchAll">
                      <add input="{HTTP_ACCEPT}" pattern="image/webp" ignoreCase="false" />
                      <add input="{HTTP_USER_AGENT}" pattern="Chrome" />
                      <add input="{DOCUMENT_ROOT}/{R:0}.webp" matchType="IsFile" />
                  </conditions>
                  <action type="Rewrite" url="{R:0}.webp" logRewrittenUrl="true" />
                  <serverVariables>
                      <set name="ACCEPTS_WEBP" value="true" />
                  </serverVariables>
              </rule>
          </rules>
          <outboundRules rewriteBeforeCache="true">
              <rule name="jpg to webp" preCondition="ResponseIsHtml" patternSyntax="ExactMatch">
                  <match filterByTags="Img" pattern="(.+)\.(jpe?g|png)$" />
                  <action type="Rewrite" value="{R:0}.webp" />
              </rule>
              <preConditions>
                  <preCondition name="ResponseIsHtml">
                      <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                      <add input="{HTTP_USER_AGENT}" pattern="Chrome" />
                  </preCondition>
              </preConditions>
          </outboundRules>
      </rewrite>

then rebuilded the solution and worked like a charm

Upvotes: 0

Related Questions