Skip to content

Commit c46a3d8

Browse files
Keep the connection state when retrying without an error, and restart polling when the reported state changes
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7412b2e commit c46a3d8

4 files changed

Lines changed: 61 additions & 4 deletions

File tree

‎.changeset/bounded-config-polling.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'@godaddy/gd-commerce-storefront': minor
33
---
44

5-
Configuration polling while the server reports `unbound` or `connecting` now ends. After the page loads, the provider polls `/api/commerce/config` every five seconds for five minutes, then every minute, and stops at thirty minutes, whether or not requests fail. Before, it polled every five seconds for as long as the page stayed visible. Reloading the page starts polling again, and focus and `retryConnection()` still refresh configuration. A page still `connecting` when polling stops shows the connection error with "Retry connection" instead of waiting indefinitely. Retrying shows connecting again and starts a new polling period; a failed retry shows connecting for at least a second before the error returns, so the click always has visible feedback.
5+
Configuration polling while the server reports `unbound` or `connecting` now ends. After the page loads, the provider polls `/api/commerce/config` every five seconds for five minutes, then every minute, and stops at thirty minutes, whether or not requests fail. Before, it polled every five seconds for as long as the page stayed visible. Reloading the page or a change in the reported state (for example, a focus refresh finding `unbound` became `connecting`) starts polling again, and focus and `retryConnection()` still refresh configuration. A page still `connecting` when polling stops shows the connection error with "Retry connection" instead of waiting indefinitely. Retrying shows connecting again and starts a new polling period; a failed retry shows connecting for at least a second before the error returns, so the click always has visible feedback. Retrying without an error refreshes without changing the connection state, so carts are kept.

‎packages/commerce-storefront/README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,9 @@ Sample grids and drawers have the attribute `data-commerce-source="sample"`.
527527

528528
The provider reads configuration on mount and focus. While the page is visible and the state is
529529
`unbound` or `connecting`, it also polls every five seconds for five minutes after the page loads,
530-
then every minute, and stops thirty minutes after load. Reloading or retrying starts it again.
531-
A page still `connecting` when polling stops shows an error with "Retry connection". The first
530+
then every minute, and stops thirty minutes after load. Reloading, retrying or a change in the
531+
reported state starts it again. A page still `connecting` when polling stops shows an error with
532+
"Retry connection". The first
532533
failed refresh hides samples and clears the sample cart; a later `unbound` response starts an
533534
empty sample cart. While connecting, refresh failures keep the connecting
534535
state for up to 60 seconds before showing the error. Once polling slows to every minute, the next

‎packages/commerce-storefront/src/commerce-provider.tsx‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ export function CommerceProvider({
235235
onEvent,
236236
}: CommerceProviderProps): ReactElement {
237237
const resolvedLocale: string = useMemo(() => resolveLocale(locale), [locale]);
238-
// Polling is timed from load, and a retry starts it over.
238+
// Polling is timed from load, and a retry or a change in the reported state starts it over.
239239
const [pollingSince, setPollingSince] = useState(Date.now);
240240
const [pollingStopped, setPollingStopped] = useState<boolean>(false);
241241
const [retrying, setRetrying] = useState<boolean>(false);
@@ -261,6 +261,13 @@ export function CommerceProvider({
261261
},
262262
refetchIntervalInBackground: false,
263263
});
264+
// A store connected after polling stopped (picked up on focus) gets a fresh window.
265+
const [reported, setReported] = useState(config.data?.state);
266+
if (reported !== config.data?.state) {
267+
setReported(config.data?.state);
268+
setPollingSince(Date.now());
269+
setPollingStopped(false);
270+
}
264271
// Hosts may restart while connecting a store. Keep the connecting state through brief
265272
// refresh failures; it never shows samples, and polling continues until the grace ends.
266273
const holdConnecting: boolean =
@@ -295,6 +302,11 @@ export function CommerceProvider({
295302
retryConnection={() => {
296303
const started = Date.now();
297304
setPollingSince(started);
305+
// Only an error shows retry feedback; changing a ready or unbound connection would reset the cart.
306+
if (connection !== 'error') {
307+
void config.refetch();
308+
return;
309+
}
298310
setRetrying(true);
299311
void config.refetch().then(async (result) => {
300312
if (result.isError)

‎packages/commerce-storefront/src/storefront.test.tsx‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,50 @@ describe('bounded polling', () => {
835835
}
836836
});
837837

838+
it('keeps a connected cart when retrying without an error', async () => {
839+
localStorage.setItem(storageKey, 'cart-1');
840+
const api = mockApi((path) =>
841+
path.endsWith('/config') ? response(configuration) : response({ cart: cart() }),
842+
);
843+
const view = mount();
844+
await connected(view);
845+
const loaded = view.context().cart;
846+
const reads = api.mock.calls.filter(([path]) => String(path).includes('/cart')).length;
847+
act(() => view.context().retryConnection());
848+
expect(view.context().connection).toBe('ready');
849+
await waitFor(() => expect(configReads(api)).toBe(2));
850+
expect(view.context().cart).toBe(loaded);
851+
expect(api.mock.calls.filter(([path]) => String(path).includes('/cart')).length).toBe(reads);
852+
});
853+
854+
it('gives a store connected after polling stopped a fresh window', async () => {
855+
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'Date'] });
856+
try {
857+
let config: unknown = { state: 'unbound' };
858+
const api = mockApi((path) =>
859+
path.endsWith('/config') ? response(config) : response({ skuGroups: { edges: [] } }),
860+
);
861+
const view = mount(<Catalog sampleProducts />);
862+
await vi.waitFor(() => expect(view.context().connection).toBe('unbound'));
863+
await advance(minutes(60));
864+
// A focus refresh after polling stopped finds the store connecting.
865+
config = { state: 'connecting' };
866+
await act(async () => {
867+
await view.client.refetchQueries({ queryKey: ['commerce', 'configuration'] });
868+
});
869+
await vi.waitFor(() => expect(view.context().connection).toBe('connecting'));
870+
expect(screen.queryByText('The store is taking too long to connect.')).toBeNull();
871+
const read = configReads(api);
872+
await advance(5_000);
873+
expect(configReads(api)).toBe(read + 1);
874+
config = { state: 'ready', ...configuration };
875+
await advance(5_000);
876+
await vi.waitFor(() => expect(view.context().connection).toBe('ready'));
877+
} finally {
878+
vi.useRealTimers();
879+
}
880+
});
881+
838882
it('polls again after a reload once polling has stopped', async () => {
839883
vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval', 'Date'] });
840884
try {

0 commit comments

Comments
 (0)