function youtubeVideoId
youtubeVideoId(
host: string,
pathname: string,
): string | null

Returns the id of the video a YouTube URL points at, in any of the shapes people paste (youtu.be/ID, /watch?v=ID, /embed/ID, /shorts/ID, /live/ID), and null for any other URL. host is lower case, without "www.". The canonical key drops the timestamp, since the same video linked at 2:30 is still a repost; the YouTube provider reads it separately.

Parameters

host: string
pathname: string

Return Type

string | null

Usage

import { youtubeVideoId } from "api/lib/canonical-url.ts";