Skip to content

Commit c609b12

Browse files
committed
fix:lp issue and env fix
1 parent 023f5ff commit c609b12

9 files changed

Lines changed: 290 additions & 137 deletions

File tree

.env.local.sample

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11

22
# This is a settings file for our application.
33

4-
# Contentstack is the tool we use to manage our website's content.
5-
# You need to replace 'your_stack_api_key', 'your_delivery_token', and 'your_environment_name' with the actual information.
6-
CONTENTSTACK_API_KEY=your_stack_api_key
7-
CONTENTSTACK_DELIVERY_TOKEN=your_delivery_token
8-
CONTENTSTACK_ENVIRONMENT=your_environment_name
4+
# Contentstack — use NEXT_PUBLIC_* only (available in browser + server via Next.js).
5+
NEXT_PUBLIC_CONTENTSTACK_API_KEY=your_stack_api_key
6+
NEXT_PUBLIC_CONTENTSTACK_DELIVERY_TOKEN=your_delivery_token
7+
NEXT_PUBLIC_CONTENTSTACK_ENVIRONMENT=your_environment_name
98

10-
# Live Preview lets us see changes before they are shown on the website.
11-
# Replace 'your_live_preview_token' with the actual information.
12-
CONTENTSTACK_PREVIEW_HOST=rest-preview.contentstack.com
13-
CONTENTSTACK_PREVIEW_TOKEN=your_live_preview_token
14-
CONTENTSTACK_APP_HOST=app.contentstack.com
15-
CONTENTSTACK_LIVE_PREVIEW=true
16-
CONTENTSTACK_LIVE_EDIT_TAGS=false
9+
# Live Preview
10+
NEXT_PUBLIC_CONTENTSTACK_PREVIEW_HOST=rest-preview.contentstack.com
11+
NEXT_PUBLIC_CONTENTSTACK_PREVIEW_TOKEN=your_live_preview_token
12+
NEXT_PUBLIC_CONTENTSTACK_APP_HOST=app.contentstack.com
13+
NEXT_PUBLIC_CONTENTSTACK_LIVE_PREVIEW=true
14+
NEXT_PUBLIC_CONTENTSTACK_LIVE_EDIT_TAGS=false
1715

18-
# These are extra settings. You can remove the '#' at the start of the line and fill these if needed.
19-
# CONTENTSTACK_API_HOST= api.contentstack.io
20-
# CONTENTSTACK_REGION=us
21-
# CONTENTSTACK_BRANCH=main
16+
# Optional (defaults in next.config.js if unset)
17+
# NEXT_PUBLIC_CONTENTSTACK_API_HOST=api.contentstack.io
18+
# NEXT_PUBLIC_CONTENTSTACK_REGION=us
19+
# NEXT_PUBLIC_CONTENTSTACK_BRANCH=main
2220

23-
#site-map
21+
# Site map
2422
NEXT_PUBLIC_HOSTED_URL=http://localhost:3000
2523

2624
# Notes:
27-
# - CONTENTSTACK_API_HOST: This is for setting a custom address for the Contentstack tool.
28-
# - CONTENTSTACK_REGION: This is for setting a custom region for the Contentstack tool (default is 'us').
29-
# - CONTENTSTACK_BRANCH: This is for setting a custom branch for the Contentstack tool (default is 'main').
30-
# - CONTENTSTACK_PREVIEW_HOST: If you're in the EU just append "eu-" to "rest-preview.contentstack.com"
31-
# - example eu-rest-preview.contentstack.com
25+
# - NEXT_PUBLIC_CONTENTSTACK_API_HOST: custom Contentstack API host if needed.
26+
# - NEXT_PUBLIC_CONTENTSTACK_REGION: default is us.
27+
# - NEXT_PUBLIC_CONTENTSTACK_BRANCH: default is main.
28+
# - NEXT_PUBLIC_CONTENTSTACK_PREVIEW_HOST: EU example — eu-rest-preview.contentstack.com
29+
# - Delivery + preview tokens are exposed in the client bundle; use preview-scoped tokens only.

contentstack-sdk/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ type GetEntryByUrl = {
1919
jsonRtePath: string[] | undefined;
2020
};
2121

22-
let customHostBaseUrl = process.env.CONTENTSTACK_API_HOST as string;
22+
let customHostBaseUrl = process.env
23+
.NEXT_PUBLIC_CONTENTSTACK_API_HOST as string;
2324

24-
customHostBaseUrl = customHostBaseUrl? customHostUrl(customHostBaseUrl): '';
25+
customHostBaseUrl = customHostBaseUrl
26+
? customHostUrl(customHostBaseUrl)
27+
: "";
2528

2629
// SDK initialization
2730
const Stack = initializeContentStackSdk();
@@ -35,10 +38,10 @@ if (!!customHostBaseUrl && isValidCustomHostUrl(customHostBaseUrl)) {
3538
ContentstackLivePreview.init({
3639
//@ts-ignore
3740
stackSdk: Stack,
38-
clientUrlParams:{
39-
host: process.env.CONTENTSTACK_APP_HOST,
41+
clientUrlParams: {
42+
host: process.env.NEXT_PUBLIC_CONTENTSTACK_APP_HOST,
4043
},
41-
ssr:false,
44+
ssr: false,
4245
})?.catch((err) => console.error(err));
4346

4447
export const { onEntryChange } = ContentstackLivePreview;

contentstack-sdk/utils.ts

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,80 @@
11
import { Config, Region, LivePreview, Stack } from "contentstack";
22

3-
const {
4-
CONTENTSTACK_API_KEY,
5-
CONTENTSTACK_DELIVERY_TOKEN,
6-
CONTENTSTACK_ENVIRONMENT,
7-
CONTENTSTACK_BRANCH,
8-
CONTENTSTACK_REGION,
9-
CONTENTSTACK_PREVIEW_TOKEN,
10-
CONTENTSTACK_PREVIEW_HOST,
11-
CONTENTSTACK_APP_HOST,
12-
CONTENTSTACK_LIVE_PREVIEW,
13-
} = process.env;
3+
/** All Contentstack config uses NEXT_PUBLIC_* (see .env.local.sample and next.config.js). */
4+
const apiKey = () => process.env.NEXT_PUBLIC_CONTENTSTACK_API_KEY;
5+
const deliveryToken = () => process.env.NEXT_PUBLIC_CONTENTSTACK_DELIVERY_TOKEN;
6+
const environment = () => process.env.NEXT_PUBLIC_CONTENTSTACK_ENVIRONMENT;
7+
const branch = () => process.env.NEXT_PUBLIC_CONTENTSTACK_BRANCH;
8+
const regionEnv = () => process.env.NEXT_PUBLIC_CONTENTSTACK_REGION;
9+
const previewToken = () => process.env.NEXT_PUBLIC_CONTENTSTACK_PREVIEW_TOKEN;
10+
const previewHost = () => process.env.NEXT_PUBLIC_CONTENTSTACK_PREVIEW_HOST;
11+
const appHost = () => process.env.NEXT_PUBLIC_CONTENTSTACK_APP_HOST;
12+
const livePreviewFlag = () => process.env.NEXT_PUBLIC_CONTENTSTACK_LIVE_PREVIEW;
13+
14+
const livePreviewEnabled = () => livePreviewFlag() === "true";
1415

1516
// basic env validation
1617
export const isBasicConfigValid = () => {
17-
return (
18-
!!CONTENTSTACK_API_KEY &&
19-
!!CONTENTSTACK_DELIVERY_TOKEN &&
20-
!!CONTENTSTACK_ENVIRONMENT
21-
);
18+
return !!(apiKey() && deliveryToken() && environment());
2219
};
20+
2321
// Live preview config validation
2422
export const isLpConfigValid = () => {
2523
return (
26-
!!CONTENTSTACK_LIVE_PREVIEW &&
27-
!!CONTENTSTACK_PREVIEW_TOKEN &&
28-
!!CONTENTSTACK_PREVIEW_HOST &&
29-
!!CONTENTSTACK_APP_HOST
24+
livePreviewEnabled() &&
25+
!!previewToken() &&
26+
!!previewHost() &&
27+
!!appHost()
3028
);
3129
};
30+
3231
// set region
3332
const setRegion = (): Region => {
3433
let region = "US" as keyof typeof Region;
35-
if (!!CONTENTSTACK_REGION && CONTENTSTACK_REGION !== "us") {
36-
region = CONTENTSTACK_REGION.toLocaleUpperCase().replace(
37-
"-",
38-
"_"
39-
) as keyof typeof Region;
34+
const r = regionEnv();
35+
if (!!r && r.toLowerCase() !== "us") {
36+
region = r.toUpperCase().replace(/-/g, "_") as keyof typeof Region;
4037
}
4138
return Region[region];
4239
};
40+
4341
// set LivePreview config
4442
const setLivePreviewConfig = (): LivePreview => {
4543
if (!isLpConfigValid())
46-
throw new Error("Your LP config is set to true. Please make you have set all required LP config in .env");
44+
throw new Error(
45+
"Live preview is enabled but required variables are missing. Set NEXT_PUBLIC_CONTENTSTACK_PREVIEW_TOKEN, PREVIEW_HOST, and APP_HOST in .env."
46+
);
4747
return {
48-
preview_token: CONTENTSTACK_PREVIEW_TOKEN as string,
49-
enable: CONTENTSTACK_LIVE_PREVIEW === "true",
50-
host: CONTENTSTACK_PREVIEW_HOST as string,
48+
preview_token: previewToken() as string,
49+
enable: true,
50+
host: previewHost() as string,
5151
} as LivePreview;
5252
};
53+
5354
// contentstack sdk initialization
5455
export const initializeContentStackSdk = (): Stack => {
5556
if (!isBasicConfigValid())
56-
throw new Error("Please set you .env file before running starter app");
57+
throw new Error(
58+
"Set NEXT_PUBLIC_CONTENTSTACK_API_KEY, DELIVERY_TOKEN, and ENVIRONMENT in .env"
59+
);
5760
const stackConfig: Config = {
58-
api_key: CONTENTSTACK_API_KEY as string,
59-
delivery_token: CONTENTSTACK_DELIVERY_TOKEN as string,
60-
environment: CONTENTSTACK_ENVIRONMENT as string,
61+
api_key: apiKey() as string,
62+
delivery_token: deliveryToken() as string,
63+
environment: environment() as string,
6164
region: setRegion(),
62-
branch: CONTENTSTACK_BRANCH || "main",
65+
branch: (branch() as string) || "main"
6366
};
64-
if (CONTENTSTACK_LIVE_PREVIEW === "true") {
67+
if (livePreviewEnabled()) {
6568
stackConfig.live_preview = setLivePreviewConfig();
6669
}
6770
return Stack(stackConfig);
6871
};
72+
6973
// api host url
7074
export const customHostUrl = (baseUrl: string): string => {
7175
return baseUrl.replace("api", "cdn");
7276
};
77+
7378
// generate prod api urls
7479
export const generateUrlBasedOnRegion = (): string[] => {
7580
return Object.keys(Region).map((region) => {
@@ -79,7 +84,8 @@ export const generateUrlBasedOnRegion = (): string[] => {
7984
return `${region}-cdn.contentstack.com`;
8085
});
8186
};
87+
8288
// prod url validation for custom host
83-
export const isValidCustomHostUrl = (url=''): boolean => {
89+
export const isValidCustomHostUrl = (url = ""): boolean => {
8490
return url ? !generateUrlBasedOnRegion().includes(url) : false;
8591
};

helper/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { Page, BlogPosts } from "../typescript/pages";
33
import { FooterProps, HeaderProps } from "../typescript/layout";
44
import { getEntry, getEntryByUrl } from "../contentstack-sdk";
55

6-
const liveEdit = process.env.CONTENTSTACK_LIVE_EDIT_TAGS === "true";
6+
const liveEdit =
7+
process.env.NEXT_PUBLIC_CONTENTSTACK_LIVE_EDIT_TAGS === "true";
78

89
export const getHeaderRes = async (): Promise<HeaderProps> => {
910
const response = (await getEntry({

next.config.js

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,44 @@
1+
const path = require("path");
2+
const { loadEnvConfig } = require("@next/env");
3+
4+
// Load .env / .env.local before reading process.env (so `env` below is populated).
5+
const projectDir = path.resolve(__dirname);
6+
loadEnvConfig(projectDir, process.env.NODE_ENV !== "production");
7+
18
const withPWA = require("next-pwa")({
29
dest: "public",
310
});
411

5-
/** Inlined for server + client (same role as former publicRuntimeConfig). */
12+
/** Contentstack: use NEXT_PUBLIC_* only in .env so values are available server + client. */
613
const config = {
14+
logging: {
15+
incomingRequests: false,
16+
browserToTerminal: false,
17+
},
718
env: {
8-
CONTENTSTACK_API_KEY: process.env.CONTENTSTACK_API_KEY,
9-
CONTENTSTACK_DELIVERY_TOKEN: process.env.CONTENTSTACK_DELIVERY_TOKEN,
10-
CONTENTSTACK_BRANCH: process.env.CONTENTSTACK_BRANCH || "main",
11-
CONTENTSTACK_REGION: process.env.CONTENTSTACK_REGION || "us",
12-
CONTENTSTACK_ENVIRONMENT: process.env.CONTENTSTACK_ENVIRONMENT,
13-
CONTENTSTACK_PREVIEW_TOKEN: process.env.CONTENTSTACK_PREVIEW_TOKEN,
14-
CONTENTSTACK_PREVIEW_HOST:
15-
process.env.CONTENTSTACK_PREVIEW_HOST || "rest-preview.contentstack.com",
16-
CONTENTSTACK_API_HOST:
17-
process.env.CONTENTSTACK_API_HOST || "api.contentstack.io",
18-
CONTENTSTACK_APP_HOST:
19-
process.env.CONTENTSTACK_APP_HOST || "app.contentstack.com",
20-
CONTENTSTACK_LIVE_PREVIEW: process.env.CONTENTSTACK_LIVE_PREVIEW || "true",
21-
CONTENTSTACK_LIVE_EDIT_TAGS:
22-
process.env.CONTENTSTACK_LIVE_EDIT_TAGS || "false",
19+
NEXT_PUBLIC_CONTENTSTACK_API_KEY:
20+
process.env.NEXT_PUBLIC_CONTENTSTACK_API_KEY,
21+
NEXT_PUBLIC_CONTENTSTACK_DELIVERY_TOKEN:
22+
process.env.NEXT_PUBLIC_CONTENTSTACK_DELIVERY_TOKEN,
23+
NEXT_PUBLIC_CONTENTSTACK_ENVIRONMENT:
24+
process.env.NEXT_PUBLIC_CONTENTSTACK_ENVIRONMENT,
25+
NEXT_PUBLIC_CONTENTSTACK_BRANCH:
26+
process.env.NEXT_PUBLIC_CONTENTSTACK_BRANCH || "main",
27+
NEXT_PUBLIC_CONTENTSTACK_REGION:
28+
process.env.NEXT_PUBLIC_CONTENTSTACK_REGION || "us",
29+
NEXT_PUBLIC_CONTENTSTACK_PREVIEW_TOKEN:
30+
process.env.NEXT_PUBLIC_CONTENTSTACK_PREVIEW_TOKEN,
31+
NEXT_PUBLIC_CONTENTSTACK_PREVIEW_HOST:
32+
process.env.NEXT_PUBLIC_CONTENTSTACK_PREVIEW_HOST ||
33+
"rest-preview.contentstack.com",
34+
NEXT_PUBLIC_CONTENTSTACK_API_HOST:
35+
process.env.NEXT_PUBLIC_CONTENTSTACK_API_HOST || "api.contentstack.io",
36+
NEXT_PUBLIC_CONTENTSTACK_APP_HOST:
37+
process.env.NEXT_PUBLIC_CONTENTSTACK_APP_HOST || "app.contentstack.com",
38+
NEXT_PUBLIC_CONTENTSTACK_LIVE_PREVIEW:
39+
process.env.NEXT_PUBLIC_CONTENTSTACK_LIVE_PREVIEW || "true",
40+
NEXT_PUBLIC_CONTENTSTACK_LIVE_EDIT_TAGS:
41+
process.env.NEXT_PUBLIC_CONTENTSTACK_LIVE_EDIT_TAGS || "false",
2342
},
2443
experimental: { largePageDataBytes: 128 * 100000 },
2544
};

0 commit comments

Comments
 (0)