Skip to content
Content is incomplete and some pages may be missing or low quality. Check back soon for updates.

Javascript

A javascript programmatic client for resolving assets built with ghettocdn. Fetches your CDN’s manifest and lets you resolve original asset names to their hashed, optimized output URLs.

Terminal window
pnpm add ghettocdn-helper
index.ts
import { GhettoCDN } from "ghettocdn-helper"
const cdn = await GhettoCDN.create("https://your-cdn.example.com")
// Single-format asset
cdn.asset("config/config.json")
// => "https://your-cdn.example.com/config/config.9c8d7e6f.json"
// Multi-format asset — pick a format
cdn.asset("image/logo.image", "webp")
// => "https://your-cdn.example.com/image/logo.a1b2c3d4.webp"
// Multi-format asset — get first available format
cdn.asset("image/logo.image")
// => "https://your-cdn.example.com/image/logo.a1b2c3d4.webp"

Fetches the manifest from {endpoint}/.ghettocdn/manifest.json and returns a GhettoCDN instance. This is the only way to construct an instance.

const cdn = await GhettoCDN.create("https://your-cdn.example.com")

cdn.asset(name: string, format?: string): string

Section titled “cdn.asset(name: string, format?: string): string”

Resolves an asset name to its CDN URL.

  • name — the asset key as it appears in the manifest (e.g. "image/logo.image", "config/config.json")
  • format — optional format to select for multi-format assets (e.g. "webp", "png")

If format is passed for a single-output asset, it is ignored with a console warning and the single URL is returned.

Throws if the asset name is not found in the manifest, or if the requested format is not available.

Re-fetches the manifest from the CDN endpoint and updates the instance in place. Useful for long-running processes where the CDN may have been redeployed.

await cdn.refetch()

ghettocdn-helper is designed to work with the manifest generated by ghettocdn. Asset keys follow the same conventions:

  • Multi-format assets (images, animations) use a generic type extension as their key (.image, .animation) so long as their transform pipeline is enabled
  • Single-format assets use their actual extension (.json, .yaml, .js, etc.)
manifest.json
{
"image/logo.image": {
"webp": "image/logo.a1b2c3d4.webp",
"png": "image/logo.e5f6a7b8.png"
},
"config/config.json": "config/config.9c8d7e6f.json"
}

For more information on the manifest, check out our manifest documenatation here.