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

fix: support image src with no protocol #60

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

srindom
Copy link

@srindom srindom commented Aug 16, 2022

Was having issues with displaying images where all URLs are of the format: //domain.com/path-to-img

@changeset-bot
Copy link

changeset-bot bot commented Aug 16, 2022

⚠️ No Changeset found

Latest commit: c3d795b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link

vercel bot commented Aug 16, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
nextjs-starter-medusa ❌ Failed (Inspect) Aug 16, 2022 at 4:41PM (UTC)

Copy link
Collaborator

@kasperkristensen kasperkristensen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would properly extract this into a utility, and also if we are in a development environment console.error that we tried to fix the URL, but it might not have worked, as an example if the actual URL is http://something and it can't provide a secure connection.

const ensureSrc = (src: string) => {
    if (src.startsWith("//")) {
      if (process.env.NODE_ENV === "development") {
        console.error(
          "Tried to fix malformed URL by prefixing protocol. If the image still does not load then please fix your URL."
        )
      }

      return `https:${src}`
    }

    return src
  }

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

Successfully merging this pull request may close these issues.

None yet

2 participants