Introducing a new way to review your PRs.

Read the announcement

iv-org / invidious

Invidious is an alternative front-end to YouTube

View on GitHub

19 documents · Updated Oct 12, 2026

docs / rendering-api-serialization-and-localization

Rendering, API serialization, and localization

Invidious turns fetched feature data into two main public forms: HTML pages for people and structured responses for programs. Shared ECR templates compose page shells and feature parts, JSON writers keep API objects consistent, and the request locale controls translated labels, dates, counts, and text in both forms.

Response transformation model

The response layer sits after route handling and before the browser or API client receives data. A route first prepares feature data and request preferences. It then selects either a template flow for HTML or a JSON builder flow for an API response.

flowchart LR
    A[Feature data] --> B[Route handler]
    B --> C{Response surface}
    C --> D[Shared page layout]
    D --> E[Feature template and components]
    E --> F[Localized HTML and asset links]
    C --> G[JSON writer]
    G --> H[Localized API fields and nested objects]
    F --> I[Browser]
    H --> J[Programmatic client]

The same domain value can serve both surfaces without making the surfaces identical. HTML can include controls, links, scripts, and visual state. API output exposes a stable object shape with fields such as identifiers, titles, counts, thumbnails, and playback data. The API v1 transformation layer is described in API v1 response serialization.

References: src/…/search.cr• L39-95 src/…/search.cr• L2-23 src/…/macros.cr• L51-63

HTML view composition

HTML rendering uses a shared layout around a feature view. The route supplies a context with feature data, preferences, request state, and any prepared fragments. The view fills the page body and can add head content such as titles, metadata, or page-specific styles.

Shared page shell

src/invidious/views/template.ecr is the common page shell. It sets the document language and theme, includes shared styles and scripts, renders the navigation and user controls, inserts the feature content, and adds the footer. This makes a page-specific template responsible for its content while the shell keeps site-wide behavior aligned.

The shell also places small data objects in JSON script elements for browser code. For example, signed-in pages pass notification text to the notification client, while feature views pass playback or pagination state to their scripts.

References: src/…/template.ecr• L1-30 src/…/template.ecr• L31-114 src/…/template.ecr• L116-193

Reusable view components

Components are shared view fragments for repeated interface roles. Search boxes, feed menus, item cards, channel information, players, and pagination can therefore be used by several feature pages without creating separate versions of the same interface.

The item component chooses a view based on the item kind. It presents videos, channels, playlists, hashtags, and parse-error placeholders with the links, counts, thumbnails, and actions that fit each kind. Paginated pages wrap the item grid with navigation and pass the navigation labels and direction state to browser code.

References: src/…/item.ecr• L1-32 src/…/item.ecr• L33-111 src/…/item.ecr• L112-217 src/…/items_paginated.ecr• L1-21

Feature page assembly

Feature pages combine page-specific data with shared components. A search page adds filter controls and an empty-result message before it delegates result display to the paginated item view. A channel page adds metadata and channel actions before it uses the same paginated item view for channel content.

The watch page follows the same model but adds a player, playback choices, comments state, and video metadata. The route prepares stream groups, captions, and related state before the template composes them into the page. This keeps data selection in the route and interface composition in the view.

References: src/…/search.ecr• L1-26 src/…/channel.ecr• L1-59 src/…/channel_info.ecr• L1-64 src/…/watch.cr• L120-202

Browser data and asset composition

The HTML interface is a mix of server-rendered markup and small browser features. Templates emit links to shared CSS and JavaScript, then place feature state in JSON elements or HTML data attributes so the browser can enhance the page without replacing the main render path.

The player component converts prepared stream and caption choices into media sources and tracks. It also passes a compact player configuration to player.js. Other pages use the same pattern for watched indicators, pagination, comments, subscriptions, playlists, and notifications. The shared helper client provides a common XHR and storage interface for these enhancements.

Static files are delivered by the HTTP asset layer rather than by feature templates. The layout adds an asset commit value to shared links so a changed asset receives a new browser URL. See HTTP middleware and static assets for the request path, cache policy, and asset serving rules.

References: src/…/template.ecr• L21-29 src/…/template.ecr• L177-191 src/…/player.ecr• L4-10 src/…/player.ecr• L89-100 assets/…/_helpers.js• L42-114 src/invidious.cr• L79-81

API response shaping

API routes return JSON built for machine use rather than HTML fragments. A route sets the JSON content type, obtains the requested feature data, and writes an object or array through a serializer. Nested feature values use the same writers so a video or thumbnail has one shared representation across search, feeds, channels, playlists, and video endpoints.

Resource representations

The API v1 writers expose resource identity first, then descriptive data and related resources. A video representation can include metadata, translated relative dates, thumbnails, storyboards, captions, stream choices, music, and recommended videos. Search result types use distinct type values for videos, channels, playlists, hashtags, categories, and parse-error placeholders.

Thumbnails are a shared nested representation with quality, URL, width, and height. Stream URLs can use the local proxy when the request asks for local media. This means serialization does not only copy source fields. It also creates client-facing links and compatibility values that match the Invidious API contract.

References: src/…/video_json.cr• L6-77 src/…/video_json.cr• L79-159 src/…/video_json.cr• L161-279 src/…/common.cr• L3-17 src/…/serialized_yt_data.cr• L81-153 src/…/serialized_yt_data.cr• L175-291

API route integration

API route families own resource lookup and endpoint-specific envelopes. Search returns an array of typed items. Channel endpoints wrap lists in fields such as videos or relatedChannels. Video endpoints delegate the main object to the shared video writer. Errors stay JSON-shaped when lookup or parsing fails.

Some endpoints can return HTML inside a JSON response when a browser feature requests an AJAX fragment. The mix flow is an example: it first builds structured playlist data, then turns that data into playlistHtml and next-item fields when the format asks for HTML. This is a boundary case between API serialization and browser view composition.

References: src/…/videos.cr• L7-27 src/…/search.cr• L2-23 src/…/channels.cr• L17-27 src/…/channels.cr• L47-132 src/…/misc.cr• L133-173 src/…/handlers.cr• L22-60

Locale selection and translated output

Localization means selecting text and formatting rules for a language. The request preference named locale is the value used by templates, front-end helpers, and API writers. It can come from saved preferences, a query override, or browser language negotiation when no preference cookie exists.

Request locale

The shared request setup loads preferences before routes run. It reads the preference cookie first. If no cookie exists, it negotiates Accept-Language against the supported locale set. A query hl value then overrides the locale for the current request and is stored in the request preferences used by later rendering.

The preferences page lets a user select a locale and saves it with the other display settings. The locale list is backed by the JSON files in locales, with English as the fallback language. The supported locale names and translation data must remain aligned.

References: src/…/before_all.cr• L14-31 src/…/before_all.cr• L110-118 src/…/preferences.cr• L31-34 src/…/preferences.cr• L116-123 src/…/i18n.cr• L16-25 src/…/i18n.cr• L96-103

Translation and interpolation

I18n.translate resolves a key in the requested locale and falls back to English when the locale or key is not available. It supports a single replacement such as a relative date and named replacements for text placed in browser data. A missing key is returned as the key and logged so the page remains usable while the translation set is incomplete.

Count text uses a separate plural resolver. It selects a plural form for the locale, applies a number format, and inserts the count into the selected translation. This matters for labels such as views, videos, subscribers, and comments because the same count can need different words in different languages. Right-to-left locales also change directional pagination arrows so the control matches the reading direction.

References: src/…/i18n.cr• L106-153 src/…/i18n.cr• L156-199 src/…/i18n.cr• L201-208 src/…/i18next.cr• L135-187 src/…/pagination.cr• L6-60

Localized API fields

API responses keep stable field names and identifiers, but some display fields use the request locale. Relative publication text and compact view counts are examples. The client can therefore receive a consistent schema with human-readable fields that match the same language as the related web request.

This split is important: machine clients should use numeric timestamps and counts for logic, while translated text supports direct display. A change to a translation key or plural rule can affect both HTML labels and API display fields when they share the same localization helper.

References: src/…/video_json.cr• L22-30 src/…/video_json.cr• L267-274 src/…/serialized_yt_data.cr• L114-124 src/…/search.cr• L2-20

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep every new template translation key present in the English base locale before adding or updating other locale files. Missing base keys return the key itself and can expose internal names in the interface.
  • Keep plural translation keys aligned with the suffixes selected by the plural resolver. A translation can exist but still fall back if its count form does not match the locale rule.
  • Keep the request locale source aligned with all consumers of preferences.locale. Query overrides, saved preferences, and browser negotiation must produce one value for the page, API text, and embedded JSON data.
  • Keep shared component context names aligned with the route variables that render them. A missing items, locale, pagination fragment, or feature object can break many pages at once.
  • Keep API resource writers and endpoint envelopes aligned when a field or nested representation changes. Search, feed, channel, playlist, and video clients can depend on the same typed item or thumbnail shape.
  • Keep asset link versioning aligned with the asset change process. A stale asset commit value can leave a browser with old scripts or styles after a server-side view change.

Important failure modes

TriggerConsequenceGuard
A locale is not in the loaded locale setOutput falls back to English for translated text.I18n.translate and translate_count use en-US fallback behavior.
A key is missing from en-USThe internal key appears in the output and a warning is logged.Add the key to the base locale before relying on it in templates or API fields.
A count translation lacks the selected plural formThe same-locale singular form or English translation is used.Keep locale JSON plural keys aligned with the resolver rules.
A shared component receives the wrong item kind or missing contextA page can render the wrong card or fail while building the view.Keep route-provided data and the component’s item/context contract aligned.
An API route bypasses the shared JSON writer for a common resourceClients can see different fields or nested shapes for the same resource.Reuse the API v1 resource and thumbnail writers.
A server-side view changes without a matching asset version changeBrowsers can run stale JavaScript or CSS against new markup.Use the asset commit value on shared asset links and rebuild it when assets change.

Key files

FileWhy to read it
src/invidious/helpers/macros.crShows the common template and component render entry points.
src/invidious/views/template.ecrShows the shared HTML shell, request state, common assets, and browser data.
src/invidious/views/components/item.ecrShows how different feature item types become shared interface cards.
src/invidious/views/components/player.ecrShows how playback data and captions become HTML media sources and browser state.
src/invidious/routes/watch.crShows how a feature route prepares data for a complete page render.
src/invidious/jsonify/api_v1/video_json.crShows the main API v1 video representation and nested media data.
src/invidious/jsonify/api_v1/common.crShows the shared thumbnail representation.
src/invidious/helpers/serialized_yt_data.crShows typed search items and their JSON representations.
src/invidious/helpers/i18n.crDefines locale loading, fallback, interpolation, count formatting, and text direction.
src/invidious/helpers/i18next.crDefines the plural form selection used by localized counts.
src/invidious/routes/before_all.crShows how request locale and preferences reach web and API handlers.
locales/en-US.jsonProvides the base translation keys and plural forms.
API v1 response serializationChild overview of the API transformation contract.
HTTP middleware and static assetsRelated overview of asset delivery and shared HTTP response behavior.
Web and API route familiesRelated overview of the routes that select HTML and API response surfaces.