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.
Installation
Section titled “Installation”pnpm add ghettocdn-helpernpm install ghettocdn-helperyarn add ghettocdn-helperbun add ghettocdn-helperimport { GhettoCDN } from "ghettocdn-helper"
const cdn = await GhettoCDN.create("https://your-cdn.example.com")
// Single-format assetcdn.asset("config/config.json")// => "https://your-cdn.example.com/config/config.9c8d7e6f.json"
// Multi-format asset — pick a formatcdn.asset("image/logo.image", "webp")// => "https://your-cdn.example.com/image/logo.a1b2c3d4.webp"
// Multi-format asset — get first available formatcdn.asset("image/logo.image")// => "https://your-cdn.example.com/image/logo.a1b2c3d4.webp"GhettoCDN.create(endpoint: string)
Section titled “GhettoCDN.create(endpoint: string)”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.
cdn.refetch()
Section titled “cdn.refetch()”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()Manifest Format
Section titled “Manifest Format”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.)
{ "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.