Skip to content

6. Native PHP + HTMX Blog, SEO & Media Layer

Repository: Webpage (planovi-webpage) • Rule Prefix: BR-BLG • Documented Rules: 10 • Open [TEAM_WHY_NEEDED] Questions: 2

1. Business Purpose & Actors

  • Problem solved: Publishes bilingual SEO/GEO-optimized articles with reactive category/search filtering (zero full-page reload via HTMX), cross-language article linking via translation_key, dynamic XML sitemap generation, LLM discovery (llms.txt), and headless CMS publishing directly into Supabase (website.blog_posts + blog-media Storage bucket).
  • Actors: Readers searching/reading articles (/blog, /blog/{slug}); Search & AI crawlers (/sitemap.xml, /llms.txt); Editorial/n8n automation publishing content directly via Supabase REST/Storage APIs (supabase/API_EXAMPLES.md).

2. All Features & End-to-End Flows

  • Flow A — Blog Index SSR & HTMX Reactive Filtering (blog.php, components/blog-grid-partial.php, includes/blog-repository.php):
    • Trigger (Full Load): GET /blog[?category=...&q=...] renders full page SSR with up to 24 posts (get_blog_posts($current_lang, 24, $category, $search)) and category pills (get_blog_categories($current_lang) fetching up to 50 posts to derive unique categories).
    • Trigger (HTMX Live Update): Typing in #blog-search-input (hx-trigger="keyup changed delay:350ms, search", hx-include="#blog-filter-category") or clicking a .category-pill (hx-include="#blog-search-input") fires GET /blog?... with header HX-Request: true and hx-push-url="true".
    • Processing: blog.php:9-13 detects !empty($_SERVER['HTTP_HX_REQUEST']), queries get_blog_posts($current_lang, 24, $category, $search), renders ONLY components/blog-grid-partial.php, and exits. HTMX swaps #blog-results and toggles aria-busy via htmx:beforeRequest / htmx:afterOnLoad.
  • Flow B — Single Article View & Bilingual Slug Switching (blog-post.php, includes/blog-repository.php:92-183):
    • Trigger: GET /blog/{slug} (rewritten to blog-post.php?slug={slug}).
    • Validation: Extracts slug from $_GET['slug'] or fallback regex on REQUEST_URI; validates via validate_slug().
    • Lookup: Queries website.blog_posts?slug=eq.<slug>&is_published=eq.true&limit=1 (8s timeout). If found and translation_key is non-empty, fires a second query (4s timeout) for translation_key=eq.<key>&lang=eq.<opposite>&is_published=eq.true&limit=1 to populate translated_slug.
    • Not Found: Returns HTTP 404 HTML page with <meta name="robots" content="noindex, follow">.
    • Found: Sets $custom_lang_urls so navbar PL/EN switcher routes to /blog/{translated_slug}?lang={other} (or /blog?lang={other} if untranslated). Sets <html lang="<?= $post['lang'] ?>">. Renders sanitized HTML body (sanitize_blog_html()), media via render_blog_media(), tags, LinkedIn/X/Clipboard share buttons, Schema.org BlogPosting + BreadcrumbList JSON-LD, hreflang alternates, and up to 3 related posts (same category excluding self; if < 2, falls back to latest posts in same language excluding self).
  • Flow C — Dynamic XML Sitemap (sitemap.php) & llms.txt:
    • GET /sitemap.xml: Emits Content-Type: application/xml; charset=utf-8 containing 4 static pages (/, /blog, /documentation, /status) + up to 50 PL posts + up to 50 EN posts with xhtml:link rel="alternate" hreflang cross-links matched by translation_key.
    • GET /llms.txt: Static markdown file summarizing PLANOVI architecture, modules, pricing tiers, and links for LLMs.
  • Flow D — Headless CMS Ingestion (supabase/API_EXAMPLES.md, supabase/website_schema.sql:232-279):
    • External clients (n8n/Postman/cURL) upload assets to Supabase Storage POST /storage/v1/object/blog-media/{filename} (service_role), then insert/update posts via POST/PATCH /rest/v1/blog_posts (Content-Profile: website, service_role).

3. Hard Business Rules, Validations, Calculations & Edge Cases

  • BR-BLG-01 (Query Clamping & Sanitization): get_blog_posts() constrains $lang to ['pl', 'en'] (default 'pl'), $limit to 1..50, $offset >= 0. Category and search terms pass through sanitize_postgrest_query() before building category=eq.<clean> and or=(title.ilike.*<clean>*,excerpt.ilike.*<clean>*) (includes/blog-repository.php:21-55).
  • BR-BLG-02 (Mock Data Fallback Asymmetry):
    • In get_blog_posts(): returns get_fallback_blog_posts() ONLY if Supabase is unconfigured/placeholder OR HTTP request fails/non-2xx/invalid JSON. A valid 200 [] from Supabase returns [] without fallback (includes/blog-repository.php:29-83).
    • In get_blog_post_by_slug(): falls through to mock search even when Supabase returns 200 [] (includes/blog-repository.php:121,161-180), so the 4 hardcoded mock slugs remain publicly accessible even if deleted/unpublished in Supabase!
  • BR-BLG-03 (No Pagination UI & Hard Caps): blog.php requests a fixed limit=24 with offset=0 and no pagination controls (blog.php:10,16); get_blog_categories() and sitemap.php cap at 50 posts per language (includes/blog-repository.php:193, sitemap.php:55-56), so posts beyond #24 are invisible on /blog and beyond #50 are omitted from /sitemap.xml and category discovery.
  • BR-BLG-04 (Media Rendering & MIME Mismatch): render_blog_media() defaults missing cover_media_url to /assets/dashboard.jpg. Renders <video autoplay muted loop playsinline poster="/assets/dashboard.jpg"> when cover_media_type === 'video' OR URL matches /\.(mp4|webm)$/i, but hardcodes type="video/mp4" even for .webm files (includes/blog-repository.php:210-225).
  • BR-BLG-05 (HTML-Only Rendering Despite Schema Comment): Both supabase/website_schema.sql:182 and supabase/API_EXAMPLES.md:62 claim content supports "HTML lub Markdown", yet blog-post.php:252 passes content directly to sanitize_blog_html() with zero Markdown conversion.
  • BR-BLG-06 (Storage Bucket Constraints): Bucket blog-media is public-read (anon, authenticated, service_role), service_role-write, max file size 104,857,600 bytes (100 MB), allowed MIME types: image/jpeg, image/png, image/webp, image/gif, image/svg+xml, video/mp4, video/webm (supabase/website_schema.sql:240-278).
  • BR-BLG-07 (Sitemap updated_at Missing Column Bug): get_blog_posts() selects 14 explicit columns excluding updated_at (includes/blog-repository.php:31), yet sitemap.php:92,112 checks $post['updated_at'] first — so <lastmod> never reflects post edits and always falls back to published_at.
  • BR-BLG-08 (Implicit Global event Bug in Category Pill Click): blog.php:161-167 calls event.target.classList.add('active') without declaring event as a parameter in setCategory(cat). In browsers without legacy window.event (e.g., Firefox), clicking a category pill throws ReferenceError: event is not defined, preventing active pill highlighting.
  • BR-BLG-09 (Hardcoded Production Domain in SEO Tags): index.php, blog.php, blog-post.php, and sitemap.php hardcode https://planovi.app for canonical, hreflang, og:url, and sitemap <loc> even when deployed to dev-webpage.planovi.app.
  • BR-BLG-10 (Direct Public Execution of Partial): components/blog-grid-partial.php:7-13 self-initializes if $posts is unset, making /components/blog-grid-partial.php directly reachable via HTTP.

4. State Machines & Data Lifecycle

website.blog_posts.is_published is a binary boolean (true = publicly visible via RLS policy "Allow public read published blog posts", false = draft/hidden from anon and from queries filtering is_published=eq.true). updated_at is auto-maintained via trg_blog_posts_updated_at.

5. Technical Contract (Inputs → Processing → Side Effects)

  • Endpoints: GET /blog (?category=&q=&lang=, full HTML or HTMX partial when HX-Request present); GET /blog/{slug} (blog-post.php); GET /sitemap.xml (sitemap.php); GET /llms.txt.
  • DB Table (supabase/website_schema.sql:172-229): website.blog_posts (id UUID PK, created_at, created_by, updated_at, slug TEXT UNIQUE, translation_key TEXT, lang VARCHAR(5), title, excerpt, content, category, tags TEXT[], cover_media_url, cover_media_type, author_name, author_role, reading_time_min INT, meta_title, meta_description, meta_keywords, is_published BOOLEAN, published_at). Indexes: slug, (lang, is_published, published_at DESC), translation_key, category.
  • Storage Bucket: storage.buckets id 'blog-media' (public read, 100 MB limit).

6. Cross-Repository Dependencies

Caller → CalleeProtocol & AuthExact Endpoint / Contract
planovi-webpage → Supabase (planovi-backend DB)HTTPS PostgREST (apikey + Authorization: Bearer, Accept-Profile: website)GET /rest/v1/blog_posts
External n8n / Editorial CMS → Supabase (planovi-backend DB & Storage)HTTPS (service_role, Content-Profile: website)POST/PATCH /rest/v1/blog_posts, POST/DELETE /storage/v1/object/blog-media/*

7. Why It Works This Way (Decisions, Trade-offs & Workarounds)

  • Native PHP SSR + HTMX: Chosen for instant crawler indexability (SEO/GEO) without a JavaScript SPA bundle while providing live search/category UX via HTMX (blog.php:55, commit ca2682d).
  • Dedicated website PostgreSQL schema: Isolates marketing tables (website_form_messages, waitlist, blog_posts) from core application tables in public while adhering to company audit columns (id, created_at, created_by, updated_at) (supabase/website_schema.sql:1-13).
  • [TEAM_WHY_NEEDED: Why does get_blog_post_by_slug() fall back to hardcoded mock posts even when Supabase responds 200 [], and should mock fallback be gated by an explicit config flag?]
  • [TEAM_WHY_NEEDED: Is Markdown rendering planned for blog_posts.content (as mentioned in website_schema.sql:182 and API_EXAMPLES.md:62), or should those comments be updated to HTML-only?]

8. Debugging, Logs & Common Failure Modes

Symptom / Error MessageRoot Cause in CodeHow to VerifyFix
Blog displays hardcoded mock articles (Jak Agent AI optymalizuje...) instead of Supabase rowssupabase.url / supabase.key missing, website schema not exposed in Supabase PostgREST Exposed schemas, or PostgREST returned non-2xx (includes/blog-repository.php:29-83)curl -H "Accept-Profile: website" -H "apikey: <key>" "<url>/rest/v1/blog_posts?limit=1"Add website to Supabase API Exposed Schemas and verify supabase.url/key
Published article returns 404Slug contains uppercase/underscores failing validate_slug(), or is_published = falseCheck slug & is_published in website.blog_postsUse lowercase kebab-case slug and set is_published = true
Category pill active border does not move in FirefoxsetCategory() uses implicit event.target (blog.php:166)Open Firefox DevTools Console and click a category pillPass this into setCategory(cat, this)