Skip to content

Commit

Permalink
Merge pull request #77 from contentstack/mktplc-nextjs-demo
Browse files Browse the repository at this point in the history
Mktplc nextjs demo
  • Loading branch information
Amitkanswal authored May 23, 2023
2 parents 9e2c345 + 051e21f commit 1af4b83
Show file tree
Hide file tree
Showing 4 changed files with 444 additions and 380 deletions.
9 changes: 7 additions & 2 deletions contentstack-sdk/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import * as Utils from '@contentstack/utils';

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

const { publicRuntimeConfig } = getConfig();
const envConfig = process.env.CONTENTSTACK_API_KEY
? process.env
: publicRuntimeConfig;

const customHostBseUrl = customHostUrl(envConfig.CONTENTSTACK_API_HOST)


const Stack = contentstack.Stack({
api_key: envConfig.CONTENTSTACK_API_KEY
Expand All @@ -24,8 +28,9 @@ const Stack = contentstack.Stack({
},
});

if (envConfig.CONTENTSTACK_API_HOST) {
Stack.setHost(envConfig.CONTENTSTACK_API_HOST);
// set host url only for custom host or non prod base url's
if (isValidCustomHostUrl(customHostBseUrl)) {
Stack.setHost(customHostBseUrl);
}

ContentstackLivePreview.init({
Expand Down
17 changes: 17 additions & 0 deletions contentstack-sdk/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Region } from "contentstack";
export const customHostUrl = (baseUrl: string):string => {
return baseUrl.replace("api", "cdn");
};

export const generateUrlBasedOnRegion = (): string[] => {
return Object.keys(Region).map((region) => {
if (region === "US") {
return `cdn.contentstack.io`;
}
return `${region}-cdn.contentstack.com`;
});
};

export const isValidCustomHostUrl = (url: string): boolean => {
return url? !generateUrlBasedOnRegion().includes(url):false;
};
Loading

0 comments on commit 1af4b83

Please sign in to comment.