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

loadImage doesn't follow redirect responses #7383

Open
2 of 17 tasks
msawired opened this issue Nov 20, 2024 · 0 comments
Open
2 of 17 tasks

loadImage doesn't follow redirect responses #7383

msawired opened this issue Nov 20, 2024 · 0 comments

Comments

@msawired
Copy link

msawired commented Nov 20, 2024

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • Build process
  • Unit testing
  • Internationalization
  • Friendly errors
  • Other (specify if possible)

p5.js version

1.11.1

Web browser and version

Safari 18.1

Operating system

MacOSX

Steps to reproduce this

Steps:

  1. find a URL that redirects to another resource for an image
  2. use this URL in loadImage()
  3. loadImage() will simply fail and will not proceed to redirected resource.

Snippet:

Here is an example: https://openprocessing.org/sketch/2012972

Suggested solution:

Add redirection to the loadImage function if the response status is 307. ChatGPT generated solution below:

p5.prototype.loadImage = function(path, successCallback, failureCallback) {

fetch(path, req)
  .then(response => {
    if (response.status === 307) {
      // Handle redirection
      const redirectUrl = response.headers.get('location');
      if (redirectUrl) {
        return fetch(redirectUrl, req); // Follow the redirection
      } else {
        throw new Error('Redirect URL not found in Location header.');
      }
    }
    return response; // Proceed with the original response if not redirected
  })
  .then(response => {
    // GIF section
    const contentType = response.headers.get('content-type');
    if (contentType === null) {
      console.warn(
        'The image you loaded does not have a Content-Type header. If you are using the online editor consider reuploading the asset.'
      );
    }
    ...

Potential Other Improvements

This solution can also be used to catch other responses (such as server errors, 404 file not founds...) and create more specific error messaging for friendly error messaging, instead of using the generic one for file loads.

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

1 participant