cooler spotify embeds with 99.9061% less javascript

7/11/2024


hi this is my first blog post! i suck at writing anything down anything so hopefully talking about random things helps.

this all started when i wanted to display my favorite songs on my /about page, i already have a spotify playlist of them so i could just use the official embed. the problem is that the embed loads 1.96 mb of minified javascript.

spotify embed requests

…and also makes tons of strange requests back to spotify, is extremely loud (with no volume slider!), and preloads some songs. on my old site i just gave in and used the official embed, but after seeing that scraping spotify embeds was possible (thanks l-m!), i tried making my own.

all of that javascript is even more insane considering this was meant to be used on other sites, you’d think the multi-billion $ company would care about performance for a simple embed. but it’s no secret that a lot of the web is getting megabytes of javascript larger, from complex frameworks and overkill libraries just to render mostly static sites. tonsky already has a great article on this.

here they are!

extremely good songs

open in spotify
playlist thumbnail

E7len's Knife

icesawder

1

Music Baby (leroy Remix)

Jane Remover

2

Dyad

Jamie Paige, unit.0

3

Machine Love

Jamie Paige

4

Flashout Freestyle

slayr

5

My Darling, My Companion

Jamie Paige

6

NOGWAPNOLIFE

bleood

7

BIRDBRAIN

Jamie Paige, OK Glass

8

In the dark

venturing

9

Set In Stone

slayr

10

Love Blur

slayr

11

Music Baby

Jane Remover

12

media offline

ariiol

13

The End

Danny Brown, ta Ukrainka, Zheani, Cynthoni

14

Music and Me

fakemink

15

MIRINAE

TAK, Zekk

16

Rain Capsule

Namitape

17

Overfeel

Namitape

18

All At Once

Ninajirachi

19

Infohazard

Ninajirachi

20

Delete

Ninajirachi

21

weathergirl

FLAVOR FOLEY

22

Lawsyn

ayowitty

23

THUNDRRR

Quadeca

24

THAT'S WHY

Quadeca

25

WAGING WAR

Quadeca, Olēka

26

NO QUESTIONS ASKED

Quadeca

27

Always Have Always Will

Jane Remover

28

SLAM PUNK

Che

29

Akibare

Mylta

30

My life is mine alone! (feat. nenne)

Mylta, nenne

31

disarmed

kumosai, Sayako

32

spectrum

icesawder

33

NOWNEVER

Jaron Steele

34

Pretty Peach

Lucy Bedroque

35

How to Pretend

Lucy Bedroque

36

Knot Me

Lucy Bedroque

37

Sweet Pitcher

Lucy Bedroque

38

Thousand yard stare

kmoe

39

angels in camo

Jane Remover

40

Professional Vengeance

Jane Remover

41

Dreamflasher

Jane Remover

42

Experimental Skin

Jane Remover

43

Psychoboost

Jane Remover, Danny Brown

44

Dancing with your eyes closed

Jane Remover

45

Spider

venturing

46

Believe

venturing

47

alucarda lives!

smiling broadly

48

fake blood recipe

smiling broadly

49

flower bed

defsharp

50

sheaskedwhatmylifeislike

ericdoa

51

Caught up (in circles)

Syzy

52

Pastel Express

Cynax

53

St. Chroma (feat. Daniel Caesar)

Tyler, The Creator, Daniel Caesar

54

let's go home

Jane Remover

55

can you tell?

Jane Remover

56

champ

Jane Remover

57

pretender

Jane Remover

58

kodak moment

Jane Remover

59

movies for guys

Jane Remover

60

misplace

Jane Remover

61

Catch me if you can

tn-shi

62

natural

zeroth

63

mint

Snail's House

64

Emerald Lakeside - Action

はがね, Kitsui Akira

65

back off!!!

Jane Remover, kmoe, juno

66

one more life

Murphmusic, park.

67

Clinozoisite

Ludicin

68

Duhhhhhhhhhhhhhhhhh

underscores

69

Rabbit In The Black Room

Rabbit House

70

String Theocracy - Key Ingredient ver.

Mili

71

My guy (Corporate shuffle)

underscores

72

Wizards

xaev, mopearound

73

Everything Goes On

Porter Robinson, League of Legends

74

phobie d’impulsion

glaive

75

PUSH UR T3MPRR

femtanyl

76

Amethyst Aurora

BilliumMoto

77

the now now and never

what is your name?

78

Reverse Nightmare Tower

bye2

79

pop music

Limonène

80

EXACTLY WHY I'M STILL HERE - TURQUOISEDEATH Remix

bunnyprodge, TURQUOISEDEATH

81

even when the sun is dead, will you tell them how hard i tried

glaive

82

Tojita Sekai

Camellia

83

commatose

glass beach

84

200

glass beach

85

until the dawn breaks

Deathbrain

86

dumb party

Internet Girl

87

skinz

8485

88

Algas Danses

seatrus

89

うみのゆき

seatrus

90

Palmy Flakes

seatrus

91

Cloud99 (As Above Mix)

Machine Girl

92

enchanted love

linear ring

93

Midnight Theater

Kano, Nagi Nemoto

94

Mola mola

Marmalade butcher

95

Another Ride

ippo.tsk, Synthesizer V ANRI

96

neon glow

glass beach

97

I Still Miss You

bo en, Tomggg

98

(We Are) Friends

bo en, Winamp Boys

99

Our Time

bo en, PAS TASTA

100

there's more songs on spotify :)

the now now and never

what is your name?

open in spotify
playlist thumbnail

the track embed was made with 0 JS, and the playlist embed was made with 1.84kb (minified) of JS, that’s 99.9061% smaller than the official embed, and i think it fits my site much better :)

ill get the playlist volume slider done one of these days… (9/14/2024: done!)

how

first, you can just make a fetch request to the normal embed url (either for a song or playlist) pretending to be a browser

const res = await fetch(
	`https://open.spotify.com/embed/playlist/2m2lebj9Lg6Riwfyv7G9AD`,
	{
		headers: {
			"User-Agent":
				"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0",
			Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
			"Accept-Language": "en-US,en;q=0.5",
			"Upgrade-Insecure-Requests": "1",
			"Sec-Fetch-Dest": "iframe",
			"Sec-Fetch-Mode": "navigate",
			"Sec-Fetch-Site": "cross-site",
			Pragma: "no-cache",
			"Cache-Control": "no-cache",
		},
		method: "GET",
	},
);

const html = await res.text();

and in the html response you get back, at the end there’s a magical script tag with the id __NEXT_DATA__ that contains everything you need!

response

const json = html.substring(
	html.lastIndexOf(`type="application/json">`) +
		`type="application/json">`.length,
	html.lastIndexOf(`</script>`),
);

const data: PlaylistEmbedData = JSON.parse(json);

you can make whatever with this data now! i’d highly recommend caching it. you can cache song embed data much more aggressively than playlists cause those shouldn’t change.

it’s possible to make your embeds use 0 JS by using the native <audio /> element, which is what i do for singular song embeds. there’s a pretty big problem when using <audio /> for each song in a playlist embed though, it might take safari 8 seconds to load your site sometimes (not an exaggeration).

yes i did pinpoint the loading times to the hundred <audio /> elements, safari really doesn’t like that rendering that many…

note that you can barely style <audio /> elements and the element looks very different across each browser. because of all that i used a bit of JS with web components for my playlist embed, which gets server side rendered with astro.

source code for scraper, components, and typescript types


have a question/comment? want to say hi?

you can also just contact me on discord, @squisket