Skip to content

Commit

Permalink
Fix image regeneration after changing image service (#6680)
Browse files Browse the repository at this point in the history
  • Loading branch information
koriwi authored Mar 28, 2023
1 parent adecda7 commit 3863364
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/lovely-owls-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/image': patch
---

Invalidates cache when changing serviceEntryPoint
7 changes: 5 additions & 2 deletions packages/astro/src/assets/utils/transformToPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import { shorthash } from '../../runtime/server/shorthash.js';
import { isESMImportedImage } from '../internal.js';
import type { ImageTransform } from '../types.js';

export function propsToFilename(transform: ImageTransform) {
export function propsToFilename(transform: ImageTransform, imageService: string) {
if (!isESMImportedImage(transform.src)) {
return transform.src;
}

let filename = removeQueryString(transform.src.src);
const ext = extname(filename);
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;
return `/${filename}_${shorthash(JSON.stringify(transform))}${outputExt}`;
return `/${filename}_${shorthash(JSON.stringify(hashFields))}${outputExt}`;
}
5 changes: 4 additions & 1 deletion packages/astro/src/assets/vite-plugin-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ export default function assets({
}

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);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/image/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export default function integration(options: IntegrationOptions = {}): AstroInte
? staticImages.get(transform.src)!
: new Map<string, TransformOptions>();

const filename = propsToFilename(transform);
const filename = propsToFilename(transform, resolvedOptions.serviceEntryPoint);

srcTranforms.set(filename, transform);
staticImages.set(transform.src, srcTranforms);
Expand Down
5 changes: 5 additions & 0 deletions packages/integrations/image/src/loaders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ export interface TransformOptions {
* For remote images, provide the full URL.
*/
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.
*
Expand Down
7 changes: 5 additions & 2 deletions packages/integrations/image/src/utils/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,19 @@ function basename(src: string) {
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
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);
const ext = extname(filename);
filename = removeExtname(filename);

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) {
Expand Down

0 comments on commit 3863364

Please sign in to comment.