grafana/k6 · error

navigating to history entry %d: %w

Error message

navigating to history entry %d: %w

What it means

In Page's GoBackForward helper, after resolving the target history entry from page.getNavigationHistory, the CDP command Page.navigateToHistoryEntry is executed. This error wraps a failure of that command itself — the history entry exists but navigating to it failed at the protocol level.

Source

Thrown at internal/js/modules/k6/browser/common/page.go:1648

		return nil, err
	}

	targetIndex := currentIndex + int64(delta)

	// Check boundaries
	if targetIndex < 0 || targetIndex >= int64(len(entries)) {
		return nil, nil //nolint:nilnil
	}

	historyEntryID := entries[targetIndex].ID
	targetURL := entries[targetIndex].URL

	timeoutCtx, timeoutCancelFn := context.WithTimeout(p.ctx, opts.Timeout)
	defer timeoutCancelFn()

	navAction := page.NavigateToHistoryEntry(historyEntryID)
	if err := navAction.Do(cdp.WithExecutor(p.ctx, p.session)); err != nil {
		return nil, fmt.Errorf("navigating to history entry %d: %w", historyEntryID, err)
	}

	wrapTimeoutError := func(err error) error {
		if errors.Is(err, context.DeadlineExceeded) {
			err = &k6ext.UserFriendlyError{
				Err:     err,
				Timeout: opts.Timeout,
			}
		}
		p.logger.Debugf("Page:GoBackForward", "timeoutCtx done: %v", err)
		return fmt.Errorf("navigating %s to history entry %d: %w", direction, historyEntryID, err)
	}

	// Poll for URL change, don't rely on lifecycle events since bfcache
	// restorations don't re-fire them.
	ticker := time.NewTicker(50 * time.Millisecond)
	defer ticker.Stop()

View on GitHub (pinned to 93accf6570)

Solutions

  1. Space out back/forward calls (await each navigation fully)
  2. Check that the previous goBack/goForward returned a non-null response before issuing another
  3. Verify the page is not closing before navigating history
  4. Retry the navigation once if it failed transiently

Example fix

// before
await page.goBack();
await page.goBack(); // second call before first settles

// after
const res = await page.goBack();
if (res) await page.goBack();
Defensive patterns

Strategy: try-catch

Validate before calling

if (page.isClosed()) throw new Error('page closed before history navigation');

Try / catch

try {
  const res = await page.goBack();
  if (res === null) return; // no history entry
} catch (e) {
  if (!/navigating to history entry/.test(e.message)) throw e;
}

Prevention

When it happens

Trigger: page.goBack()/go.forward() when the renderer refuses the navigation (crashed target, session detached), or the history entry became invalid between listing and navigating (rapid consecutive navigations).

Common situations: Calling goBack() twice in quick succession; going back right as the page closes; renderer under memory pressure rejecting the command.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/bd266b0e516cb2c3. Report an issue: GitHub.