Commit f60bd22
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 files changed
Lines changed: 11 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
171 | | - | |
| 171 | + | |
172 | 172 | | |
173 | | - | |
| 173 | + | |
174 | 174 | | |
175 | | - | |
176 | | - | |
| 175 | + | |
| 176 | + | |
177 | 177 | | |
178 | 178 | | |
179 | 179 | | |
180 | 180 | | |
181 | 181 | | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
| 182 | + | |
| 183 | + | |
186 | 184 | | |
187 | 185 | | |
188 | 186 | | |
| |||
0 commit comments