Skip to content

Bunny Optimizer ​

The Dynamic Image API of Bunny Optimizer is supported by a helper function provided to you by this library.

Setup ​

Make sure you have the @responsive-image/cdn package installed:

bash
npm install @responsive-image/cdn
bash
yarn add @responsive-image/cdn
bash
pnpm add @responsive-image/cdn

In your bunny.net dashboard, enable Bunny Optimizer on your Pull Zone and turn on the Dynamic Image API under Optimizer → Settings. To let Bunny Optimizer pick the best format automatically, also enable WebP there.

Then set the hostname of your Pull Zone in your configuration, which you can set up in your application (e.g. app.js):

js
import { setConfig } from '@responsive-image/core';

setConfig('cdn', {
  bunny: {
    domain: 'my-zone.b-cdn.net',
  },
});

Usage ​

IMPORTANT

Please make sure you have read the section on remote images first.

Use the bunny provider function passing the path to the image on your Pull Zone, and pass the return value to the image component:

gjs
import { ResponsiveImage } from '@responsive-image/ember';
import { bunny } from '@responsive-image/cdn';

<template>
  <ResponsiveImage @src={{bunny 'path/to/image.jpg'}} />
</template>
hbs
<ResponsiveImage @src={{responsive-image-bunny 'path/to/image.jpg'}} />
ts
import { LitElement, html } from 'lit';
import { customElement } from 'lit/decorators.js';
import { bunny } from '@responsive-image/cdn';
import '@responsive-image/wc';

@customElement('my-app')
export class MyApp extends LitElement {
  render() {
    return html`<responsive-image
      .src=${bunny('path/to/image.jpg')}
    ></responsive-image>`;
  }
}
tsx
import { ResponsiveImage } from '@responsive-image/react';
import { bunny } from '@responsive-image/cdn';

export default function MyApp() {
  return <ResponsiveImage src={bunny('path/to/image.jpg')} />;
}
tsx
import { ResponsiveImage } from '@responsive-image/solid';
import { bunny } from '@responsive-image/cdn';

export default function MyApp() {
  return <ResponsiveImage src={bunny('path/to/image.jpg')} />;
}
svelte
<script>
  import { ResponsiveImage } from '@responsive-image/svelte';
  import { bunny } from '@responsive-image/cdn';
</script>

<ResponsiveImage src={bunny('path/to/image.jpg')} />
vue
<script setup>
import { ResponsiveImage } from '@responsive-image/vue';
import { bunny } from '@responsive-image/cdn';
</script>

<template>
  <ResponsiveImage :src="bunny('path/to/image.jpg')" />
</template>

You can also pass a full URL, for example to load an image from a different Pull Zone. The provider then ignores the configured domain:

js
bunny('https://other-zone.b-cdn.net/path/to/image.jpg');

Aspect Ratio ​

For the image component to be able to render width and height attributes to prevent layout shifts after loading has completed, it needs to know the aspect ratio of the source image. Unlike local images it cannot know this upfront for remote images, that's why it is recommended to supply the aspectRatio parameter if possible:

gjs
import { ResponsiveImage } from '@responsive-image/ember';
import { bunny } from '@responsive-image/cdn';

<template>
  <ResponsiveImage @src={{bunny 'path/to/image.jpg' aspectRatio=1.5}} />
</template>
hbs
<ResponsiveImage
  @src={{responsive-image-bunny 'path/to/image.jpg' aspectRatio=1.5}}
/>
ts
import { LitElement, html } from 'lit';
import { customElement } from 'lit/decorators.js';
import { bunny } from '@responsive-image/cdn';
import '@responsive-image/wc';

@customElement('my-app')
export class MyApp extends LitElement {
  render() {
    return html`<responsive-image
      .src=${bunny('path/to/image.jpg', {
        aspectRatio: 1.5,
      })}
    ></responsive-image>`;
  }
}
tsx
import { ResponsiveImage } from '@responsive-image/react';
import { bunny } from '@responsive-image/cdn';

export default function MyApp() {
  return (
    <ResponsiveImage
      src={bunny('path/to/image.jpg', {
        aspectRatio: 1.5,
      })}
    />
  );
}
tsx
import { ResponsiveImage } from '@responsive-image/solid';
import { bunny } from '@responsive-image/cdn';

export default function MyApp() {
  return (
    <ResponsiveImage
      src={bunny('path/to/image.jpg', {
        aspectRatio: 1.5,
      })}
    />
  );
}
svelte
<script>
  import { ResponsiveImage } from '@responsive-image/svelte';
  import { bunny } from '@responsive-image/cdn';
</script>

<ResponsiveImage
  src={bunny('path/to/image.jpg', {
    aspectRatio: 1.5,
  })}
/>

Custom parameters ​

Besides the width, format and quality parameters that the library adds itself, you can add any other Bunny Optimizer parameter by passing a params object:

gjs
import { ResponsiveImage } from '@responsive-image/ember';
import { bunny } from '@responsive-image/cdn';

<template>
  <ResponsiveImage
    @src={{bunny
      'path/to/image.jpg'
      params=(hash aspect_ratio='16:9' sharpen='true')
    }}
  />
</template>
hbs
<ResponsiveImage
  @src={{responsive-image-bunny
    'path/to/image.jpg'
    params=(hash aspect_ratio='16:9' sharpen='true')
  }}
/>
ts
import { LitElement, html } from 'lit';
import { customElement } from 'lit/decorators.js';
import { bunny } from '@responsive-image/cdn';
import '@responsive-image/wc';

@customElement('my-app')
export class MyApp extends LitElement {
  render() {
    return html`<responsive-image
      .src=${bunny('path/to/image.jpg', {
        params: { aspect_ratio: '16:9', sharpen: 'true' },
      })}
    ></responsive-image>`;
  }
}
tsx
import { ResponsiveImage } from '@responsive-image/react';
import { bunny } from '@responsive-image/cdn';

export default function MyApp() {
  return (
    <ResponsiveImage
      src={bunny('path/to/image.jpg', {
        params: { aspect_ratio: '16:9', sharpen: 'true' },
      })}
    />
  );
}
tsx
import { ResponsiveImage } from '@responsive-image/solid';
import { bunny } from '@responsive-image/cdn';

export default function MyApp() {
  return (
    <ResponsiveImage
      src={bunny('path/to/image.jpg', {
        params: { aspect_ratio: '16:9', sharpen: 'true' },
      })}
    />
  );
}
svelte
<script>
  import { ResponsiveImage } from '@responsive-image/svelte';
  import { bunny } from '@responsive-image/cdn';
</script>

<ResponsiveImage
  src={bunny('path/to/image.jpg', {
    params: { aspect_ratio: '16:9', sharpen: 'true' },
  })}
/>

If you crop the image with aspect_ratio, remember to pass the matching aspectRatio option too (e.g. aspectRatio: 16 / 9).

Quality ​

Use the quality parameter (0 to 100) to override the default quality, which is 85 or the value from your Pull Zone's Smart Image Optimization settings:

gjs
import { ResponsiveImage } from '@responsive-image/ember';
import { bunny } from '@responsive-image/cdn';

<template>
  <ResponsiveImage @src={{bunny 'path/to/image.jpg' quality=50}} />
</template>
hbs
<ResponsiveImage
  @src={{responsive-image-bunny 'path/to/image.jpg' quality=50}}
/>
ts
import { LitElement, html } from 'lit';
import { customElement } from 'lit/decorators.js';
import { bunny } from '@responsive-image/cdn';
import '@responsive-image/wc';

@customElement('my-app')
export class MyApp extends LitElement {
  render() {
    return html`<responsive-image
      .src=${bunny('path/to/image.jpg', {
        quality: 50,
      })}
    ></responsive-image>`;
  }
}
tsx
import { ResponsiveImage } from '@responsive-image/react';
import { bunny } from '@responsive-image/cdn';

export default function MyApp() {
  return (
    <ResponsiveImage
      src={bunny('path/to/image.jpg', {
        quality: 50,
      })}
    />
  );
}
tsx
import { ResponsiveImage } from '@responsive-image/solid';
import { bunny } from '@responsive-image/cdn';

export default function MyApp() {
  return (
    <ResponsiveImage
      src={bunny('path/to/image.jpg', {
        quality: 50,
      })}
    />
  );
}
svelte
<script>
  import { ResponsiveImage } from '@responsive-image/svelte';
  import { bunny } from '@responsive-image/cdn';
</script>

<ResponsiveImage
  src={bunny('path/to/image.jpg', {
    quality: 50,
  })}
/>

Image formats ​

By default the provider leaves out the format parameter, and Bunny Optimizer picks a format the browser supports. This only works if WebP is enabled in your Optimizer settings.

If you want a picture tag with one or more specific formats as source tags you can specify them using the formats argument. Supported formats are webp, avif, jpeg, png and gif:

gjs
import { ResponsiveImage } from '@responsive-image/ember';
import { bunny } from '@responsive-image/cdn';

<template>
  <ResponsiveImage
    @src={{bunny
      'path/to/image.jpg'
      formats=(array 'avif' 'webp')
    }}
  />
</template>
hbs
<ResponsiveImage
  @src={{responsive-image-bunny
    'path/to/image.jpg'
    formats=(array 'avif' 'webp')
  }}
/>
ts
import { LitElement, html } from 'lit';
import { customElement } from 'lit/decorators.js';
import { bunny } from '@responsive-image/cdn';
import '@responsive-image/wc';

@customElement('my-app')
export class MyApp extends LitElement {
  render() {
    return html`<responsive-image
      .src=${bunny('path/to/image.jpg', {
        formats: ['avif', 'webp'],
      })}
    ></responsive-image>`;
  }
}
tsx
import { ResponsiveImage } from '@responsive-image/react';
import { bunny } from '@responsive-image/cdn';

export default function MyApp() {
  return (
    <ResponsiveImage
      src={bunny('path/to/image.jpg', {
        formats: ['avif', 'webp'],
      })}
    />
  );
}
tsx
import { ResponsiveImage } from '@responsive-image/solid';
import { bunny } from '@responsive-image/cdn';

export default function MyApp() {
  return (
    <ResponsiveImage
      src={bunny('path/to/image.jpg', {
        formats: ['avif', 'webp'],
      })}
    />
  );
}
svelte
<script>
  import { ResponsiveImage } from '@responsive-image/svelte';
  import { bunny } from '@responsive-image/cdn';
</script>

<ResponsiveImage
  src={bunny('path/to/image.jpg', {
    formats: ['avif', 'webp'],
  })}
/>

NOTE

Bunny Optimizer only produces AVIF for images up to 4 megapixels and falls back to another format for larger ones.

Made with ❤︎ for OSS - Support 🇺🇦