Skip to content

Commit 57c152e

Browse files
authored
fix(web): prevent mock data from overwriting real widget data in production (#464)
The widget redesign in #429 removed the isMockEnvironment guard from setTimeout calls that update toolOutput with mock data. In OpenAI's production environment, window.openai always exists, so the mock data setTimeout would fire after 2s and overwrite real search results / run data with hardcoded mock actors. Also adds https://apify.com to the widget CSP resource_domains.
1 parent 9e8e0c6 commit 57c152e

3 files changed

Lines changed: 88 additions & 79 deletions

File tree

src/resources/widgets.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const OPENAI_WIDGET_CSP = {
1414
'https://images.apifyusercontent.com',
1515
'https://apify-image-uploads-prod.s3.us-east-1.amazonaws.com',
1616
'https://apify-image-uploads-prod.s3.amazonaws.com',
17+
'https://apify.com',
1718
],
1819
} as const;
1920

src/web/src/widgets/actor-run-widget.tsx

Lines changed: 69 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ const mockRunData = {
1616
// Simulate 5-second loading delay to test skeleton
1717
const LOADING_DELAY_MS = 2000;
1818

19-
// Set up mock window.openai for local development
19+
// Set up mock window.openai for local development (no-ops when window.openai already exists)
20+
const isMockEnvironment = typeof window !== "undefined" && !window.openai;
21+
2022
setupMockOpenAi({
2123
toolOutput: mockRunData, // Start with basic data (no dataset) to show header immediately
2224
initialWidgetState: {
@@ -191,69 +193,71 @@ setupMockOpenAi({
191193
},
192194
});
193195

194-
// Simulate loading delay - update toolOutput after 5 seconds to add dataset
195-
setTimeout(() => {
196-
if (window.openai) {
197-
// Update the toolOutput with dataset to show results
198-
window.openai.toolOutput = {
199-
...mockRunData,
200-
status: "SUCCEEDED",
201-
finishedAt: new Date().toISOString(),
202-
dataset: {
203-
datasetId: "test_dataset_456",
204-
itemCount: 15,
205-
previewItems: [
206-
{
207-
title: "Example Page 1",
208-
url: "https://example.com/page-1",
209-
description: "This is a long description that should test text overflow and ellipsis in table cells",
210-
category: "Technology",
211-
price: "$29.99",
212-
rating: "4.5/5",
213-
date: "2026-02-10"
214-
},
215-
{
216-
title: "Example Page 2",
217-
url: "https://example.com/page-2",
218-
description: "Another lengthy description to ensure we can test horizontal scrolling properly",
219-
category: "Business",
220-
price: "$49.99",
221-
rating: "4.8/5",
222-
date: "2026-02-09"
223-
},
224-
{
225-
title: "Example Page 3",
226-
url: "https://example.com/page-3",
227-
description: "Third item with even more text content for testing purposes",
228-
category: "Science",
229-
price: "$39.99",
230-
rating: "4.2/5",
231-
date: "2026-02-08"
232-
},
233-
{
234-
title: "Example Page 4",
235-
url: "https://example.com/page-4",
236-
description: "Fourth item in the dataset to test vertical scrolling",
237-
category: "Health",
238-
price: "$19.99",
239-
rating: "4.7/5",
240-
date: "2026-02-07"
241-
},
242-
{
243-
title: "Example Page 5",
244-
url: "https://example.com/page-5",
245-
description: "Fifth item with more content to fill the table",
246-
category: "Education",
247-
price: "$59.99",
248-
rating: "4.9/5",
249-
date: "2026-02-06"
250-
},
251-
],
252-
},
253-
} as any;
254-
// Trigger a re-render by dispatching an event
255-
window.dispatchEvent(new Event('openai:set_globals'));
256-
}
257-
}, LOADING_DELAY_MS);
196+
// Simulate loading delay - update toolOutput after delay to add dataset (only in local dev)
197+
if (isMockEnvironment) {
198+
setTimeout(() => {
199+
if (window.openai) {
200+
// Update the toolOutput with dataset to show results
201+
window.openai.toolOutput = {
202+
...mockRunData,
203+
status: "SUCCEEDED",
204+
finishedAt: new Date().toISOString(),
205+
dataset: {
206+
datasetId: "test_dataset_456",
207+
itemCount: 15,
208+
previewItems: [
209+
{
210+
title: "Example Page 1",
211+
url: "https://example.com/page-1",
212+
description: "This is a long description that should test text overflow and ellipsis in table cells",
213+
category: "Technology",
214+
price: "$29.99",
215+
rating: "4.5/5",
216+
date: "2026-02-10"
217+
},
218+
{
219+
title: "Example Page 2",
220+
url: "https://example.com/page-2",
221+
description: "Another lengthy description to ensure we can test horizontal scrolling properly",
222+
category: "Business",
223+
price: "$49.99",
224+
rating: "4.8/5",
225+
date: "2026-02-09"
226+
},
227+
{
228+
title: "Example Page 3",
229+
url: "https://example.com/page-3",
230+
description: "Third item with even more text content for testing purposes",
231+
category: "Science",
232+
price: "$39.99",
233+
rating: "4.2/5",
234+
date: "2026-02-08"
235+
},
236+
{
237+
title: "Example Page 4",
238+
url: "https://example.com/page-4",
239+
description: "Fourth item in the dataset to test vertical scrolling",
240+
category: "Health",
241+
price: "$19.99",
242+
rating: "4.7/5",
243+
date: "2026-02-07"
244+
},
245+
{
246+
title: "Example Page 5",
247+
url: "https://example.com/page-5",
248+
description: "Fifth item with more content to fill the table",
249+
category: "Education",
250+
price: "$59.99",
251+
rating: "4.9/5",
252+
date: "2026-02-06"
253+
},
254+
],
255+
},
256+
} as any;
257+
// Trigger a re-render by dispatching an event
258+
window.dispatchEvent(new Event('openai:set_globals'));
259+
}
260+
}, LOADING_DELAY_MS);
261+
}
258262

259263
renderWidget(ActorRun);

src/web/src/widgets/search-actors-widget.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ const mockActors = [
8989
},
9090
];
9191

92-
// Set up mock window.openai for local development
92+
// Set up mock window.openai for local development (no-ops when window.openai already exists)
93+
const isMockEnvironment = typeof window !== "undefined" && !window.openai;
94+
9395
setupMockOpenAi({
9496
toolOutput: {
9597
actors: [], // Start with empty to show loading state
@@ -101,18 +103,20 @@ setupMockOpenAi({
101103
},
102104
});
103105

104-
// Simulate async data loading
105-
setTimeout(() => {
106-
updateMockOpenAiState({
107-
toolOutput: {
108-
actors: mockActors,
109-
query: "web scraping",
110-
},
111-
widgetState: {
112-
loadingDetails: null,
113-
isLoading: false,
114-
},
115-
});
116-
}, 2000);
106+
// Simulate async data loading (only in local dev, never in production OpenAI environment)
107+
if (isMockEnvironment) {
108+
setTimeout(() => {
109+
updateMockOpenAiState({
110+
toolOutput: {
111+
actors: mockActors,
112+
query: "web scraping",
113+
},
114+
widgetState: {
115+
loadingDetails: null,
116+
isLoading: false,
117+
},
118+
});
119+
}, 2000);
120+
}
117121

118122
renderWidget(ActorSearch);

0 commit comments

Comments
 (0)