Skip to content

Commit 419ef56

Browse files
t3dotggclaude
andcommitted
fix(web): keep noreferrer on external links; open GitHub only while the click is active
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4b223fc commit 419ef56

4 files changed

Lines changed: 14 additions & 35 deletions

File tree

‎apps/web/src/components/GitHubStarPrompt.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ describe("recordPromptSent", () => {
164164
expect(add).not.toHaveBeenCalled();
165165
vi.unstubAllGlobals();
166166
});
167-
it("stars on click, opens GitHub when starring fails, and asks for a click when blocked", async () => {
167+
it("stars on click, opens GitHub when starring fails, and asks for a click once the click expired", async () => {
168168
setup({ sent: 5 });
169169
await (await startSession()).send();
170170
click(add.mock.lastCall?.[0].actionProps);
@@ -184,14 +184,15 @@ describe("recordPromptSent", () => {
184184

185185
setup({ sent: 5 });
186186
starred.starWorks = false;
187-
openExternal.mockRejectedValueOnce(new Error("Unable to open link."));
187+
vi.stubGlobal("navigator", { userActivation: { isActive: false } });
188188
await (await startSession()).send();
189189
click(add.mock.lastCall?.[0].actionProps);
190190
await vi.advanceTimersByTimeAsync(0);
191191
const fallback = update.mock.lastCall?.[1];
192192
expect(fallback).toMatchObject({ actionProps: { children: "Open GitHub" } });
193+
expect(openExternal).not.toHaveBeenCalled();
193194
click(typeof fallback === "object" ? fallback.actionProps : undefined);
194-
expect(openExternal).toHaveBeenCalledTimes(2);
195+
expect(openExternal).toHaveBeenCalledOnce();
195196
vi.unstubAllGlobals();
196197
});
197198
});

‎apps/web/src/components/GitHubStarPrompt.tsx‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,11 @@ function dismiss() {
2929
// Every close path, including both buttons, runs `onClose`, which saves the dismissal.
3030
function showStarPrompt(environmentId: EnvironmentId) {
3131
const close = () => toastManager.close(id);
32-
const openInBrowser = async () => {
33-
try {
34-
await readLocalApi()?.shell.openExternal(`https://github.com/${REPOSITORY}`);
35-
close();
36-
return true;
37-
} catch {
38-
return false;
39-
}
32+
const openInBrowser = () => {
33+
close();
34+
void readLocalApi()
35+
?.shell.openExternal(`https://github.com/${REPOSITORY}`)
36+
.catch(() => undefined);
4037
};
4138
const starInPlace = async () => {
4239
toastManager.update(id, { actionProps: { children: "Starring…", disabled: true } });
@@ -45,11 +42,10 @@ function showStarPrompt(environmentId: EnvironmentId) {
4542
toastManager.add({ type: "success", title: "Thanks for the star!", timeout: 4_000 });
4643
return;
4744
}
48-
if (await openInBrowser()) return;
49-
// The browser blocked a tab opened after the await; this click opens it directly.
50-
toastManager.update(id, {
51-
actionProps: { children: "Open GitHub", onClick: () => void openInBrowser() },
52-
});
45+
// Browsers block new tabs once the click's activation expires; desktop opens through its bridge.
46+
if (window.desktopBridge || navigator.userActivation?.isActive !== false)
47+
return openInBrowser();
48+
toastManager.update(id, { actionProps: { children: "Open GitHub", onClick: openInBrowser } });
5349
};
5450
const id = toastManager.add({
5551
title: "Enjoying T3 Code?",

‎apps/web/src/localApi.test.ts‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,4 @@ describe("LocalApi", () => {
7171

7272
await expect(createLocalApi().dialogs.confirm("Delete this thread?")).resolves.toBe(false);
7373
});
74-
75-
it("opens links without an opener, and fails when the browser blocks the tab", async () => {
76-
const tab: { opener: unknown } = { opener: testWindow() };
77-
const open = vi.fn((_url: string, _target: string): typeof tab | null => tab);
78-
Object.defineProperty(testWindow(), "open", { configurable: true, value: open });
79-
const { createLocalApi } = await import("./localApi");
80-
const { shell } = createLocalApi();
81-
82-
await shell.openExternal("https://github.com/pingdotgg/t3code");
83-
expect(open).toHaveBeenCalledWith("https://github.com/pingdotgg/t3code", "_blank");
84-
expect(tab.opener).toBeNull();
85-
86-
open.mockReturnValueOnce(null);
87-
await expect(shell.openExternal("https://github.com")).rejects.toThrow("Unable to open link.");
88-
});
8974
});

‎apps/web/src/localApi.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ export function createLocalApi(): LocalApi {
2727
return;
2828
}
2929

30-
// `noopener` would make window.open return null even when it opens, hiding a blocked popup.
31-
const opened = window.open(url, "_blank");
32-
if (!opened) throw new Error("Unable to open link.");
33-
opened.opener = null;
30+
window.open(url, "_blank", "noopener,noreferrer");
3431
},
3532
openSystemSettings: async (pane) => {
3633
if (!window.desktopBridge) {

0 commit comments

Comments
 (0)