Skip to content

Commit f60bd22

Browse files
authored
fix(url-preview): guard against undefined query string in YouTube URL parser (#584)
### Description Fixes a crash when any non-video YouTube URL is previewed — channels (`youtube.com/@handle`), channel IDs (`youtube.com/channel/...`), or any URL path without a `?` query string. The existing code (even after #578) had: ```ts params = path.split('?')[1].split('&'); ``` When there is no `?` in the URL, `split('?')[1]` returns `undefined`, and calling `.split('&')` on it throws: > `TypeError: Cannot read properties of undefined (reading 'split')` Fixed with optional chaining + nullish fallback: ```ts params = path.split('?')[1]?.split('&') ?? []; ``` This causes `videoId` to remain `undefined` for non-video URLs, which is handled correctly by the existing `if (!videoId) return null` guard — the preview simply doesn't render rather than crashing the entire app. Fixes # #### Type of change - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update ### Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings ### AI disclosure: - [x] Partially AI assisted (clarify which code was AI assisted and briefly explain what it does). AI identified the root cause after reviewing the PR #578 diff — the shorts fix added the `/shorts/` branch but left the original `youtube.com` branch with the same unguarded `.split()` call. The one-line fix (`?.split('&') ?? []`) was AI-suggested; I reviewed and confirmed it is correct.
2 parents 4be43b5 + 4a110ea commit f60bd22

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
default: patch
3+
---
4+
5+
Fix crash when previewing non-video YouTube URLs (channels, @handles, etc.) that lack query parameters.

src/app/components/url-preview/ClientPreview.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,19 @@ function parseYoutubeLink(url: string): YoutubeLink | null {
168168
[videoId] = split;
169169
params = split[1]?.split('&');
170170
} else if (url.includes('/shorts/')) {
171-
const split = path.split('/shorts/');
171+
const split = path.split('?');
172172
[videoId] = split;
173-
params = split[1]?.split('shorts');
173+
params = split[1]?.split('&') ?? [];
174174
} else if (url.includes('youtube.com')) {
175-
params = path.split('?')[1].split('&');
176-
videoId = params.find((s) => s.startsWith('v='), params)?.split('v=')[1];
175+
params = path.split('?')[1]?.split('&') ?? [];
176+
videoId = params.find((s) => s.startsWith('v='))?.split('v=')[1];
177177
} else return null;
178178

179179
if (!videoId) return null;
180180

181181
// playlist is not used for the embed, it can be appended as is
182-
const playlist = params ? params.find((s) => s.startsWith('list='), params) : undefined;
183-
const timestamp = params
184-
? params.find((s) => s.startsWith('t='), params)?.split('t=')[1]
185-
: undefined;
182+
const playlist = params ? params.find((s) => s.startsWith('list=')) : undefined;
183+
const timestamp = params ? params.find((s) => s.startsWith('t='))?.split('t=')[1] : undefined;
186184

187185
return {
188186
videoId,

0 commit comments

Comments
 (0)