Skip to content

Commit c27d26a

Browse files
authored
Merge pull request #48 from contentstack/feat/Release-Preview-Implementation
feat: implements release preview
2 parents 09f4d60 + f803ba8 commit c27d26a

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

src/lib/contentstack.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { InternalAxiosRequestConfig, AxiosRequestHeaders, AxiosResponse } from '
33
import { handleRequest } from './cache';
44
import { Stack as StackClass } from './stack';
55
import { Policy, StackConfig } from './types';
6-
import { getHost } from './utils';
6+
import * as Utility from './utils';
77
export * as Utils from '@contentstack/utils';
88

99
let version = '{{VERSION}}';
@@ -40,7 +40,7 @@ export function stack(config: StackConfig): StackClass {
4040
live_preview: {} as any
4141
};
4242

43-
defaultConfig.defaultHostname = config.host || getHost(config.region, config.host);
43+
defaultConfig.defaultHostname = config.host || Utility.getHost(config.region, config.host);
4444
config.host = defaultConfig.defaultHostname;
4545

4646
if (config.apiKey) {
@@ -59,6 +59,21 @@ export function stack(config: StackConfig): StackClass {
5959
throw new Error('Environment for Stack is required');
6060
}
6161

62+
if (config.live_preview) {
63+
if (Utility.isBrowser()) {
64+
const params = new URL(document.location.toString()).searchParams;
65+
if (params.has('live_preview')) {
66+
config.live_preview.live_preview = params.get('live_preview') || config.live_preview.live_preview;
67+
}
68+
if (params.has('release_id')) {
69+
defaultConfig.headers['release_id'] = params.get('release_id');
70+
}
71+
if (params.has('preview_timestamp')) {
72+
defaultConfig.headers['preview_timestamp'] = params.get('preview_timestamp');
73+
}
74+
}
75+
}
76+
6277
if (config.branch) {
6378
defaultConfig.headers.branch = config.branch;
6479
}

src/lib/stack.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,12 @@ export class Stack {
153153
}
154154
this._client.stackConfig.live_preview = livePreviewParams;
155155
}
156+
157+
if (query.hasOwnProperty('release_id')) {
158+
this._client.defaults.headers['release_id'] = query.release_id;
159+
}
160+
if (query.hasOwnProperty('preview_timestamp')) {
161+
this._client.defaults.headers['preview_timestamp'] = query.preview_timestamp;
162+
}
156163
}
157164
}

src/lib/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ export interface LivePreviewQuery {
261261
live_preview: string
262262
contentTypeUid: string
263263
entryUid?: any;
264+
preview_timestamp?: string
265+
release_id?: string
264266
}
265267

266268
export type LivePreview = {

src/lib/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ export function getHost(region: Region = Region.US, host?: string) {
1010

1111
return url;
1212
}
13+
14+
export function isBrowser() {
15+
return (typeof window !== "undefined");
16+
}

0 commit comments

Comments
 (0)