Skip to content

Commit 50a33c7

Browse files
simonihmigschiller-manuel
authored andcommitted
docs: add with-responsive-image example app
1 parent 1a3bffa commit 50a33c7

File tree

19 files changed

+600
-14
lines changed

19 files changed

+600
-14
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# TanStack + ResponsiveImage
2+
3+
Integrating [ResponsiveImage](https://responsive-image.dev) with TanStack Start.
4+
5+
Run `pnpm dev` to run locally.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "with-responsive-image",
3+
"private": true,
4+
"sideEffects": false,
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite dev",
8+
"build": "vite build && tsc --noEmit",
9+
"start": "srvx --prod -s ../client dist/server/server.js"
10+
},
11+
"dependencies": {
12+
"@responsive-image/core": "^2.1.0",
13+
"@responsive-image/react": "^1.1.5",
14+
"@tanstack/react-router": "^1.134.15",
15+
"@tanstack/react-router-devtools": "^1.134.15",
16+
"@tanstack/react-start": "^1.134.15",
17+
"react": "^19.0.0",
18+
"react-dom": "^19.0.0"
19+
},
20+
"devDependencies": {
21+
"@responsive-image/vite-plugin": "^2.0.2",
22+
"@types/node": "^22.5.4",
23+
"@types/react": "^19.0.8",
24+
"@types/react-dom": "^19.0.3",
25+
"@vitejs/plugin-react": "^4.6.0",
26+
"srvx": "^0.8.16",
27+
"typescript": "^5.7.2",
28+
"vite": "^7.1.7",
29+
"vite-tsconfig-paths": "^5.1.4"
30+
}
31+
}
15 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare module '*responsive' {
2+
import type { ImageData } from '@responsive-image/core'
3+
4+
const value: ImageData
5+
export default value
6+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { notFound } from '@tanstack/react-router'
2+
import type { ImageData } from '@responsive-image/core'
3+
4+
const thumbnails = import.meta.glob<{ default: ImageData }>(
5+
'./images/gallery/*.jpg',
6+
{
7+
eager: true, // this is just generating image meta data, not need for lazy loading
8+
query: {
9+
w: '200;400',
10+
responsive: true, // opt into processing by @responsive-image/vite, see vite.config.ts. Without this, default vite asset handling applies.
11+
},
12+
},
13+
)
14+
15+
const images = import.meta.glob<{ default: ImageData }>(
16+
'./images/gallery/*.jpg',
17+
{
18+
eager: true, // this is just generating image meta data, not need for lazy loading
19+
query: {
20+
responsive: true, // opt into processing by @responsive-image/vite, see vite.config.ts. Without this, default vite asset handling applies.
21+
},
22+
},
23+
)
24+
25+
export function getThumbsnails(): Record<string, ImageData> {
26+
return Object.fromEntries(
27+
Object.entries(thumbnails).map(([imageId, module]) => [
28+
normalizeImageId(imageId),
29+
module.default,
30+
]),
31+
)
32+
}
33+
34+
export function getImage(imageId: string): ImageData {
35+
const module = images[denormalizeImageId(imageId)]
36+
37+
if (!module) {
38+
throw notFound({ data: { foo: 1 } })
39+
}
40+
41+
return module.default
42+
}
43+
44+
export function hasImage(imageId: string): boolean {
45+
return Boolean(images[denormalizeImageId(imageId)])
46+
}
47+
48+
// Remove leading `./images/gallery/` from import.meta.glob keys for nicer URLs
49+
function normalizeImageId(imageId: string): string {
50+
return imageId.replace(/^\.\/images\/gallery\//, '')
51+
}
52+
53+
function denormalizeImageId(imageId: string): string {
54+
return `./images/gallery/${imageId}`
55+
}
1.25 MB
Loading
5.31 MB
Loading
3.45 MB
Loading
2.24 MB
Loading
221 KB
Loading

0 commit comments

Comments
 (0)