File tree Expand file tree Collapse file tree 4 files changed +444
-380
lines changed
Expand file tree Collapse file tree 4 files changed +444
-380
lines changed Original file line number Diff line number Diff line change @@ -3,11 +3,15 @@ import * as Utils from '@contentstack/utils';
33
44import ContentstackLivePreview from '@contentstack/live-preview-utils' ;
55import getConfig from 'next/config' ;
6+ import { customHostUrl , isValidCustomHostUrl } from "./utils"
67
78const { publicRuntimeConfig } = getConfig ( ) ;
89const envConfig = process . env . CONTENTSTACK_API_KEY
910 ? process . env
1011 : publicRuntimeConfig ;
12+
13+ const customHostBseUrl = customHostUrl ( envConfig . CONTENTSTACK_API_HOST )
14+
1115
1216const 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
3136ContentstackLivePreview . init ( {
Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments