Skip to content

Posts ​

A post is one editorial page — a blog article or a travel guide. Its unique identity is canonicalUrl; its slug is only unique inside a country/city/category namespace.

Model ​

prisma
model Post {
  id              String    @id @default(cuid())
  title           String?
  slug            String
  content         String
  coverImage      String?
  status          String    @default("draft")
  scheduledAt     DateTime?
  publishedAt     DateTime?
  views           Int       @default(0)
  metaTitle       String?
  metaDescription String?
  meta            Json?
  toc             Json?
  jsonLdSchema    Json?
  tags            String?
  canonicalUrl    String    @unique
  urlHistory      String[]  @default([])
  cityHandle      String?

  blogCategoryId String?
  subCategoryId  String?
  authorId       String?
  cityId         String?
  countryId      String?

  @@unique([countryId, cityId, blogCategoryId, slug])
}
FieldNotes
statusdraft, scheduled, published, trash
scheduledAtWhen the every-minute cron should publish it
publishedAtSet on publish, cleared when moved out of published
tocExtracted from the sanitised HTML on write
metaFree-form JSON for word count, reading time, focus keyphrase
tagsComma-separated string, not a relation
cityHandleStored directly so old posts without a City row still resolve
urlHistoryEvery previous canonical URL

Creating and editing ​

The admin form collects title, slug, Tiptap HTML content, cover image, category and optional subcategory, author, tags, status/schedule, SEO fields, JSON-LD, and country/city.

On create:

  • slug is slugified. If it is missing the request is rejected.
  • The canonical URL is assembled from country handle, city handle, category slug and slug, skipping default.
  • A duplicate slug in the same (countryId, cityId, blogCategoryId) namespace returns 409.
  • A duplicate canonicalUrl returns 409.
  • Content is sanitised, TOC is extracted, and publishedAt is set only when the initial status is published.

On edit:

  • The post is looked up by id when supplied, otherwise by the path slug.
  • An author cannot edit another author's post; a super admin can edit any.
  • Changing the slug, city, country or category recomputes canonicalUrl and pushes the old value onto urlHistory.
  • Content is sanitised again, absolute image URLs are stripped, internal links not in the link bank are removed, and the TOC is rebuilt.
  • Publishing revalidates the frontend. Every edit appends an audit entry.

Category "default"

The sentinel subcategory and country named/slugged default are removed from a post on save. They exist only so the schema has a fallback row.

Status lifecycle ​

text
draft ──▶ scheduled ──(cron, every minute)──▶ published
  │              │                                │
  └──────────────┴──────────────▶ trash ◀─────────┘
                                    │
                              restore (→ draft) or delete permanently
ActionEndpointEffect
TrashDELETE /api/posts/delete/:postIdSets status = "trash"
Delete permanentlyDELETE /api/posts/delete/:postId?permanent=trueRemoves the row
RestorePATCH /api/posts/restore/:idSets status = "draft"

Authors can only trash, restore or permanently delete their own posts. A super admin can act on any post.

Listing and filters ​

GET /api/posts (admin) accepts:

ParamBehaviour
statusdraft, scheduled, published, or all (default)
trashtrue lists only trashed posts
categoryCategory id, or all
searchCase-insensitive title contains
page, limitPagination

A non-super-admin only sees their own posts. The default view excludes trash and orders by updatedAt desc, then id desc. The list selects summary columns only, not the full content.

Public reads ​

EndpointBehaviour
GET /api/posts/publishedPublished only. Filters search (title or meta description), category (slug), country (handle), city (handle). Ordered by publishedAt desc.
GET /api/posts/publicPublished only, max 50 per page, same category/country/city filters, no search.
GET /api/posts/category/:slugPublished posts in one category.
GET /api/posts/author/:idPublished posts by author, ordered by updatedAt desc. No pagination.
GET /api/posts/:slugSingle post. Also returns related (same category, 3) and youMayAlsoLike (tag overlap, 5).
GET /api/posts/resolve/:slug{ exists, canonicalPath, slug } for any current or historical path.

The single-post endpoint accepts a full path in the URL segment (/api/posts/anything), or query parameters path, country, category, city. A full path always wins over a bare slug because the slug alone may be ambiguous. A 25-character value is also tried as a cuid.

Responses normalise canonicalUrl, removing default segments. Related posts include their excerpt, category and subcategory; youMayAlsoLike is built from tag overlap.

URL rules ​

text
/{countryHandle}/{cityHandle}/{categorySlug}/{slug}

Every segment except the slug is optional. Examples:

DataCanonical URL
Country Nepal, city Pokhara, category travel-guide, slug best-time/nepal/pokhara/travel-guide/best-time
Country Nepal, category blog, slug permits/nepal/blog/permits
No relations, slug about-us/about-us

Lookups always try the exact canonicalUrl, then the normalised value, then urlHistory. A bare slug that matches more than one row is treated as ambiguous and returns no post rather than guessing.

Categories and subcategories ​

PostCategory and SubCategory are independent two-level taxonomies: a post has one optional category and one optional subcategory, but the subcategory is not scoped to the parent category.

Both models carry featureImage, secondaryImage, subtitle, content, metaTitle and metaDescription, so a category page can be a full landing page. GET /api/post-categories supports q, page and limit and returns a post count per category.

Writes require the categories or subcategories permission. Slugs are slugified; a duplicate slug is rejected by the unique constraint.

Caching ​

Every published post is cached by the frontend under the tag post-<slug>. Publishing, updating a published post, or the scheduler promotes a scheduled post triggers the revalidation webhook. A post edited while still a draft sends no revalidation because no public page exists yet.

Built with VitePress