Skip to content

Commit 051e21f

Browse files
committed
fix:setting custom base url [ECO-2924]
Signed-off-by: Amitkanswal <amit.kanswal@contentstack.com>
2 parents 5c96353 + 9c0f74b commit 051e21f

File tree

4 files changed

+444
-380
lines changed

4 files changed

+444
-380
lines changed

contentstack-sdk/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ import * as Utils from '@contentstack/utils';
33

44
import ContentstackLivePreview from '@contentstack/live-preview-utils';
55
import getConfig from 'next/config';
6+
import{customHostUrl, isValidCustomHostUrl} from "./utils"
67

78
const { publicRuntimeConfig } = getConfig();
89
const envConfig = process.env.CONTENTSTACK_API_KEY
910
? process.env
1011
: publicRuntimeConfig;
12+
13+
const customHostBseUrl = customHostUrl(envConfig.CONTENTSTACK_API_HOST)
14+
1115

1216
const Stack = contentstack.Stack({
1317
api_key: envConfig.CONTENTSTACK_API_KEY
@@ -24,8 +28,9 @@ const Stack = contentstack.Stack({
2428
},
2529
});
2630

27-
if (envConfig.CONTENTSTACK_API_HOST) {
28-
Stack.setHost(envConfig.CONTENTSTACK_API_HOST);
31+
// set host url only for custom host or non prod base url's
32+
if (isValidCustomHostUrl(customHostBseUrl)) {
33+
Stack.setHost(customHostBseUrl);
2934
}
3035

3136
ContentstackLivePreview.init({

contentstack-sdk/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Region } from "contentstack";
2+
export const customHostUrl = (baseUrl: string):string => {
3+
return baseUrl.replace("api", "cdn");
4+
};
5+
6+
export const generateUrlBasedOnRegion = (): string[] => {
7+
return Object.keys(Region).map((region) => {
8+
if (region === "US") {
9+
return `cdn.contentstack.io`;
10+
}
11+
return `${region}-cdn.contentstack.com`;
12+
});
13+
};
14+
15+
export const isValidCustomHostUrl = (url: string): boolean => {
16+
return url? !generateUrlBasedOnRegion().includes(url):false;
17+
};

0 commit comments

Comments
 (0)