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

Add a possibility to pass a fallback image url to the options #59

Open
Takheer opened this issue Jan 28, 2022 · 2 comments
Open

Add a possibility to pass a fallback image url to the options #59

Takheer opened this issue Jan 28, 2022 · 2 comments

Comments

@Takheer
Copy link

Takheer commented Jan 28, 2022

In my project, I need to get images via ipx, and if there is no such image, I would love to get some placeholder that I would have specified in options. So, is there any way to add such a thing, so the initialization would look like

import { createIPX, createIPXMiddleware } from "ipx";

const ipx = createIPX({
  /* other options */
  fallbackUrl: '/path/to/the/image.png'
});
const app = express();
app.use("/image", createIPXMiddleware(ipx));

Or is there already a way I could handle such 404 errors myself?

@Takheer
Copy link
Author

Takheer commented Feb 1, 2022

For those who could face a similar problem, I solved it by creating a node middleware that looks if there is an attempt to fetch something using the /_ipx route, and if the server doesn't have such an image, it will redirect to the specified placeholder:

import { existsSync } from 'fs';

export default function (req, res, next) {
  if (
    req.url.includes('/_ipx') &&
    !existsSync('static/img/path.png') // you can parse the req.url to get the path of the image of interest
  ) {
    res.redirect('path/to/the/placeholder.png');
  } else {
    next();
  }
}

Is it worth adding this to the docs to showcase to others how it can be solved?

@oemer-aran
Copy link

I have another issue where the user could upload a corrupted jpg file, IPX will throw an error at this case, since it can't parse the meta data to optimize the image. Can I fallback to the original image in this case?

Essentially I need to hook into the response of an ipx call in my nuxt-app. If the ipx call fails, i first want to try to get the original image and if that fails too, send an error. Is that possible?

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

No branches or pull requests

3 participants