Skip to main content

Search, views and "Most read"

Search​

GET /v2/public/cms/{siteSlug}/entries?locale=vi&q=bon%20mang&page=1&limit=12
  • Searches the title, excerpt and body (the text only, not HTML tag names) of the translation in exactly that locale.
  • Accent- and case-insensitive: bon mang finds «Bổn mạng».
  • Matches one contiguous substring; words are not split: mang bon does not find «Bổn mạng». The % and _ characters are taken literally.
  • Searches only published posts, not static pages. Posts of an "unlisted" content type are not in the results.
  • Results are sorted newest first; featured posts are ignored (featured is always false).
  • q must be 2–100 characters long after trimming whitespace at both ends; outside that range you get 400 with message: "CMS_SEARCH_QUERY_INVALID".
  • Do not combine q with category, tag or sort (400 CMS_FILTER_CONFLICT).

Normalize the search term before calling, so you do not waste a rejected request and so every reader with the same term shares one cached copy:

function searchTerm(raw: string): string | null {
const term = raw.trim()
return term.length >= 2 && term.length <= 100 ? term : null
}

Search results are cached for up to 60 seconds and are not refreshed right away when staff edit a post.

Views​

POST /v2/public/cms/{siteSlug}/entries/{entrySlug}/view

Send it once each time a reader opens a post or page. Views feed the staff's statistics and the "Most read" block.

The correct way to call it, from the reader's browser:

function sendView(entrySlug: string): void {
if (typeof window === 'undefined') return // never during server rendering or a static build
try {
fetch(`${API}/entries/${encodeURIComponent(entrySlug)}/view`, {
method: 'POST',
keepalive: true,
credentials: 'omit',
}).catch(() => undefined)
} catch {
// ignore: a lost view must never break the page
}
}

Rules:

  • No request body, no headers (including Content-Type). That makes it a CORS "simple request", so the browser does not have to send an extra OPTIONS request.
  • It always returns 204, with no content, even when the view is not counted. Do not read the response and do not report errors to the reader.
  • Send it only from the browser. Sending it from a server or during a static build makes every view look like the same reader, so almost none are counted.
  • Do not send anything else about the reader: the API neither accepts nor needs personal information.

The server does not count a view when:

  • the same reader was already counted for that entry in the last 30 minutes (identified by a one-way hash of the IP address and User-Agent; this hash is used only to prevent double counting and is never logged or stored in the database);
  • the User-Agent looks like a machine (contains bot, crawl, spider, preview or slurp);
  • the entry does not exist, is unpublished, or the slug is malformed.

Views show up for staff and in "Most read" after about 30 seconds. An old (renamed) slug is still counted for the current entry.

This path has stricter rate limits than the read paths: see Limits.

"Most read"​

GET /v2/public/cms/{siteSlug}/entries?locale=vi&sort=popular&page=1&limit=6
  • The posts with the most views in the last 30 days (counted by UTC day); on a tie, the newer post comes first.
  • Only posts viewed at least once in that period are included, so the list may be shorter than limit, or even empty on a new site. Hide this block when data is empty.
  • Only page=1, and limit is 3, 6 or 12; anything else gets 400 with message: "CMS_POPULAR_QUERY_INVALID".
  • Do not combine it with category, tag or q.
  • meta.total equals the number of items actually returned.
  • Posts of an "unlisted" content type are not in the list.
Was this page helpful?