Skip to content

Commit

Permalink
Merge pull request #3 from e3stpavel/works
Browse files Browse the repository at this point in the history
Fixed 'works' endpoint by adding '.json' extension
  • Loading branch information
e3stpavel authored Sep 25, 2024
2 parents a4ab2a9 + ac404a4 commit 0ffabde
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/components/product/ProductsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type ProductImage = Pick<GetImageResult, 'src' | 'attributes'>

async function fetcher() {
const host = import.meta.env.DEV ? 'http://localhost:4321' : import.meta.env.SITE
const response = await fetch(`${host}/api/works/`)
const response = await fetch(`${host}/api/works.json/`)

if (!response.ok) {
throw new Error('Failed to fetch products')
Expand Down
39 changes: 39 additions & 0 deletions src/pages/api/works.json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { APIRoute } from 'astro'
import { getImage } from 'astro:assets'

export const GET: APIRoute = async () => {
const productsImages = import.meta.glob<{ default: ImageMetadata }>([
'/src/assets/products/**/*.{jpeg,jpg,png}',
'!**/og/**/*.{jpeg,jpg,png}',
'!**/fallback.png',
'!**/works/*.{jpeg,jpg,png}',
], { eager: true })

const worksImages = import.meta.glob<{ default: ImageMetadata }>(
'/src/assets/products/works/*.{jpeg,jpg,png}',
{ eager: true },
)

// resolve only unused images
const resolved = await Promise.all(
Object.values(worksImages).map(
async ({ default: src }) => await getImage({ src }),
),
)

return new Response(
JSON.stringify([
...Object.values(productsImages)
.map(({ default: { src, width, height } }) => ({
src,
attributes: {
width,
height,
loading: 'lazy',
decoding: 'async',
},
})),
...resolved.map(({ src, attributes }) => ({ src, attributes })),
]),
)
}
27 changes: 0 additions & 27 deletions src/pages/api/works/index.ts

This file was deleted.

0 comments on commit 0ffabde

Please sign in to comment.