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.
This commit is contained in:
7w1 authored and GitHub committed 2026-03-30 03:31:15 +00:00
commit f60bd221f9
2 files changed
+11 -8

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
default: patch
---
Fix crash when previewing non-video YouTube URLs (channels, @handles, etc.) that lack query parameters.
@@ -168,21 +168,19 @@ function parseYoutubeLink(url: string): YoutubeLink | null {
[videoId] = split;
params = split[1]?.split('&');
} else if (url.includes('/shorts/')) {
const split = path.split('/shorts/');
const split = path.split('?');
[videoId] = split;
params = split[1]?.split('shorts');
params = split[1]?.split('&') ?? [];
} else if (url.includes('youtube.com')) {
params = path.split('?')[1].split('&');
videoId = params.find((s) => s.startsWith('v='), params)?.split('v=')[1];
params = path.split('?')[1]?.split('&') ?? [];
videoId = params.find((s) => s.startsWith('v='))?.split('v=')[1];
} else return null;
if (!videoId) return null;
// playlist is not used for the embed, it can be appended as is
const playlist = params ? params.find((s) => s.startsWith('list='), params) : undefined;
const timestamp = params
? params.find((s) => s.startsWith('t='), params)?.split('t=')[1]
: undefined;
const playlist = params ? params.find((s) => s.startsWith('list=')) : undefined;
const timestamp = params ? params.find((s) => s.startsWith('t='))?.split('t=')[1] : undefined;
return {
videoId,