Back to blog
Blog

How to Build a Spotify Clone with Next.js and Cosmic

Cosmic Intelligence's avatar

Cosmic Intelligence

March 21, 2025

cover image

How to Build a Spotify Clone with Next.js and Cosmic

Streaming music platforms have revolutionized how we consume audio content. In this tutorial, we'll build a Spotify-like music streaming application using Next.js for the frontend and Cosmic as our headless CMS to manage music tracks, artists, and playlists.

Live Demo

GitHub Repository

Deploy with Vercel

Spotify Clone Screenshot 2

Spotify Clone Screenshot 1

What We'll Build

Our Spotify clone will feature:

  • Music library with artists, albums, and tracks
  • Playlist creation and management
  • A music player with playback controls
  • Responsive design for all devices

Prerequisites

  • Node.js 18.x or later (if using npm)
  • Bun runtime (if using bun, recommended)
  • A Cosmic account and bucket
  • Basic knowledge of React and TypeScript

Setting Up Our Project

First, let's create a new Next.js project with TypeScript:

Using bun (recommended):


Using npm:


Install the required dependencies:

Using bun:


Using npm:


Initialize Tailwind CSS:

Using bun:


Using npm:


Update your :


Setting Up Environment Variables

Create a file in your project root:


Project Structure

Let's organize our project with the following structure:


Setting Up TypeScript Types

Create to define our data types:


Configuring Cosmic Connection

Create :


Seeding the CMS

Create to set up our object types and sample content:


Run the seed script to set up your Cosmic bucket:


Building the Application

1. Create the Root Layout

Create :


2. Create the Player Context

Create :


3. Create Server Actions

Create :


4. Create Components

AlbumCard Component

Create :


MusicLibrary Component

Create :


TrackList Component

Create :


MusicPlayer Component

Create :


The MusicPlayer component includes several key features:

  1. Playback Controls: Users can play/pause tracks using the play/pause button in the UI.
  2. Keyboard Controls: The spacebar can be used to play/pause tracks from anywhere on the page, making it convenient for users to control playback without having to click the UI.
  3. Progress Bar: A visual progress bar shows the current position in the track and allows users to see how much of the track has played.
  4. Track Information: Displays the current track's title, artist, and album cover.
  5. Time Display: Shows the current time and total duration of the track.

The component uses the library for audio playback and manages its state using React hooks. It also includes keyboard event listeners for enhanced user experience.

5. Create Pages

Home Page

Create :


Album Detail Page

Create :


Playlist Page

Create :


Running the Application

Start the development server:


Conclusion

We've successfully built a Spotify clone using Next.js and Cosmic! Here's what we've accomplished:

  1. Created a modern, responsive music streaming application
  2. Implemented a music player with playback controls and keyboard shortcuts
  3. Built a library system for albums and playlists
  4. Set up a headless CMS with Cosmic to manage our music content
  5. Created a beautiful UI with Tailwind CSS

The application demonstrates the power of combining Next.js for the frontend with Cosmic as a headless CMS. We've shown how to:

  • Structure a Next.js application with TypeScript
  • Manage state with React Context
  • Handle server-side rendering and data fetching
  • Create reusable components
  • Implement audio playback functionality
  • Style our application with Tailwind CSS

Next Steps

Here are some ideas to enhance the application further:

  1. Authentication: Add user authentication to allow users to create and manage their own playlists
  2. Search Functionality: Implement a search feature to find tracks, albums, and artists
  3. User Profiles: Create user profiles with listening history and favorite tracks
  4. Social Features: Add the ability to follow artists and share playlists
  5. Offline Mode: Implement offline playback using service workers
  6. Mobile App: Create a mobile app version using React Native or Next.js mobile features

Resources

We hope this tutorial has helped you understand how to build a modern music streaming application. Feel free to customize the design and add more features to make it your own!