astro-get-remote-img
v1.2.0
Published
Astro integration to download remote assets from CMS and update HTML references during build
Downloads
902
Maintainers
Readme
astro-get-remote-img
An Astro integration that automatically downloads remote images from your CMS during the build process and updates HTML references to use local paths.
Installation
npm install astro-get-remote-imgUsage
Add the integration to your astro.config.mjs:
import { defineConfig } from 'astro/config';
import getRemoteAssets from 'astro-get-remote-img';
export default defineConfig({
integrations: [
getRemoteAssets({
url: 'https://images.microcms-assets.io',
imageDir: './images',
}),
],
});
// Or with multiple URLs
export default defineConfig({
integrations: [
getRemoteAssets({
url: ['https://images.microcms-assets.io', 'https://newt.io'],
imageDir: './images',
}),
],
});Configuration Options
url(string | string[]): The base URL(s) of your CMS image server to match and download images from. Can be a single URL or an array of URLs.imageDir(string): The directory where downloaded images will be saved (default:'./images')
How it Works
This integration:
- Runs after your Astro site builds
- Scans all HTML files in the build output
- Finds all
<img>and<source>tags withsrcorsrcsetattributes matching your CMS URL - Downloads these images to your local
imageDir - Updates the HTML to reference the local image paths
Example
If your HTML contains:
<img src="https://images.microcms-assets.io/assets/blog/hero.jpg" alt="Hero">After building, it will be updated to:
<img src="./images/cms/cms-image-[hash].jpg" alt="Hero">Features
- Automatic image downloading during build
- Handles both
srcandsrcsetattributes - Prevents duplicate downloads with caching
- Supports redirects
- 30-second timeout per image download
- Preserves image extensions when possible
- Creates unique filenames using MD5 hashes to avoid conflicts
