Skip to content

Instantly share code, notes, and snippets.

@anatooly
Created May 19, 2025 18:32
Show Gist options
  • Select an option

  • Save anatooly/96413aea3b1bd223f87522e6a1c3677c to your computer and use it in GitHub Desktop.

Select an option

Save anatooly/96413aea3b1bd223f87522e6a1c3677c to your computer and use it in GitHub Desktop.
get-background-image-url.ts
const assetPrefix = process.env.NEXT_PUBLIC_ASSET_PREFIX || "";
export const ALLOWED_WIDTHS = [16, 32, 48, 64, 96, 128, 256, 384, 640, 750, 828, 1080, 1200, 1920, 2048, 3840] as const;
export type AllowedWidth = (typeof ALLOWED_WIDTHS)[number];
export const getBackgroundImageUrl = (path: string, width: AllowedWidth = 1920, quality = 75) => {
const encodedUrl = encodeURIComponent(path);
return `${assetPrefix}/_next/image?url=${encodedUrl}&w=${width}&q=${quality}`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment