Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@cloudflare/kv-asset-handler

Package Overview
Dependencies
Maintainers
0
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cloudflare/kv-asset-handler

Routes requests to KV assets

  • 0.3.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
509K
decreased by-57.67%
Maintainers
0
Weekly downloads
 
Created

What is @cloudflare/kv-asset-handler?

@cloudflare/kv-asset-handler is a package designed to help developers serve static assets from Cloudflare Workers using Cloudflare's KV (Key-Value) storage. It simplifies the process of handling requests for static assets, such as HTML, CSS, JavaScript, and images, by providing a straightforward API to fetch and serve these assets efficiently.

What are @cloudflare/kv-asset-handler's main functionalities?

Serve Static Assets

This feature allows you to serve static assets stored in Cloudflare's KV storage. The code sample demonstrates how to use the `getAssetFromKV` function to handle fetch events and return the requested asset.

const { getAssetFromKV } = require('@cloudflare/kv-asset-handler');

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event));
});

async function handleRequest(event) {
  try {
    const response = await getAssetFromKV(event);
    return response;
  } catch (e) {
    return new Response('Error fetching asset', { status: 500 });
  }
}

Custom Cache Control

This feature allows you to customize cache control settings for the assets served. The code sample shows how to set browser and edge TTL (Time To Live) values to control caching behavior.

const { getAssetFromKV, mapRequestToAsset } = require('@cloudflare/kv-asset-handler');

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event));
});

async function handleRequest(event) {
  try {
    const options = {
      cacheControl: {
        browserTTL: 60 * 60 * 24, // 1 day
        edgeTTL: 60 * 60 * 24 * 30, // 30 days
      },
    };
    const response = await getAssetFromKV(event, options);
    return response;
  } catch (e) {
    return new Response('Error fetching asset', { status: 500 });
  }
}

Custom Asset Mapping

This feature allows you to customize how requests are mapped to assets in KV storage. The code sample demonstrates how to modify the request URL to remove a '/static' prefix before fetching the asset.

const { getAssetFromKV, mapRequestToAsset } = require('@cloudflare/kv-asset-handler');

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event));
});

async function handleRequest(event) {
  try {
    const customKeyModifier = request => {
      let url = new URL(request.url);
      url.pathname = url.pathname.replace('/static', '');
      return new Request(url.toString(), request);
    };
    const response = await getAssetFromKV(event, { mapRequestToAsset: customKeyModifier });
    return response;
  } catch (e) {
    return new Response('Error fetching asset', { status: 500 });
  }
}

Other packages similar to @cloudflare/kv-asset-handler

Keywords

FAQs

Package last updated on 25 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc