Skip to content

Commit

Permalink
feat: allow path encoding to be disabled for Imgix component
Browse files Browse the repository at this point in the history
  • Loading branch information
frederickfogerty committed Feb 28, 2022
1 parent c7b1273 commit 045bb42
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
18 changes: 9 additions & 9 deletions src/constructUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Minor syntax modifications have been made
*/

const PACKAGE_VERSION = require("../package.json").version;
import extractQueryParams from "./extractQueryParams";
import ImgixClient from "@imgix/js-core";
import extractQueryParams from "./extractQueryParams";

// @see https://www.imgix.com/docs/reference
var PARAM_EXPANSION = Object.freeze({
Expand Down Expand Up @@ -110,28 +110,28 @@ var DEFAULT_OPTIONS = Object.freeze({
* Construct a URL for an image with an Imgix proxy, expanding image options
* per the [API reference docs](https://www.imgix.com/docs/reference).
* @param {String} src src of raw image
* @param {Object} longOptions map of image API options, in long or short form per expansion rules
* @param {Object} longImgixParams map of image API options, in long or short form per expansion rules
* @return {String} URL of image src transformed by Imgix
*/
function constructUrl(src, longOptions) {
function constructUrl(src, longImgixParams, srcOptions) {
if (!src) {
return "";
}
const params = compactParamKeys(longOptions);
const params = compactParamKeys(longImgixParams);
const { client, pathComponents } = extractClientAndPathComponents(src);
return client.buildURL(pathComponents.join("/"), params);
return client.buildURL(pathComponents.join("/"), params, srcOptions);
}

function compactParamKeys(longOptions) {
const keys = Object.keys(longOptions);
function compactParamKeys(longImgixParams) {
const keys = Object.keys(longImgixParams);
const keysLength = keys.length;
const params = {};
for (let i = 0; i < keysLength; i++) {
let key = keys[i];
if (PARAM_EXPANSION[key]) {
params[PARAM_EXPANSION[key]] = longOptions[key];
params[PARAM_EXPANSION[key]] = longImgixParams[key];
} else {
params[key] = longOptions[key];
params[key] = longImgixParams[key];
}
}

Expand Down
22 changes: 13 additions & 9 deletions src/react-imgix.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const SHARED_IMGIX_AND_SOURCE_PROP_TYPES = Object.assign(
disableQualityByDPR: PropTypes.bool,
disableSrcSet: PropTypes.bool,
disableLibraryParam: PropTypes.bool,
disablePathEncoding: PropTypes.bool,
imgixParams: PropTypes.object,
sizes: PropTypes.string,
width: PropTypes.number,
Expand Down Expand Up @@ -103,6 +104,7 @@ function buildSrc({
height,
disableLibraryParam,
disableSrcSet,
disablePathEncoding,
imgixParams,
disableQualityByDPR,
srcSetOptions,
Expand All @@ -111,7 +113,7 @@ function buildSrc({

const [rawSrc, params] = extractQueryParams(inputSrc);

const srcOptions = Object.assign(
const srcImgixParams = Object.assign(
{},
params,
imgixParams,
Expand All @@ -120,15 +122,20 @@ function buildSrc({
fixedSize && width ? { width } : {}
);

const src = constructUrl(rawSrc, srcOptions);
const srcOptions = {
disablePathEncoding,
};

const src = constructUrl(rawSrc, srcImgixParams, srcOptions);

let srcSet;

if (disableSrcSet) {
srcSet = src;
} else {
const sharedSrcSetOptions = Object.assign({}, srcSetOptions, {disablePathEncoding});
if (fixedSize) {
const { width, w, height, h, q, ...urlParams } = srcOptions;
const { width, w, height, h, q, ...urlParams } = srcImgixParams;
if (q) {
urlParams["q"] = q;
}
Expand All @@ -144,18 +151,15 @@ function buildSrc({
urlParams["h"] = finalHeight;
}

srcSet = buildSrcSet(rawSrc, urlParams, {
disableVariableQuality: disableQualityByDPR,
...srcSetOptions,
});
srcSet = buildSrcSet(rawSrc, urlParams, Object.assign({disableVariableQuality: disableQualityByDPR}, sharedSrcSetOptions ));
} else {
const { width, w, height, h, ...urlParams } = srcOptions;
const { width, w, height, h, ...urlParams } = srcImgixParams;

const aspectRatio = imgixParams.ar;
let showARWarning =
aspectRatio != null && aspectRatioIsValid(aspectRatio) === false;

srcSet = buildSrcSet(rawSrc, urlParams, srcSetOptions);
srcSet = buildSrcSet(rawSrc, urlParams, sharedSrcSetOptions);

if (
NODE_ENV !== "production" &&
Expand Down

0 comments on commit 045bb42

Please sign in to comment.