Skip to content

Commit f793784

Browse files
committed
refactor: serve connects to about:blank first, then navigates
Serve mode now connects CdpDriver with empty URL (picks any existing tab) then uses _navigateToUrl for smart tab reuse. This prevents CdpDriver.connect() from creating new tabs when the target domain has no existing tab, avoiding the duplicate-tab issue entirely.
1 parent eecd2d5 commit f793784

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

lib/src/cli/serve.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,24 @@ Future<void> runServe(List<String> args) async {
7272
print(' Hot Reload Watch: $watch');
7373
print('');
7474

75-
// Step 1: Connect via CDP
75+
// Step 1: Connect via CDP (always connect to about:blank first to avoid
76+
// creating duplicate tabs when target URL redirects to a different subdomain)
7677
print('📡 Connecting to Chrome...');
7778
final cdp = CdpDriver(
78-
url: url,
79+
url: '', // Connect to any existing tab, don't create one for target URL
7980
port: cdpPort,
8081
launchChrome: launchChrome,
8182
headless: headless,
8283
);
8384
await cdp.connect();
8485
print('✅ Connected via CDP');
8586

87+
// Step 1b: Navigate to target URL via smart navigation (reuses tabs by domain)
88+
if (url.isNotEmpty && url != 'about:blank') {
89+
await _navigateToUrl(cdp, url, cdpPort);
90+
await Future.delayed(const Duration(seconds: 2));
91+
}
92+
8693
// Step 2: Initial tool discovery
8794
print('🔍 Scanning page for interactive elements...');
8895
var toolCache = await _discoverAndPrint(cdp);
@@ -237,7 +244,6 @@ Future<void> _navigateToUrl(CdpDriver cdp, String url, int cdpPort) async {
237244
: targetHost;
238245

239246
Map<String, dynamic>? matchingTab;
240-
bool isRootDomainMatch = false;
241247

242248
// 1. Exact origin match
243249
matchingTab = tabs.cast<Map<String, dynamic>?>().firstWhere(
@@ -259,7 +265,6 @@ Future<void> _navigateToUrl(CdpDriver cdp, String url, int cdpPort) async {
259265
},
260266
orElse: () => null,
261267
);
262-
if (matchingTab != null) isRootDomainMatch = true;
263268
}
264269

265270
if (matchingTab != null) {

0 commit comments

Comments
 (0)