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

Trajectory

lapix, Mylta, RANASOL

1

fish in the pool

yeule

2

E7len's Knife

icesawder

3

Music Baby (leroy Remix)

Jane Remover

4

Dyad

Jamie Paige, unit.0

5

Machine Love

Jamie Paige

6

Flashout Freestyle

slayr

7

My Darling, My Companion

Jamie Paige

8

NOGWAPNOLIFE

bleood

9

BIRDBRAIN

Jamie Paige, OK Glass

10

In the dark

venturing

11

Set In Stone

slayr

12

Love Blur

slayr

13

Music Baby

Jane Remover

14

media offline

ariiol

15

The End

Danny Brown, ta Ukrainka, Zheani, Cynthoni

16

Music and Me

fakemink

17

MIRINAE

TAK, Zekk

18

Rain Capsule

Namitape

19

Overfeel

Namitape

20

All At Once

Ninajirachi

21

Infohazard

Ninajirachi

22

Delete

Ninajirachi

23

weathergirl

FLAVOR FOLEY

24

Lawsyn

ayowitty

25

THUNDRRR

Quadeca

26

THAT'S WHY

Quadeca

27

WAGING WAR

Quadeca, Olēka

28

NO QUESTIONS ASKED

Quadeca

29

Always Have Always Will

Jane Remover

30

SLAM PUNK

Che

31

Akibare

Mylta

32

My life is mine alone! (feat. nenne)

Mylta, nenne

33

disarmed

kumosai, Sayako

34

spectrum

icesawder

35

NOWNEVER

Jaron Steele

36

Pretty Peach

Lucy Bedroque

37

How to Pretend

Lucy Bedroque

38

Knot Me

Lucy Bedroque

39

Sweet Pitcher

Lucy Bedroque

40

Thousand yard stare

kmoe

41

angels in camo

Jane Remover

42

Professional Vengeance

Jane Remover

43

Dreamflasher

Jane Remover

44

Experimental Skin

Jane Remover

45

Psychoboost

Jane Remover, Danny Brown

46

Dancing with your eyes closed

Jane Remover

47

Spider

venturing

48

Believe

venturing

49

alucarda lives!

smiling broadly

50

fake blood recipe

smiling broadly

51

flower bed

defsharp

52

sheaskedwhatmylifeislike

ericdoa

53

Caught up (in circles)

Syzy

54

Pastel Express

Cynax

55

St. Chroma (feat. Daniel Caesar)

Tyler, The Creator, Daniel Caesar

56

let's go home

Jane Remover

57

can you tell?

Jane Remover

58

champ

Jane Remover

59

pretender

Jane Remover

60

kodak moment

Jane Remover

61

movies for guys

Jane Remover

62

misplace

Jane Remover

63

Catch me if you can

tn-shi

64

natural

zeroth

65

mint

Snail's House

66

Emerald Lakeside - Action

はがね, Kitsui Akira

67

back off!!!

Jane Remover, kmoe, juno

68

one more life

Murphmusic, park.

69

Clinozoisite

Ludicin

70

Duhhhhhhhhhhhhhhhhh

underscores

71

Rabbit In The Black Room

Rabbit House

72

String Theocracy - Key Ingredient ver.

Mili

73

My guy (Corporate shuffle)

underscores

74

Wizards

xaev, mopearound

75

Everything Goes On

Porter Robinson, League of Legends

76

phobie d’impulsion

glaive

77

PUSH UR T3MPRR

femtanyl

78

Amethyst Aurora

BilliumMoto

79

the now now and never

what is your name?

80

Reverse Nightmare Tower

bye2

81

pop music

Limonène

82

EXACTLY WHY I'M STILL HERE - TURQUOISEDEATH Remix

bunnyprodge, TURQUOISEDEATH

83

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

glaive

84

Tojita Sekai

Camellia

85

commatose

glass beach

86

200

glass beach

87

until the dawn breaks

Deathbrain

88

dumb party

Internet Girl

89

skinz

8485

90

Algas Danses

seatrus

91

うみのゆき

seatrus

92

Palmy Flakes

seatrus

93

Cloud99 (As Above Mix)

Machine Girl

94

enchanted love

linear ring

95

Midnight Theater

Kano, Nagi Nemoto

96

Mola mola

Marmalade butcher

97

Another Ride

ippo.tsk, Synthesizer V ANRI

98

neon glow

glass beach

99

I Still Miss You

bo en, Tomggg

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