Skip to content
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

Image Trace double rendering and not replacing traced image #229

Closed
BrandonKlotz opened this issue Feb 5, 2021 · 3 comments
Closed

Image Trace double rendering and not replacing traced image #229

BrandonKlotz opened this issue Feb 5, 2021 · 3 comments

Comments

@BrandonKlotz
Copy link

BrandonKlotz commented Feb 5, 2021

This may totally be a misunderstanding of the setup but I was trying to implement images loading with an SVG trace and then being replaced with the png image.

// index.js
import Head from 'next/head'
import planet from '../public/planet-1.png?trace'

export default function Home() {
  return (
    <div>
      Hello world
      <div className="w-96 h-auto">
         <img src={planet.trace} />
         <img src={planet.src} />
      </div>
    </div>
  )
}
// next.config.js

const withOptimizedImages = require('next-optimized-images');

module.exports = withOptimizedImages({
  optimizeImagesInDev: true
});

Actual Result
Screen Shot 2021-02-05 at 4 08 25 PM

Am I totally missing something? 🤣

Link to full repo:

https://github.com/BrandonKlotz/next-optimized-images-demo

@BrandonKlotz
Copy link
Author

BrandonKlotz commented Feb 7, 2021

Made a small wrapper package to achieve what I was hoping for. Still curious why it wasn't working here.

https://github.com/BrandonKlotz/next-image-trace-loader

@vitbokisch
Copy link

@BrandonKlotz Well, you get both variants at the same time (original and trace) during compilation, and then you just render them both at the same time. That's why you see both of them. You miss either a condition to render only one of your images or in your component or you can replace it with your own hook to detect if the image is loaded, like this:

useEffect(() => {
      const originalImage = new Image()
      originalImage.src = src.original
      originalImage.onload = () => {
        setLoaded(true)
      }
    }, [src])

Hope it helps :)

@BrandonKlotz
Copy link
Author

Going to close this as a today I learned 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants