-
Notifications
You must be signed in to change notification settings - Fork 628
Description
Expected Behavior
When rendering multiple next/image
components with different source URLs, each image should be uniquely processed and displayed, even when captured by HTML-to-image libraries.
Current Behavior
HTML-to-image libraries incorrectly cache Next.js optimized images based only on the base URL path (http://localhost:3000/_next/image
), ignoring the query parameters that make each image unique (?url=...&w=640&q=75
).
This results in all images appearing identical in the captured output, despite having different source URLs in the original React components.
Root Cause Analysis
Next.js Image component works by:
- Taking the original image source
- Creating an optimized version via an internal API route (
/_next/image
) - Passing parameters as query strings to specify dimensions, quality, etc.
HTML-to-image libraries typically:
- Fetch and cache external resources
- Use the resource URL as the cache key
- Fail to consider query parameters when determining uniqueness
The conflict occurs because the library treats all Next.js image URLs as identical since they share the same base path (/_next/image
), ignoring the distinguishing query parameters.
Possible Solutions
-
Use unoptimized images (trade-off solution):
<Image src={url} alt="..." unoptimized />
This bypasses Next.js optimization but ensures correct rendering in HTML-to-image.
-
Use native HTML img elements for captured content:
<img src={url} alt="..." />
Loses Next.js optimization benefits but works reliably with HTML-to-image.
Library Enhancement Request
The HTML-to-image library should be updated to:
- Consider full URLs including query parameters when caching resources
- Provide an option to disable caching for specific URL patterns
Reproduction Steps
- Go to the minimal reproduction: https://stackblitz.com/edit/stackblitz-starters-p8qimg5n?file=app%2Fpage.tsx
- Wait for Stackblitz to install dependencies and run the preview
- Press
Download as PNG
then observe how multiple distinct images are rendered identically when captured
If it fails to download, just refresh the Stackblitz container, it's an issue with Stackblitz.
Technical Environment
- Next.js version: >13
- HTML-to-image library: 1.11.13
- Browser: All modern browsers
- Platform: Cross-platform issue
Related Documentation
This issue highlights a fundamental incompatibility between Next.js image optimization and resource caching strategies in HTML-to-image