npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@greener-games/image-optimiser

v1.0.5

Published

Vue 3 plugin for caching and optimizing images

Readme

@greener-games/image-optimiser

A modular Vue 3 plugin that provides an optimized <CacheImage> component, reactive composables, and associated services to cache and transform images fetched from Hygraph, standard CDNs, or custom providers.

Features

  • 🖼️ Automated Sizing: Intelligently requests the correct image size based on the user's current viewport width.
  • 💾 Smart Caching: Caches downloaded images using the browser's Cache API to prevent redundant network requests.
  • ♻️ Cache Sibling Reuse: If a larger version of an image is already cached, it reuses that blob instead of requesting a smaller one.
  • 🧩 Zero Config Component: Includes the <CacheImage> Vue component which can be used as a drop-in replacement for <img>.
  • 🔌 Extensible Optimizers: Easily inject your own ICmsOptimizer logic for custom image transformations (e.g. Cloudinary, AWS, SharePoint).
  • 🛠️ Vue Composables: Built-in useImageOptimizer composable for fetching and resolving cached images reactively within <script setup>.

Installation

npm install @greener-games/image-optimiser

Setup & Configuration

Install the plugin in your Vue application instance using createMediaOptimizerPlugin(). You can pass in a configuration object to override defaults and inject custom optimizers.

import { createApp } from 'vue';
import App from './App.vue';
import { createMediaOptimizerPlugin, HygraphOptimizer, HygraphCacheIdentifier } from '@greener-games/image-optimiser';

const app = createApp(App);

// Initialize the plugin with optional configuration
app.use(createMediaOptimizerPlugin({
  enableCaching: true,
  enableOptimization: true,
  // Set the internal log level ('off' | 'low' | 'high')
  // 'low': Errors and warnings only
  // 'high': All cache hits/misses and network fetches
  logLevel: 'off',
  // Note: The plugin ships with NO active optimizers or cache identifiers by default.
  // You MUST inject the ones you wish to use.
  optimizers: [new HygraphOptimizer()],
  cacheIdentifiers: [new HygraphCacheIdentifier()] 
}));

app.mount('#app');

Creating a Custom Optimizer

The plugin ships with a HygraphOptimizer and a DefaultFallbackOptimizer. You can easily add your own by implementing the ICmsOptimizer interface and injecting it into the plugin configuration. The system will automatically use the first optimizer whose canHandle method returns true.

import type { ICmsOptimizer, TransformOptions } from '@greener-games/image-optimiser';

export class CustomCloudinaryOptimizer implements ICmsOptimizer {
  async canHandle(url: string): Promise<boolean> {
    return url.includes('res.cloudinary.com');
  }

  async optimize(url: string, options: TransformOptions): Promise<string> {
    // Implement your own URL transformation logic here!
    const widthParam = options.width ? `w_${options.width}` : '';
    // ... parse URL and inject parameters ...
    return `${url}?transform=${widthParam}`; 
  }
}

Creating a Custom Cache Identifier

To allow the smart caching layer to understand how to read dimensions and base URLs from your custom CMS URLs, you can inject custom ICmsCacheIdentifier classes.

import type { ICmsCacheIdentifier, AssetCacheInfo } from '@greener-games/image-optimiser';

export class CloudinaryCacheIdentifier implements ICmsCacheIdentifier {
  canHandle(url: string): boolean {
    return url.includes('res.cloudinary.com');
  }

  getAssetInfo(url: string): AssetCacheInfo {
    // Parse your URL to extract the base URL, requested width, and filename
    return {
      base: 'https://res.cloudinary.com/my-cloud/image',
      width: 800,
      name: 'my-image.jpg'
    };
  }
}

Usage

1. Template Component

Use the component in your templates as a simple drop-in replacement:

<template>
  <!-- Basic Usage (defaults to 'm' size) -->
  <CacheImage src="https://cdn.hygraph.com/your-image-id" alt="Description" />
  
  <!-- Explicit Logical Size (xs, s, m, lg, xl, 2xl, full) -->
  <CacheImage src="https://cdn.hygraph.com/your-image-id" size="xl" />
</template>

2. Vue Composable (Reactive)

If you need to resolve an image programmatically within your logic, use the provided composable:

<script setup lang="ts">
import { useImageOptimizer } from '@greener-games/image-optimiser';

const { displayUrl } = useImageOptimizer().useOptimizedImage('https://cdn.hygraph.com/your-image-id', 'lg');
</script>

<template>
  <div :style="{ backgroundImage: `url(${displayUrl})` }"></div>
</template>

3. Direct Services (Async)

You can also bypass the Vue reactivity system and use the services directly:

import { ImageCacheService, ImageOptimiser } from '@greener-games/image-optimiser';

// Get a transformed URL based on the active optimizers
const optimized = await ImageOptimiser.getOptimizedUrl('https://...', 'lg');

// Fetch and cache the URL, returning a blob URL for immediate use
const blobUrl = await ImageCacheService.getImageUrl(optimized);