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:
npm install @responsive-image/cdnyarn add @responsive-image/cdnpnpm add @responsive-image/cdnIn 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):
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:
import { ResponsiveImage } from '@responsive-image/ember';
import { bunny } from '@responsive-image/cdn';
<template>
<ResponsiveImage @src={{bunny 'path/to/image.jpg'}} />
</template><ResponsiveImage @src={{responsive-image-bunny 'path/to/image.jpg'}} />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>`;
}
}import { ResponsiveImage } from '@responsive-image/react';
import { bunny } from '@responsive-image/cdn';
export default function MyApp() {
return <ResponsiveImage src={bunny('path/to/image.jpg')} />;
}import { ResponsiveImage } from '@responsive-image/solid';
import { bunny } from '@responsive-image/cdn';
export default function MyApp() {
return <ResponsiveImage src={bunny('path/to/image.jpg')} />;
}<script>
import { ResponsiveImage } from '@responsive-image/svelte';
import { bunny } from '@responsive-image/cdn';
</script>
<ResponsiveImage src={bunny('path/to/image.jpg')} /><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:
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:
import { ResponsiveImage } from '@responsive-image/ember';
import { bunny } from '@responsive-image/cdn';
<template>
<ResponsiveImage @src={{bunny 'path/to/image.jpg' aspectRatio=1.5}} />
</template><ResponsiveImage
@src={{responsive-image-bunny 'path/to/image.jpg' aspectRatio=1.5}}
/>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>`;
}
}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,
})}
/>
);
}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,
})}
/>
);
}<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:
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><ResponsiveImage
@src={{responsive-image-bunny
'path/to/image.jpg'
params=(hash aspect_ratio='16:9' sharpen='true')
}}
/>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>`;
}
}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' },
})}
/>
);
}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' },
})}
/>
);
}<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:
import { ResponsiveImage } from '@responsive-image/ember';
import { bunny } from '@responsive-image/cdn';
<template>
<ResponsiveImage @src={{bunny 'path/to/image.jpg' quality=50}} />
</template><ResponsiveImage
@src={{responsive-image-bunny 'path/to/image.jpg' quality=50}}
/>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>`;
}
}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,
})}
/>
);
}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,
})}
/>
);
}<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:
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><ResponsiveImage
@src={{responsive-image-bunny
'path/to/image.jpg'
formats=(array 'avif' 'webp')
}}
/>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>`;
}
}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'],
})}
/>
);
}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'],
})}
/>
);
}<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.