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

Support generating relative URLs #23

Open
onomated opened this issue Oct 15, 2023 · 0 comments
Open

Support generating relative URLs #23

onomated opened this issue Oct 15, 2023 · 0 comments

Comments

@onomated
Copy link

onomated commented Oct 15, 2023

This currently throws a TypeError if config.baseUrl is not an absolute URL here. There has been an ongoing debate on adding support for relative URLs to the URL class. My current setup has to work with relative URLs as it's a backing server to multiple domains. It will be great to provide a flexible way to build the final URL.

A suggestion would be to support a url builder function in the config, for example:

export interface ImgproxyConfig {
  baseUrl: string;
  insecure?: boolean | string;
  urlBuilder?: (baseUrl: string, signature: string, uri: string): string
}

To work around this, I provide a placeholder url in the config and replace it after imgproxy url generation. It works great with relative urls:

const imageServerUrl = "/image-server/";
const urlHack = "https://www.example.com/";
const reverseHack = (generatedUrl: string) => {
  if (generatedUrl.startsWith(urlHack)) {
    return `${imageServerUrl}${generatedUrl.substring(urlHack.length)}`;
  }
  return generatedUrl;
};

const imgproxy = new ImageProxy({
  baseUrl: urlHack,
  key,
  salt,
  encode: true,
});

const imgUrl = reverseHack(imgproxy.builder().generateUrl(cdnUrl));
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

1 participant