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

Putting CDN base URL for urlPath changes https:// to https:/ #239

Open
danburzo opened this issue Aug 7, 2024 · 2 comments
Open

Putting CDN base URL for urlPath changes https:// to https:/ #239

danburzo opened this issue Aug 7, 2024 · 2 comments

Comments

@danburzo
Copy link
Contributor

danburzo commented Aug 7, 2024

I’m trying to prefix the src/srcset attributes of the markup generated by Image.generateHTML with the base URL for a CDN (.e.g. https://mycdn.com/). However, sending that value to parseUrl results in the https:/mycdn.com prefix (with a single slash). I assume this function is the culprit, as Node’s path.join() is not equipped to deal with URLs.

static convertFilePathToUrl(dir, filename) {
    let src = path.join(dir, filename);
    return src.split(path.sep).join("/");
  }

Should we make this function work for the case where dir is a valid URL?


Relevant .eleventy.js snippet:

let metadata = await Image(inputPath, {
	urlPath: `https://dans-cdn.com/img/`,
	// ...
});

const markup = Image.generateHTML(metadata, { ... });
@danburzo
Copy link
Contributor Author

danburzo commented Aug 7, 2024

Note: For a quick solution, since ImagePath is exported, the function can be monkey-patched, e.g.

const prevImplementation = ImagePath.convertFilePathToUrl;
ImagePath.convertFilePathToUrl = function(dir, filename) {
	if (/https?:\/\//.test(dir)) {
		return new URL(filename, dir).toString();
	}
	return prevImplementation(...arguments);
}

@chmking
Copy link

chmking commented Sep 30, 2024

Ran into the same issue. Workaround solved it for now. Thanks!

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