Skip to content

Commit 04c4f0a

Browse files
committed
remove gallery api endpoints, use import.meta
1 parent cf3f1e4 commit 04c4f0a

File tree

10 files changed

+135
-232
lines changed

10 files changed

+135
-232
lines changed

docs/working-notes/todo4.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ placeholder - blur, bg-color, single // extract line
7272
img with srcset
7373

7474
rename all-images to gallery-images
75+
endpoint to create open graph images, because they dont exist in the file system
76+
77+
// only in dev
78+
http://localhost:3000/_image?href=/@fs/home/username/Desktop/nemanjam.github.io/src/assets/images/all-images/morning1.jpg?origWidth=4608&origHeight=2592&origFormat=jpg&w=1280&h=720&f=webp
79+
80+
// in prod
81+
http://localhost:3000/_astro/focus1.CEdGhKb3_nVk9T.webp
82+
7583
```
7684

7785

src/constants/image.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export const TW_SCREENS = {
2525
// add quality and loading in future
2626
export const IMAGE_SIZES = {
2727
FIXED: {
28+
BLUR: {
29+
width: 10,
30+
height: 10,
31+
},
2832
AVATAR: {
2933
width: 48,
3034
height: 48,

src/libs/api/gallery/image-path.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/libs/api/gallery/images.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/libs/gallery/images.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { EXCLUDE_IMAGES } from '@/constants/gallery';
2+
import { imageMetadataToImageProps } from '@/libs/gallery/transform';
3+
4+
import type { GalleryImageProps, HeroImageProps, ImageProps } from '@/libs/gallery/transform';
5+
import type { ImageMetadata } from 'astro';
6+
7+
// no need for API route, image server already exists, it will only rewrite url
8+
9+
export const getGalleryImagesMetadata = (): ImageMetadata[] => {
10+
const imageModules = import.meta.glob<{ default: ImageMetadata }>(
11+
// cant be even variable
12+
'/src/assets/images/all-images/*.jpg',
13+
{ eager: true }
14+
);
15+
16+
// convert map to array
17+
const imagesMetadata = Object.keys(imageModules)
18+
// filter excluded filenames
19+
.filter((path) => !EXCLUDE_IMAGES.some((excludedFileName) => path.endsWith(excludedFileName)))
20+
// return metadata array
21+
.map((path) => imageModules[path].default);
22+
23+
return imagesMetadata;
24+
};
25+
26+
export const getImagesProps = async (): Promise<ImageProps[]> =>
27+
Promise.all(getGalleryImagesMetadata().map(imageMetadataToImageProps));
28+
29+
export const getHeroImagesProps = (): Promise<HeroImageProps[]> =>
30+
getImagesProps().then((imagesProps) =>
31+
imagesProps.map((imageProps) => ({
32+
blur: imageProps.blur,
33+
hero: imageProps.hero,
34+
}))
35+
);
36+
37+
export const getGalleryImagesProps = (): Promise<GalleryImageProps[]> =>
38+
getImagesProps().then((imagesProps) =>
39+
imagesProps.map((imageProps) => ({
40+
blur: imageProps.blur,
41+
thumbnail: imageProps.thumbnail,
42+
lightbox: imageProps.lightbox,
43+
}))
44+
);

src/libs/gallery/transform.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { getImage } from 'astro:assets';
2+
3+
import { IMAGE_SIZES } from '@/constants/image';
4+
5+
import type { GetImageResult, ImageMetadata } from 'astro';
6+
7+
export interface ImageAttributes {
8+
src: string;
9+
width: number;
10+
height: number;
11+
}
12+
13+
export interface ImageProps {
14+
blur: ImageAttributes;
15+
color?: ImageAttributes;
16+
thumbnail: ImageAttributes;
17+
lightbox: ImageAttributes; // fullSize, 1280x720
18+
hero: ImageAttributes;
19+
}
20+
21+
export type HeroImageProps = Pick<ImageProps, 'blur' | 'hero'>;
22+
23+
export type GalleryImageProps = Pick<ImageProps, 'blur' | 'thumbnail' | 'lightbox'>;
24+
25+
export const imageMetadataToImageProps = async (
26+
imagesMetadata: ImageMetadata
27+
): Promise<ImageProps> => {
28+
const astroImageProps = {
29+
src: imagesMetadata,
30+
format: 'webp',
31+
};
32+
33+
const blurAstroImageProps = {
34+
...astroImageProps,
35+
...IMAGE_SIZES.FIXED.BLUR,
36+
alt: 'Blur image',
37+
};
38+
39+
const thumbnailAstroImageProps = {
40+
...astroImageProps,
41+
...IMAGE_SIZES.FIXED.MDX_XS_16_9,
42+
alt: 'Thumbnail image',
43+
};
44+
45+
const lightboxAstroImageProps = {
46+
...astroImageProps,
47+
...IMAGE_SIZES.FIXED.MDX_XL_16_9,
48+
alt: 'Lightbox image',
49+
};
50+
51+
const heroAstroImageProps = {
52+
...lightboxAstroImageProps,
53+
alt: 'Hero image',
54+
};
55+
56+
const optimizedBlurAstroImageProps = await getImage(blurAstroImageProps);
57+
const optimizedThumbnailAstroImageProps = await getImage(thumbnailAstroImageProps);
58+
const optimizedLightboxAstroImageProps = await getImage(lightboxAstroImageProps);
59+
const optimizedHeroAstroImageProps = await getImage(heroAstroImageProps);
60+
61+
const imageProps = {
62+
blur: getImageAttributes(optimizedBlurAstroImageProps),
63+
thumbnail: getImageAttributes(optimizedThumbnailAstroImageProps),
64+
lightbox: getImageAttributes(optimizedLightboxAstroImageProps),
65+
hero: getImageAttributes(optimizedHeroAstroImageProps),
66+
};
67+
68+
return imageProps;
69+
};
70+
71+
/*-------------------------------- utils ------------------------------*/
72+
73+
const getImageAttributes = (image: GetImageResult): ImageAttributes => ({
74+
src: image.src,
75+
width: parseInt(String(image.attributes.width)),
76+
height: parseInt(String(image.attributes.height)),
77+
});

src/pages/api/gallery/[...route].ts

Lines changed: 0 additions & 137 deletions
This file was deleted.

src/pages/api/home/[...route].ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/pages/api/open-graph/[...route].png.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const getStaticPaths = async () => {
2828
return paths;
2929
};
3030

31+
// endpoint to create open graph images, because they don't exist in the file system
3132
export const GET: APIRoute = async ({ props }: APIContext) => {
3233
// limit number of chars
3334
const { title, heroImage, pageId } = props.page;

0 commit comments

Comments
 (0)