Skip to content

Commit

Permalink
fix: Issue with base URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Amr Wagdy committed Nov 30, 2023
1 parent df5f663 commit 6d69778
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Types of changes:
-------------

-------------
## 1.2.3 - 2023-11-30
### Fixed
- base URL issue adding extra slash

## 1.2.2 - 2023-09-12
### Fixed
- Low preview image baseURL
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ Note: this will disregard your token above as this should be built into the CNAM

### baseURL

###### Type: **String** | Default: **"/"** | _optional_
###### Type: **String** | Default: **""** | _optional_

Your image folder on server, this alows to shorten your origin image URLs.

Expand Down
2 changes: 1 addition & 1 deletion demo/components/screen-info/screen-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function ScreenInfo({ footerRef }) {
<div className="label">
Your device pixel ratio:
<div className="window-width-box">
<span>{dpr}</span>
<span>{dpr.toFixed(2)}</span>
</div>
</div>
<div className="label">
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-cloudimage-responsive",
"version": "1.2.2",
"version": "1.2.3",
"private": false,
"scripts": {
"start": "next dev",
Expand Down
29 changes: 23 additions & 6 deletions src/utils/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ const parseParams = (params) => {
return _params;
};

const injectToUrl = (entryUrl, path, skipEndSlash = false) => {
if (entryUrl.endsWith('/')) return `${entryUrl}${path}`;

if (!entryUrl) return path;

if (skipEndSlash) return `${entryUrl}/${path}`;

return `${entryUrl}/${path}/`;
};


const parseImageSrc = ({
cName,
doNotReplaceURL,
Expand All @@ -23,16 +34,22 @@ const parseImageSrc = ({
params,
lowPreview,
}) => {
const isIncludesApiVersion = apiVersion && !doNotReplaceURL;
const allowApiVersion = apiVersion && !doNotReplaceURL;
const isIncludesWidthParam = WIDTH_PARAMS.some((widthParam) => src.includes(widthParam));
const _params = (lowPreview && params) ? params.replace(INFO_REGEX, '') : params;

let mainUrl = (!doNotReplaceURL && cName) ? `https://${cName}` : '';


if (allowApiVersion) {
mainUrl = injectToUrl(mainUrl, apiVersion);
}

mainUrl = injectToUrl(mainUrl, src, true);

return [
(!doNotReplaceURL && cName) ? `https://${cName}` : '',
isIncludesApiVersion ? `/${apiVersion}/` : '',
(doNotReplaceURL || src.startsWith('/')) ? '' : '/',
src,
src.includes('?') ? '&' : '?',
mainUrl,
mainUrl.includes('?') ? '&' : '?',
width && !isIncludesWidthParam ? `w=${width}&` : '',
lowPreview ? 'blur=80&' : '',
_params || '',
Expand Down

0 comments on commit 6d69778

Please sign in to comment.