Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copy to `.env` and fill in. Never commit `.env`.

# --- TIDAL (required) -------------------------------------------------------
# From your app at https://developer.tidal.com/dashboard
TIDAL_CLIENT_ID=
TIDAL_CLIENT_SECRET=

# Your TIDAL user ID — the playlists owned by this user are shown.
# `npm run tidal:login` prints it for you.
TIDAL_USER_ID=

# Only needed if TIDAL rejects client-credentials access to your playlists.
# Run `npm run tidal:login` once to obtain it. Treat it like a password.
TIDAL_REFRESH_TOKEN=

# Redirect URI registered in the TIDAL dashboard, used by `npm run tidal:login`.
# TIDAL_REDIRECT_URI=http://localhost:3000/callback

# ISO 3166-1 country code sent to the TIDAL API.
# TIDAL_COUNTRY_CODE=GB

# --- Site ------------------------------------------------------------------
# Public URL of the site (used for social sharing tags). On Vercel, defaults to
# the project's production URL; set it if you use a custom domain.
# SITE_URL=https://example.com
# Link for the "View on TIDAL" button in the footer. Hidden if empty.
TIDAL_PROFILE_URL=

# --- Behaviour -------------------------------------------------------------
# How long the server trusts its copy of your playlists before re-checking TIDAL (ms).
CACHE_DURATION=600000
# On Vercel: how long the CDN serves cached playlist data before refreshing (seconds).
# CDN_CACHE_SECONDS=600
# PORT=3000
# LOG_LEVEL=info

# --- Error reporting (optional) ---------------------------------------------
# Set to send server and browser errors to Sentry (https://sentry.io).
# SENTRY_DSN=

# --- Local development ------------------------------------------------------
# Serve generated demo playlists instead of calling TIDAL (`npm run dev:mock`).
# TIDAL_MOCK=1
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CI

on:
pull_request:
push:
branches: [main]

permissions:
contents: read

concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node: [22, 24]
steps:
- uses: actions/checkout@v5
- uses: actions/setup-node@v5
with:
node-version: ${{ matrix.node }}
cache: npm
- run: npm ci
- run: npm run check
- run: npm test
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
node_modules/
.env
**/.DS_Store
.env.*
!.env.example
.cache/
coverage/
**/.DS_Store
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
22
138 changes: 110 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,128 @@
# Selections
# SoundSelections

Selections is a Spotify playlist app that serves my weekly curated playlists for every genre and mood, as well as deep dives on artist's back catalogues. With this app, you'll be able to discover new music and artists that you might not have found otherwise.
A fast, responsive browser for Beere's curated playlists on [TIDAL](https://tidal.com): weekly selections for every genre and mood, plus deep dives into artists' back catalogues.

## Features
- Browse every public playlist, artwork first, on any screen size.
- Search titles, artist/genre tags and descriptions as you type (accents ignored: "bjork" finds "Björk").
- Filter by artist or genre, taken from the playlist title — "Night drive (rock)" is tagged *rock*.
- Open a playlist to play it in TIDAL's embedded player, open it in TIDAL, or copy its link.
- Every view has its own URL (`?q=`, `?tag=`, `?sort=`, `?p=<playlist>`), so it can be shared, bookmarked, and the Back button closes an open playlist.

* Weekly curated playlists for every genre and mood
* Deep dives on artist's back catalogues
## Quick start

## Getting Started
Requires Node.js 22.12 or newer.

1. Clone the repository to your local machine.
```bash
npm install
npm run dev:mock # demo playlists, no TIDAL account needed
```

2. Create a new Spotify App at the Spotify Developer Dashboard.
Open <http://localhost:3000>.

3. Add http://localhost:3000/callback as a Redirect URI in your Spotify App settings.
## Connecting your TIDAL account

4. Copy the Client ID and Client Secret from your Spotify App settings and add them, along with a cache duration to a `.env` file at the root of the project:

```
CLIENT_ID=<your-client-id>
CLIENT_SECRET=<your-client-secret>
CACHE_DURATION=<your cache duration in MS>
```
1. In your app at [developer.tidal.com](https://developer.tidal.com/dashboard), copy the **Client ID** and **Client Secret**.
2. Copy `.env.example` to `.env` and fill in:

1. Install the required packages
```
TIDAL_CLIENT_ID=…
TIDAL_CLIENT_SECRET=…
TIDAL_USER_ID=… # whose playlists to show (see below)
TIDAL_PROFILE_URL=… # optional: link for the "View on TIDAL" button
```

```bash
npm install
```
3. `npm run dev`, and check the terminal: it logs `Fetched playlists from TIDAL` with a count when everything works.

### If TIDAL refuses: sign in once

TIDAL has said its playlist endpoints may require a signed-in user, even for public playlists. If the log shows *"TIDAL denied access … Run `npm run tidal:login`"*:

1. In the TIDAL dashboard, add `http://localhost:3000/callback` as a **Redirect URI** for your app, and enable the `user.read` and `playlists.read` scopes.
2. Stop the app, then run `npm run tidal:login -- --save` and sign in with the link it prints.
3. It saves `TIDAL_REFRESH_TOKEN` (and your `TIDAL_USER_ID`) to `.env`. Add the same values to your Vercel project's environment variables.

The refresh token grants read access to your account: keep it secret. Only **public** playlists are ever shown, whichever way the app signs in.

**Finding your user ID:** `npm run tidal:login` prints it.

## Deploying (Vercel)

1. Start the development server
1. In Vercel, **Add New → Project** and import this GitHub repository. Vercel detects an Express app (`app.js`). There is no build step, and the settings in `vercel.json` need no changes.
2. Under **Settings → Environment Variables**, add for Production (and Preview, if you want working preview deployments):
- `TIDAL_CLIENT_ID`, `TIDAL_CLIENT_SECRET`, `TIDAL_USER_ID`, `TIDAL_REFRESH_TOKEN`
- `TIDAL_PROFILE_URL` (optional) and `SENTRY_DSN` (optional)
- `SITE_URL` only if you use a custom domain; otherwise the project's production URL is used.
3. Deploy. Every pull request gets its own preview deployment.
4. Check `https://<your-site>/healthz`: it should report `"status":"ok"` and a playlist count.

How the app runs on Vercel:

- **One function, CDN-served files:** `app.js` is a single Vercel Function. Everything in `public/` is served directly by Vercel's CDN; `vercel.json` maps the versioned `/static/<hash>/…` URLs onto those files and marks them cacheable for a year.
- **Playlist data is cached on the CDN:** API responses carry a `Vercel-CDN-Cache-Control` header, so the CDN answers repeat requests and refreshes in the background. Visitors only wait for TIDAL (about 10 seconds for ~340 playlists) on the first request after a deploy, or when the CDN has no copy at all. Change `CDN_CACHE_SECONDS` (default 600) to tune this.
- **Disk:** each function instance keeps its saved copy in the temp directory, the only writable place on Vercel.
- **Background work:** refreshes and Sentry reports run through `waitUntil`, so they finish after the response is sent.
- **Preview toolbar:** on preview deployments, Vercel's toolbar may be blocked by the site's Content Security Policy and show up as CSP reports in the logs. This is harmless; the toolbar can be turned off in the project settings.

`npm start` still runs a normal long-lived server for any other host.

## How it stays fast

**On the server.** All playlists and their artwork come from TIDAL in a handful of paginated requests. The old app made one extra request for every playlist. The result is kept in memory for `CACHE_DURATION` (10 minutes by default) and saved to disk, so a restart serves instantly. After that, the next visit triggers a background refresh, and visitors never wait for it. On Vercel, the CDN also caches the playlist data (see *Deploying*). If TIDAL is down, the last good copy keeps being served while the server retries with back-off.

**In the browser.**
- **First visit:** one request for everything, roughly 10–20 KB compressed for 150 playlists. A tiny script in `<head>` starts it before the app's code has even downloaded, and the newest playlist's artwork is preloaded.
- **Return visits:** the grid renders instantly from the browser's saved copy (localStorage). The app then asks the server whether anything has changed, using an ETag. Usually the answer is an empty `304 Not Modified`.
- **When something has changed:** the server sends a manifest of every playlist's id and revision. The browser then downloads only the playlists that are new or edited, drops deleted ones, and says *"A new playlist has been added since your last visit."*

**Images and assets.**
- **Artwork:** each image is sized to fit its card (`srcset`/`sizes`), lazy-loaded as you scroll, and fades in over its own palette colour. Only the first row loads eagerly.
- **TIDAL player:** loads only when a playlist is opened, and is removed when it closes.
- **CSS, JS and fonts:** served from URLs that include a hash of their contents, so browsers cache them for a year and a deploy updates them all at once. No build step is needed. The Outfit font is self-hosted.

## Errors: handling and reporting

- **For visitors:** nothing fails silently or shows a broken page.
- Clear messages, with a *Try again* button.
- Offline detection, with an automatic retry on reconnect.
- Automatic retries while TIDAL is unavailable.
- The saved copy stays on screen when a refresh fails.
- A placeholder for any artwork that fails to load, and a fallback when the clipboard is blocked.
- **Server logs:** structured JSON, one line per request, each with a request ID. Visitors see that ID ("Reference: …") on error messages, so a report can be matched to its log line. Secrets are redacted.
- **Browser errors:** sent to `POST /api/reports`, which de-duplicates and rate-limits them before logging. Content-Security-Policy violations are reported there too.
- **Sentry (optional):** set `SENTRY_DSN` to receive server and browser errors with alerting. The SDK loads only when configured.
- **TIDAL failures:** carry TIDAL's own error code and request ID. Credential problems explain how to fix them.
- **Health:** `GET /healthz` reports `ok`, `degraded` (serving the last good copy) or `unavailable`, with the data's age.
- **Crashes:** an unexpected crash is reported before the process exits, and the host restarts it. Shutdown on deploy is graceful.

## Development

```bash
node app.js
npm run dev # with your TIDAL account; restarts on server changes
npm run dev:mock # with generated demo data
npm test # unit and integration tests (Node's built-in runner)
npm run check # syntax-check every JS file, including browser code
```

1. Visit http://localhost:3000 in your web browser to use the app.
CI runs `check` and `test` on every pull request (`.github/workflows/ci.yml`).

## Contributing

Contributions are always welcome! If you find a bug or have a feature request, please open an issue on the GitHub repository.
```
app.js entry point: exported as the Vercel Function, or listens locally
vercel.json Vercel rewrites, cache headers and function settings
src/
site.js builds the app; serve() runs it as a normal server
config.js settings from the environment, validated at start-up
catalogue.js server copy of the playlists: refresh, back-off, disk snapshot
reporting.js one place for errors: logs + optional Sentry
tidal/ TIDAL auth, API client (retries, pagination), normalisation, mock
http/ Express app, security headers, report intake, asset versioning
views/ page templates
public/
css/app.css all styles
js/ browser modules: sync, store, grid, dialog, filter, report…
scripts/tidal-login.js one-time TIDAL sign-in helper
test/ tests
```

## License
## Licence

This project is licensed under the MIT License.
MIT. The Outfit font is licensed under the SIL Open Font License (`public/fonts/OFL.txt`).
Loading
Loading