Skip to content

Commit ac7003c

Browse files
Fixed timeline preview issue
1 parent 8287465 commit ac7003c

File tree

3 files changed

+138
-29
lines changed

3 files changed

+138
-29
lines changed

Contentstack.Core/ContentstackClient.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,88 @@ private string _Url
6161
private string currentContenttypeUid = null;
6262
private string currentEntryUid = null;
6363
public List<IContentstackPlugin> Plugins { get; set; } = new List<IContentstackPlugin>();
64+
65+
private static LivePreviewConfig CloneLivePreviewConfig(LivePreviewConfig source)
66+
{
67+
if (source == null) return null;
68+
69+
return new LivePreviewConfig
70+
{
71+
Enable = source.Enable,
72+
Host = source.Host,
73+
ManagementToken = source.ManagementToken,
74+
PreviewToken = source.PreviewToken,
75+
ReleaseId = source.ReleaseId,
76+
PreviewTimestamp = source.PreviewTimestamp,
77+
78+
// internal state (same assembly)
79+
LivePreview = source.LivePreview,
80+
ContentTypeUID = source.ContentTypeUID,
81+
EntryUID = source.EntryUID,
82+
PreviewResponse = source.PreviewResponse
83+
};
84+
}
85+
86+
private static ContentstackOptions CloneOptions(ContentstackOptions source)
87+
{
88+
if (source == null) return null;
89+
90+
return new ContentstackOptions
91+
{
92+
ApiKey = source.ApiKey,
93+
AccessToken = source.AccessToken,
94+
DeliveryToken = source.DeliveryToken,
95+
Environment = source.Environment,
96+
Host = source.Host,
97+
Proxy = source.Proxy,
98+
Region = source.Region,
99+
Version = source.Version,
100+
Branch = source.Branch,
101+
Timeout = source.Timeout,
102+
EarlyAccessHeader = source.EarlyAccessHeader,
103+
LivePreview = CloneLivePreviewConfig(source.LivePreview)
104+
};
105+
}
106+
107+
/// <summary>
108+
/// Clears any in-memory Live Preview context (hash, release, timestamp, content type, entry).
109+
/// Useful when switching back to the Delivery API after using Live Preview / Timeline preview.
110+
/// </summary>
111+
public void ResetLivePreview()
112+
{
113+
if (this.LivePreviewConfig == null) return;
114+
115+
this.LivePreviewConfig.LivePreview = null;
116+
this.LivePreviewConfig.ReleaseId = null;
117+
this.LivePreviewConfig.PreviewTimestamp = null;
118+
this.LivePreviewConfig.ContentTypeUID = null;
119+
this.LivePreviewConfig.EntryUID = null;
120+
this.LivePreviewConfig.PreviewResponse = null;
121+
}
122+
123+
/// <summary>
124+
/// Creates a new client instance with the same configuration but isolated in-memory state.
125+
/// Use this to safely perform Timeline comparisons (left/right) without shared Live Preview context.
126+
/// </summary>
127+
public ContentstackClient Fork()
128+
{
129+
var forked = new ContentstackClient(CloneOptions(_options));
130+
131+
// Preserve any runtime header mutations (e.g., custom headers added via SetHeader).
132+
if (this._LocalHeaders != null)
133+
{
134+
foreach (var kvp in this._LocalHeaders)
135+
{
136+
forked.SetHeader(kvp.Key, kvp.Value?.ToString());
137+
}
138+
}
139+
140+
// Carry over current content type / entry hints (used when live preview query omits them)
141+
forked.currentContenttypeUid = this.currentContenttypeUid;
142+
forked.currentEntryUid = this.currentEntryUid;
143+
144+
return forked;
145+
}
64146
/// <summary>
65147
/// Initializes a instance of the <see cref="ContentstackClient"/> class.
66148
/// </summary>

Contentstack.Core/Models/Entry.cs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,39 +1401,54 @@ public async Task<T> Fetch<T>()
14011401

14021402
//Dictionary<string, object> urlQueries = new Dictionary<string, object>();
14031403

1404+
var livePreviewConfig = this.ContentTypeInstance?.StackInstance?.LivePreviewConfig;
14041405
if (headers != null && headers.Count() > 0)
14051406
{
14061407
foreach (var header in headers)
14071408
{
1408-
if (this.ContentTypeInstance.StackInstance.LivePreviewConfig.Enable == true
1409-
&& this.ContentTypeInstance.StackInstance.LivePreviewConfig.ContentTypeUID == this.ContentTypeInstance.ContentTypeId
1410-
&& header.Key == "access_token" && !string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.LivePreview))
1409+
if (this.ContentTypeInstance != null
1410+
&& livePreviewConfig != null
1411+
&& livePreviewConfig.Enable
1412+
&& livePreviewConfig.ContentTypeUID == this.ContentTypeInstance.ContentTypeId
1413+
&& header.Key == "access_token"
1414+
&& !string.IsNullOrEmpty(livePreviewConfig.LivePreview))
14111415
{
14121416
continue;
14131417
}
14141418
headerAll.Add(header.Key, (String)header.Value);
14151419
}
14161420
}
14171421
bool isLivePreview = false;
1418-
if (this.ContentTypeInstance.StackInstance.LivePreviewConfig.Enable == true && this.ContentTypeInstance.StackInstance.LivePreviewConfig.ContentTypeUID == this.ContentTypeInstance.ContentTypeId)
1422+
var hasLivePreviewContext =
1423+
this.ContentTypeInstance != null
1424+
&& livePreviewConfig != null
1425+
&& livePreviewConfig.Enable
1426+
&& livePreviewConfig.ContentTypeUID == this.ContentTypeInstance.ContentTypeId
1427+
&& (
1428+
!string.IsNullOrEmpty(livePreviewConfig.LivePreview)
1429+
|| !string.IsNullOrEmpty(livePreviewConfig.ReleaseId)
1430+
|| !string.IsNullOrEmpty(livePreviewConfig.PreviewTimestamp)
1431+
);
1432+
1433+
if (hasLivePreviewContext)
14191434
{
1420-
mainJson.Add("live_preview", string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.LivePreview)? "init" : this.ContentTypeInstance.StackInstance.LivePreviewConfig.LivePreview);
1435+
mainJson.Add("live_preview", string.IsNullOrEmpty(livePreviewConfig.LivePreview)? "init" : livePreviewConfig.LivePreview);
14211436

1422-
if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.ManagementToken)) {
1423-
headerAll["authorization"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.ManagementToken;
1424-
} else if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewToken)) {
1425-
headerAll["preview_token"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewToken;
1437+
if (!string.IsNullOrEmpty(livePreviewConfig.ManagementToken)) {
1438+
headerAll["authorization"] = livePreviewConfig.ManagementToken;
1439+
} else if (!string.IsNullOrEmpty(livePreviewConfig.PreviewToken)) {
1440+
headerAll["preview_token"] = livePreviewConfig.PreviewToken;
14261441
} else {
14271442
throw new LivePreviewException();
14281443
}
14291444

1430-
if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.ReleaseId))
1445+
if (!string.IsNullOrEmpty(livePreviewConfig.ReleaseId))
14311446
{
1432-
headerAll["release_id"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.ReleaseId;
1447+
headerAll["release_id"] = livePreviewConfig.ReleaseId;
14331448
}
1434-
if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewTimestamp))
1449+
if (!string.IsNullOrEmpty(livePreviewConfig.PreviewTimestamp))
14351450
{
1436-
headerAll["preview_timestamp"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewTimestamp;
1451+
headerAll["preview_timestamp"] = livePreviewConfig.PreviewTimestamp;
14371452
}
14381453

14391454
isLivePreview = true;

Contentstack.Core/Models/Query.cs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,26 +1874,37 @@ private async Task<JObject> Exec()
18741874
Dictionary<string, object> mainJson = new Dictionary<string, object>();
18751875

18761876
bool isLivePreview = false;
1877-
if (this.ContentTypeInstance!=null && this.ContentTypeInstance.StackInstance.LivePreviewConfig.Enable == true
1878-
&& this.ContentTypeInstance.StackInstance?.LivePreviewConfig.ContentTypeUID == this.ContentTypeInstance.ContentTypeId)
1879-
{
1880-
mainJson.Add("live_preview", string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.LivePreview) ? "init" : this.ContentTypeInstance.StackInstance.LivePreviewConfig.LivePreview);
1881-
1882-
if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.ManagementToken)) {
1883-
headerAll["authorization"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.ManagementToken;
1884-
} else if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewToken)) {
1885-
headerAll["preview_token"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewToken;
1877+
var livePreviewConfig = this.ContentTypeInstance?.StackInstance?.LivePreviewConfig;
1878+
var hasLivePreviewContext =
1879+
this.ContentTypeInstance != null
1880+
&& livePreviewConfig != null
1881+
&& livePreviewConfig.Enable
1882+
&& livePreviewConfig.ContentTypeUID == this.ContentTypeInstance.ContentTypeId
1883+
&& (
1884+
!string.IsNullOrEmpty(livePreviewConfig.LivePreview)
1885+
|| !string.IsNullOrEmpty(livePreviewConfig.ReleaseId)
1886+
|| !string.IsNullOrEmpty(livePreviewConfig.PreviewTimestamp)
1887+
);
1888+
1889+
if (hasLivePreviewContext)
1890+
{
1891+
mainJson.Add("live_preview", string.IsNullOrEmpty(livePreviewConfig.LivePreview) ? "init" : livePreviewConfig.LivePreview);
1892+
1893+
if (!string.IsNullOrEmpty(livePreviewConfig.ManagementToken)) {
1894+
headerAll["authorization"] = livePreviewConfig.ManagementToken;
1895+
} else if (!string.IsNullOrEmpty(livePreviewConfig.PreviewToken)) {
1896+
headerAll["preview_token"] = livePreviewConfig.PreviewToken;
18861897
} else {
18871898
throw new LivePreviewException();
18881899
}
18891900

1890-
if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.ReleaseId))
1901+
if (!string.IsNullOrEmpty(livePreviewConfig.ReleaseId))
18911902
{
1892-
headerAll["release_id"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.ReleaseId;
1903+
headerAll["release_id"] = livePreviewConfig.ReleaseId;
18931904
}
1894-
if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewTimestamp))
1905+
if (!string.IsNullOrEmpty(livePreviewConfig.PreviewTimestamp))
18951906
{
1896-
headerAll["preview_timestamp"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewTimestamp;
1907+
headerAll["preview_timestamp"] = livePreviewConfig.PreviewTimestamp;
18971908
}
18981909

18991910
isLivePreview = true;
@@ -1903,10 +1914,11 @@ private async Task<JObject> Exec()
19031914
{
19041915
foreach (var header in headers)
19051916
{
1906-
if (this.ContentTypeInstance!=null && this.ContentTypeInstance?.StackInstance.LivePreviewConfig.Enable == true
1907-
&& this.ContentTypeInstance?.StackInstance.LivePreviewConfig.ContentTypeUID == this.ContentTypeInstance?.ContentTypeId
1917+
if (this.ContentTypeInstance!=null && livePreviewConfig != null
1918+
&& livePreviewConfig.Enable
1919+
&& livePreviewConfig.ContentTypeUID == this.ContentTypeInstance?.ContentTypeId
19081920
&& header.Key == "access_token"
1909-
&& !string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.LivePreview))
1921+
&& !string.IsNullOrEmpty(livePreviewConfig.LivePreview))
19101922
{
19111923
continue;
19121924
}

0 commit comments

Comments
 (0)