Skip to content

Commit c838f40

Browse files
authored
feat: Support youtube list embed URL (#4786)
## Description With this PR user can paste an embed url like this https://youtube.com/embed?listType=playlist&list=UUjk2nKmHzgH5Xy-C5qYRd5A and we will still be able to render it ## Steps for reproduction 1. click button 2. expect xyz ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 0000) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env` file
1 parent 5dcc8e5 commit c838f40

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

packages/sdk-components-react/src/youtube.tsx

+20-4
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ const getVideoId = (url?: string) => {
173173
}
174174
try {
175175
const urlObj = new URL(url);
176+
// It's already an embed URL, we don't need to extract video id.
177+
// It could be something like this https://youtube.com/embed?listType=playlist&list=UUjk2nKmHzgH5Xy-C5qYRd5A
178+
if (urlObj.pathname === "/embed") {
179+
return;
180+
}
176181
if (urlObj.hostname === "youtu.be") {
177182
return urlObj.pathname.slice(1);
178183
}
@@ -185,12 +190,23 @@ const getVideoId = (url?: string) => {
185190

186191
const getVideoUrl = (options: YouTubePlayerOptions, videoUrlOrigin: string) => {
187192
const videoId = getVideoId(options.url);
188-
if (!videoId) {
189-
return;
193+
const url = new URL(videoUrlOrigin);
194+
195+
if (videoId) {
196+
url.pathname = `/embed/${videoId}`;
197+
} else if (options.url) {
198+
// E.g. this won't have videoId https://youtube.com/embed?listType=playlist&list=UUjk2nKmHzgH5Xy-C5qYRd5A
199+
// It may also contain parameters since its an embed URL, so we want to keep it as-is and just use the origin we predefined
200+
// so that no cookies option still works
201+
try {
202+
const parsedUrl = new URL(options.url);
203+
url.pathname = parsedUrl.pathname;
204+
url.search = parsedUrl.search;
205+
} catch {
206+
// Ignore invalid URL
207+
}
190208
}
191209

192-
const url = new URL(`${videoUrlOrigin}/embed/${videoId}`);
193-
194210
const optionsKeys = Object.keys(options) as (keyof YouTubePlayerParameters)[];
195211

196212
const parameters: Record<string, string | undefined> = {};

0 commit comments

Comments
 (0)