mirror of
https://github.com/withastro/astro.git
synced 2025-01-20 22:12:38 -05:00
Fix image regeneration after changing image service (#6680)
This commit is contained in:
parent
adecda7d60
commit
386336441a
6 changed files with 26 additions and 6 deletions
6
.changeset/lovely-owls-sniff.md
Normal file
6
.changeset/lovely-owls-sniff.md
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
'@astrojs/image': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Invalidates cache when changing serviceEntryPoint
|
|
@ -4,7 +4,7 @@ import { shorthash } from '../../runtime/server/shorthash.js';
|
||||||
import { isESMImportedImage } from '../internal.js';
|
import { isESMImportedImage } from '../internal.js';
|
||||||
import type { ImageTransform } from '../types.js';
|
import type { ImageTransform } from '../types.js';
|
||||||
|
|
||||||
export function propsToFilename(transform: ImageTransform) {
|
export function propsToFilename(transform: ImageTransform, imageService: string) {
|
||||||
if (!isESMImportedImage(transform.src)) {
|
if (!isESMImportedImage(transform.src)) {
|
||||||
return transform.src;
|
return transform.src;
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,9 @@ export function propsToFilename(transform: ImageTransform) {
|
||||||
let filename = removeQueryString(transform.src.src);
|
let filename = removeQueryString(transform.src.src);
|
||||||
const ext = extname(filename);
|
const ext = extname(filename);
|
||||||
filename = basename(filename, ext);
|
filename = basename(filename, ext);
|
||||||
|
// take everything from transform except alt, which is not used in the hash
|
||||||
|
const { alt, ...rest } = transform;
|
||||||
|
const hashFields = { ...rest, imageService };
|
||||||
const outputExt = transform.format ? `.${transform.format}` : ext;
|
const outputExt = transform.format ? `.${transform.format}` : ext;
|
||||||
return `/${filename}_${shorthash(JSON.stringify(transform))}${outputExt}`;
|
return `/${filename}_${shorthash(JSON.stringify(hashFields))}${outputExt}`;
|
||||||
}
|
}
|
||||||
|
|
|
@ -167,7 +167,10 @@ export default function assets({
|
||||||
}
|
}
|
||||||
|
|
||||||
filePath = prependForwardSlash(
|
filePath = prependForwardSlash(
|
||||||
joinPaths(settings.config.build.assets, propsToFilename(options))
|
joinPaths(
|
||||||
|
settings.config.build.assets,
|
||||||
|
propsToFilename(options, settings.config.image.service)
|
||||||
|
)
|
||||||
);
|
);
|
||||||
globalThis.astroAsset.staticImages.set(options, filePath);
|
globalThis.astroAsset.staticImages.set(options, filePath);
|
||||||
}
|
}
|
||||||
|
|
|
@ -122,7 +122,7 @@ export default function integration(options: IntegrationOptions = {}): AstroInte
|
||||||
? staticImages.get(transform.src)!
|
? staticImages.get(transform.src)!
|
||||||
: new Map<string, TransformOptions>();
|
: new Map<string, TransformOptions>();
|
||||||
|
|
||||||
const filename = propsToFilename(transform);
|
const filename = propsToFilename(transform, resolvedOptions.serviceEntryPoint);
|
||||||
|
|
||||||
srcTranforms.set(filename, transform);
|
srcTranforms.set(filename, transform);
|
||||||
staticImages.set(transform.src, srcTranforms);
|
staticImages.set(transform.src, srcTranforms);
|
||||||
|
|
|
@ -94,6 +94,11 @@ export interface TransformOptions {
|
||||||
* For remote images, provide the full URL.
|
* For remote images, provide the full URL.
|
||||||
*/
|
*/
|
||||||
src: string;
|
src: string;
|
||||||
|
/**
|
||||||
|
* The alt tag of the image. This is used for accessibility and will be made required in a future version.
|
||||||
|
* Empty string is allowed.
|
||||||
|
*/
|
||||||
|
alt?: string;
|
||||||
/**
|
/**
|
||||||
* The output format to be used in the optimized image.
|
* The output format to be used in the optimized image.
|
||||||
*
|
*
|
||||||
|
|
|
@ -35,16 +35,19 @@ function basename(src: string) {
|
||||||
return removeQueryString(src.replace(/^.*[\\\/]/, ''));
|
return removeQueryString(src.replace(/^.*[\\\/]/, ''));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function propsToFilename(transform: TransformOptions) {
|
export function propsToFilename(transform: TransformOptions, serviceEntryPoint: string) {
|
||||||
// strip off the querystring first, then remove the file extension
|
// strip off the querystring first, then remove the file extension
|
||||||
let filename = removeQueryString(transform.src);
|
let filename = removeQueryString(transform.src);
|
||||||
|
// take everything from transform except alt, which is not used in the hash
|
||||||
|
const { alt, ...rest } = transform;
|
||||||
|
const hashFields = { ...rest, serviceEntryPoint };
|
||||||
filename = basename(filename);
|
filename = basename(filename);
|
||||||
const ext = extname(filename);
|
const ext = extname(filename);
|
||||||
filename = removeExtname(filename);
|
filename = removeExtname(filename);
|
||||||
|
|
||||||
const outputExt = transform.format ? `.${transform.format}` : ext;
|
const outputExt = transform.format ? `.${transform.format}` : ext;
|
||||||
|
|
||||||
return `/${filename}_${shorthash(JSON.stringify(transform))}${outputExt}`;
|
return `/${filename}_${shorthash(JSON.stringify(hashFields))}${outputExt}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function appendForwardSlash(path: string) {
|
export function appendForwardSlash(path: string) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue