Skip to content

Commit ffe452f

Browse files
committed
fix(storefront): Prevent errors with undefined globalThis.astroAsset
1 parent 9c02c48 commit ffe452f

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

packages/storefront/src/images/use-ssr-picture.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ export type ImageSize = { width?: number, height?: number };
2929
export type TryImageSize = (src: string) => ImageSize;
3030

3131
const resetGlobalAstroAssets = () => {
32+
if (!globalThis.astroAsset) return;
3233
if (!globalThis.astroAsset.staticImages) {
3334
globalThis.astroAsset.staticImages = new Map();
35+
// @ts-expect-error: Internal `__ready`.
36+
} else if (globalThis.astroAsset.staticImages.get.__ready) {
37+
return;
3438
}
3539
const _get = globalThis.astroAsset.staticImages.get;
3640
const _set = globalThis.astroAsset.staticImages.set;
@@ -41,6 +45,8 @@ const resetGlobalAstroAssets = () => {
4145
}
4246
return _get.apply(globalThis.astroAsset.staticImages, args);
4347
};
48+
// @ts-expect-error: Internal `__ready`.
49+
globalThis.astroAsset.staticImages.get.__ready = true;
4450
globalThis.astroAsset.staticImages.set = function set(...args) {
4551
if (args[0].includes(publicDir)) {
4652
args[0] = args[0].replace(publicDir, '');
@@ -76,6 +82,7 @@ export type UsePictureParams = PictureProps & {
7682
};
7783

7884
const useSSRPicture = async (params: UsePictureParams) => {
85+
resetGlobalAstroAssets();
7986
const {
8087
src,
8188
alt,

0 commit comments

Comments
 (0)