-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
docs: add with-responsive-image example app #5070
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
simonihmig
wants to merge
3
commits into
TanStack:main
Choose a base branch
from
simonihmig:with-responsive-image
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,085
−1,177
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# TanStack + ResponsiveImage | ||
|
||
Integrating [ResponsiveImage](https://responsive-image.dev) with TanStack Start. | ||
|
||
Run `pnpm dev` to run locally. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "with-responsive-image", | ||
"private": true, | ||
"sideEffects": false, | ||
"type": "module", | ||
"scripts": { | ||
"dev": "vite dev", | ||
"build": "vite build && tsc --noEmit", | ||
"start": "vite start" | ||
}, | ||
"dependencies": { | ||
"@responsive-image/core": "^2.1.0", | ||
"@responsive-image/react": "^1.1.4", | ||
"@tanstack/react-router": "^1.132.47", | ||
"@tanstack/react-router-devtools": "^1.132.50", | ||
"@tanstack/react-start": "^1.132.48", | ||
"react": "^19.0.0", | ||
"react-dom": "^19.0.0" | ||
}, | ||
"devDependencies": { | ||
"@responsive-image/vite-plugin": "^2.0.0", | ||
"@types/node": "^22.5.4", | ||
"@types/react": "^19.0.8", | ||
"@types/react-dom": "^19.0.3", | ||
"@vitejs/plugin-react": "^4.3.4", | ||
"typescript": "^5.7.2", | ||
"vite": "^7.1.7", | ||
"vite-tsconfig-paths": "^5.1.4" | ||
} | ||
} |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
declare module '*responsive' { | ||
import type { ImageData } from '@responsive-image/core' | ||
|
||
const value: ImageData | ||
export default value | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { notFound } from '@tanstack/react-router' | ||
import type { ImageData } from '@responsive-image/core' | ||
|
||
const thumbnails = import.meta.glob<{ default: ImageData }>( | ||
'./images/gallery/*.jpg', | ||
{ | ||
eager: true, // this is just generating image meta data, not need for lazy loading | ||
query: { | ||
w: '200;400', | ||
responsive: true, // opt into processing by @responsive-image/vite, see vite.config.ts. Without this, default vite asset handling applies. | ||
}, | ||
}, | ||
) | ||
|
||
const images = import.meta.glob<{ default: ImageData }>( | ||
'./images/gallery/*.jpg', | ||
{ | ||
eager: true, // this is just generating image meta data, not need for lazy loading | ||
query: { | ||
responsive: true, // opt into processing by @responsive-image/vite, see vite.config.ts. Without this, default vite asset handling applies. | ||
}, | ||
}, | ||
) | ||
|
||
export function getThumbsnails(): Record<string, ImageData> { | ||
return Object.fromEntries( | ||
Object.entries(thumbnails).map(([imageId, module]) => [ | ||
normalizeImageId(imageId), | ||
module.default, | ||
]), | ||
) | ||
} | ||
|
||
export function getImage(imageId: string): ImageData { | ||
const module = images[denormalizeImageId(imageId)] | ||
|
||
if (!module) { | ||
throw notFound({ data: { foo: 1 } }) | ||
} | ||
|
||
return module.default | ||
} | ||
|
||
// Remove leading `./images/gallery/` from import.meta.glob keys for nicer URLs | ||
function normalizeImageId(imageId: string): string { | ||
return imageId.replace(/^\.\/images\/gallery\//, '') | ||
} | ||
|
||
function denormalizeImageId(imageId: string): string { | ||
return `./images/gallery/${imageId}` | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* eslint-disable */ | ||
|
||
// @ts-nocheck | ||
|
||
// noinspection JSUnusedGlobalSymbols | ||
|
||
// This file was automatically generated by TanStack Router. | ||
// You should NOT make any changes in this file as it will be overwritten. | ||
// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified. | ||
|
||
import { Route as rootRouteImport } from './routes/__root' | ||
import { Route as ImageIdRouteImport } from './routes/$imageId' | ||
import { Route as IndexRouteImport } from './routes/index' | ||
|
||
const ImageIdRoute = ImageIdRouteImport.update({ | ||
id: '/$imageId', | ||
path: '/$imageId', | ||
getParentRoute: () => rootRouteImport, | ||
} as any) | ||
const IndexRoute = IndexRouteImport.update({ | ||
id: '/', | ||
path: '/', | ||
getParentRoute: () => rootRouteImport, | ||
} as any) | ||
|
||
export interface FileRoutesByFullPath { | ||
'/': typeof IndexRoute | ||
'/$imageId': typeof ImageIdRoute | ||
} | ||
export interface FileRoutesByTo { | ||
'/': typeof IndexRoute | ||
'/$imageId': typeof ImageIdRoute | ||
} | ||
export interface FileRoutesById { | ||
__root__: typeof rootRouteImport | ||
'/': typeof IndexRoute | ||
'/$imageId': typeof ImageIdRoute | ||
} | ||
export interface FileRouteTypes { | ||
fileRoutesByFullPath: FileRoutesByFullPath | ||
fullPaths: '/' | '/$imageId' | ||
fileRoutesByTo: FileRoutesByTo | ||
to: '/' | '/$imageId' | ||
id: '__root__' | '/' | '/$imageId' | ||
fileRoutesById: FileRoutesById | ||
} | ||
export interface RootRouteChildren { | ||
IndexRoute: typeof IndexRoute | ||
ImageIdRoute: typeof ImageIdRoute | ||
} | ||
|
||
declare module '@tanstack/react-router' { | ||
interface FileRoutesByPath { | ||
'/$imageId': { | ||
id: '/$imageId' | ||
path: '/$imageId' | ||
fullPath: '/$imageId' | ||
preLoaderRoute: typeof ImageIdRouteImport | ||
parentRoute: typeof rootRouteImport | ||
} | ||
'/': { | ||
id: '/' | ||
path: '/' | ||
fullPath: '/' | ||
preLoaderRoute: typeof IndexRouteImport | ||
parentRoute: typeof rootRouteImport | ||
} | ||
} | ||
} | ||
|
||
const rootRouteChildren: RootRouteChildren = { | ||
IndexRoute: IndexRoute, | ||
ImageIdRoute: ImageIdRoute, | ||
} | ||
export const routeTree = rootRouteImport | ||
._addFileChildren(rootRouteChildren) | ||
._addFileTypes<FileRouteTypes>() | ||
|
||
import type { getRouter } from './router.tsx' | ||
import type { createStart } from '@tanstack/react-start' | ||
declare module '@tanstack/react-start' { | ||
interface Register { | ||
ssr: true | ||
router: Awaited<ReturnType<typeof getRouter>> | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { createRouter } from '@tanstack/react-router' | ||
import { routeTree } from './routeTree.gen' | ||
|
||
export function getRouter() { | ||
const router = createRouter({ | ||
routeTree, | ||
defaultPreload: 'intent', | ||
defaultErrorComponent: (err) => <p>{err.error.stack}</p>, | ||
defaultNotFoundComponent: () => <p>not found</p>, | ||
scrollRestoration: true, | ||
}) | ||
|
||
return router | ||
} | ||
|
||
declare module '@tanstack/react-router' { | ||
interface Register { | ||
router: ReturnType<typeof getRouter> | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
examples/react/with-responsive-image/src/routes/$imageId.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { createFileRoute } from '@tanstack/react-router' | ||
import { ResponsiveImage } from '@responsive-image/react' | ||
import { getImage } from '../images' | ||
|
||
export const Route = createFileRoute('/$imageId')({ | ||
loader: ({ params }) => getImage(params.imageId), | ||
component: Image, | ||
}) | ||
|
||
function Image() { | ||
const image = Route.useLoaderData() | ||
|
||
return <ResponsiveImage src={image} className="large" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add accessible alt text (and consider a remount key to work around the ThumbHash LQIP bug). Provide meaningful alt text; optionally key by imageId to force a remount when switching images. Apply: function Image() {
- const image = Route.useLoaderData()
+ const image = Route.useLoaderData()
+ const { imageId } = Route.useParams()
- return <ResponsiveImage src={image} className="large" />
+ return (
+ <ResponsiveImage
+ key={imageId}
+ src={image}
+ className="large"
+ alt={image.alt ?? image.title ?? imageId}
+ />
+ )
} Also applies to: 12-12 🤖 Prompt for AI Agents
|
||
} |
42 changes: 42 additions & 0 deletions
42
examples/react/with-responsive-image/src/routes/__root.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { | ||
Outlet, | ||
Link, | ||
createRootRoute, | ||
HeadContent, | ||
Scripts, | ||
} from '@tanstack/react-router' | ||
import { ResponsiveImage } from '@responsive-image/react' | ||
import { getThumbsnails } from '../images' | ||
import appCss from '~/styles.css?url' | ||
|
||
export const Route = createRootRoute({ | ||
head: () => ({ | ||
links: [{ rel: 'stylesheet', href: appCss }], | ||
}), | ||
component: App, | ||
}) | ||
|
||
function App() { | ||
return ( | ||
<html> | ||
<head> | ||
<HeadContent /> | ||
</head> | ||
<body> | ||
<div className="wrapper"> | ||
<aside> | ||
{Object.entries(getThumbsnails()).map(([imageId, image]) => ( | ||
<Link to="/$imageId" params={{ imageId }} key={imageId}> | ||
<ResponsiveImage src={image} width={200}></ResponsiveImage> | ||
</Link> | ||
))} | ||
</aside> | ||
<main> | ||
<Outlet /> | ||
</main> | ||
</div> | ||
<Scripts /> | ||
</body> | ||
</html> | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { createFileRoute } from '@tanstack/react-router' | ||
import { ResponsiveImage } from '@responsive-image/react' | ||
import visual from '../images/splash-dark.png?w=300;600&responsive' | ||
|
||
export const Route = createFileRoute('/')({ | ||
component: Index, | ||
}) | ||
|
||
function Index() { | ||
return ( | ||
<> | ||
<ResponsiveImage src={visual} width={300} /> | ||
<section className="intro"> | ||
<h1>Welcome to this TanStack Router + ResponsiveImage demo</h1> | ||
<ul> | ||
<li> | ||
<a | ||
className="App-link" | ||
href="https://tanstack.com" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
Learn TanStack | ||
</a> | ||
</li> | ||
<li> | ||
<a | ||
className="App-link" | ||
href="https://responsive-image.dev" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
Learn ResponsiveImage | ||
</a> | ||
</li> | ||
</ul> | ||
</section> | ||
</> | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
body { | ||
margin: 0; | ||
font-family: | ||
-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', | ||
'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
|
||
background-color: black; | ||
color: #ccc; | ||
} | ||
|
||
a { | ||
color: #00c2ab; | ||
} | ||
|
||
code { | ||
font-family: | ||
source-code-pro, Menlo, Monaco, Consolas, 'Courier New', monospace; | ||
} | ||
|
||
aside { | ||
display: flex; | ||
overflow: auto; | ||
justify-content: center; | ||
|
||
flex: 0 0 auto; | ||
|
||
margin-bottom: 5px; | ||
} | ||
|
||
aside a { | ||
border: solid 1px transparent; | ||
} | ||
|
||
aside a.active { | ||
border-color: #00c2ab; | ||
} | ||
|
||
img { | ||
display: block; | ||
} | ||
|
||
.wrapper { | ||
display: flex; | ||
flex-direction: column; | ||
width: 100%; | ||
height: 100vh; | ||
} | ||
|
||
main { | ||
flex: 1; | ||
} | ||
|
||
.intro { | ||
padding: 20px; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"include": ["**/*.ts", "**/*.tsx", "public/script*.js"], | ||
"compilerOptions": { | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"jsx": "react-jsx", | ||
"module": "ESNext", | ||
"moduleResolution": "Bundler", | ||
"lib": ["DOM", "DOM.Iterable", "ES2022"], | ||
"isolatedModules": true, | ||
"resolveJsonModule": true, | ||
"skipLibCheck": true, | ||
"target": "ES2022", | ||
"allowJs": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"baseUrl": ".", | ||
"paths": { | ||
"~/*": ["./src/*"] | ||
}, | ||
"noEmit": true, | ||
"types": ["vite/client"] | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.