Bảng Routes hiện URL đầy đủ cho từng path khách truy cập
URL tuyệt đối

DINHDANH Creator · Bài AM10 · URL · Dữ liệu rỗng · Rate limit

Ba chủ đề hay nhầm
link · rỗng · giới hạn gọi API

URL visitor khác API vdisk. Đọc lần đầu luôn rỗng - không phải lỗi. Mutation quá dày có thể bị chặn IP tạm thời.

Mục tiêu: Link đúng domain; xử lý content: [] bình thường; luôn addRoute khi publish; bọc try/catch khi bị rate limit.

Trước: AM09 · Sau: AM11 (System files & CMS)

A. URL tuyệt đối - vì sao quan trọng

Platform inject vào mọi trang app. Mọi URL tương đối resolve từ gốc app, không từ thư mục template như blog/detail.html.

Visitor: https://domain.com/{url_name}/{route_path}
→ PHP đọc routes.json → serve HTML template
→ Head có 

site_url trong JS luôn là domain platform - có thể khác host với khi app gắn domain riêng. Ưu tiên lấy gốc từ thẻ .

Cách viếtKết quả
href="tag/js"✅ Đúng - slug dưới gốc app
getAbsoluteUrl('tag/js')✅ Rõ ràng - dùng cho location.href, JS build link
href="/tag/js"❌ Root domain - mất url_name
href="../tag/js"❌ .. nhảy ra ngoài app
href="blog/detail.html"❌ File template FS - visitor cần route slug, không phải .html

Quy tắc: Link điều hướng - getAbsoluteUrl(slug) hoặc href="slug" (không / đầu, không .., không .html). Slug phải có trong routes.json (addRoute).

Helper - copy vào HTML app

/** Ưu tiên  - đúng cả custom domain */
function getAppBaseUrl() {
  const base = document.querySelector('base[href]')?.href
  if (base) return base.replace(/\/$/, '') + '/'
  const site = (window.appifio?.global?.apps?.site_url || window.location.origin).replace(/\/$/, '')
  const name = window.appifio?.global?.apps?.url_name || ''
  return name ? `${site}/${name}/` : `${site}/`
}

/** path = slug route: '' | 'admin' | 'my-post' | 'tag/javascript' */
function getAbsoluteUrl(path) {
  const base = getAppBaseUrl().replace(/\/$/, '')
  const segment = String(path || '').replace(/^\//, '')
  return segment ? `${base}/${segment}` : `${base}/`
}
Ví dụ (url_name=test-blog)Kết quả
getAbsoluteUrl('')Trang chủ app
getAbsoluteUrl('admin')Panel admin
getAbsoluteUrl('login')Trang đăng nhập
getAbsoluteUrl('bai-viet-ve-ai')Chi tiết bài (route slug)
getAbsoluteUrl('tag/javascript')Trang tag lồng

B. Dữ liệu rỗng & route bắt buộc

Hai khái niệm tách biệt - đừng nhầm:

TầngKhi chưa cóVí dụ
API vdisk (dữ liệu JSON)success: true + rỗng - không 404readList → content: []
Physical FS (HTML trên disk)success: false nếu chưa tạoreadFsFile('blog/detail.html')
URL visitor (routing)HTTP 404 trang webSlug không có trong routes.json

B.1 - Đọc vdisk lần đầu

MethodKhi chưa có dữ liệu
readListcontent: []
readFile (flat)content: ""
readFile (indexed + id)content: null (không tìm thấy / bị lọc)
✅ Pattern đúng
async function loadBlogs() {
  const result = await appifio_client.appifio_readList('blog.json', ['id','title','slug'])
  if (result?.success) return result.data.content || []  // [] lần đầu - bình thường
  return []
}

// Thêm bài đầu tiên - backend tự tạo sổ
await appifio_client.appifio_appendData('blog.json', { title, slug, content })

// Flat config - giữ default nếu rỗng
let config = { theme: 'light' }
const res = await appifio_client.appifio_readFile('config.json')
if (res?.success && res.data.content && typeof res.data.content === 'object') {
  config = res.data.content
}
❌ Pattern sai
// Expect lỗi khi file chưa có - vdisk KHÔNG làm vậy
if (!result.success) { /* never on first read */ }

// JSON.parse - Aura đã parse sẵn
const items = JSON.parse(result.data.content)

// writeFile cho list - phá sharding indexed
await appifio_client.appifio_writeFile('blog.json', JSON.stringify([...]))

B.2 - Route bắt buộc cho URL visitor

Mặc định chỉ /urlname/ serve index.html. Mọi slug khác phải có entry trong routes.json - nếu không, visitor nhận 404 (không liên quan API vdisk).

// ❌ SAI - có data nhưng visitor 404
await appifio_client.appifio_appendData('blog.json', blogData)

// ✅ ĐÚNG - data + route + template HTML đã có trên FS
const { data: { id } } = await appifio_client.appifio_appendData('blog.json', blogData)
await appifio_client.appifio_addRoute(blogData.slug, 'blog/detail.html', 'blog')

// Route lồng tag / category
await appifio_client.appifio_addRoute('tag/javascript', 'blog/tag.html', 'tag')
await appifio_client.appifio_addRoute('category/cong-nghe', 'blog/category.html', 'category')

// Xóa bài → xóa route tương ứng
await appifio_client.appifio_updateData('blog.json', id, null)
await appifio_client.appifio_removeRoute(slug)

addRoute map slug → file HTML trên Physical FS - file blog/detail.html phải tồn tại (Panel hoặc writeFsFile). Chi tiết method: AM05.

C. Rate limit & IP block

Panel Backend hiện hạn mức request API theo tháng
Giới hạn API

Giới hạn tính theo IP + link (url_name) - mỗi app độc lập.

LoạiBị đếm?Khi vượt
Mutation (append, update, write, upload, route, đổi mật khẩu…)✅ Cao trong cửa sổ ngắnBlock IP tạm thời
executeBackendFunction✅ Riêng - cửa sổ dài hơnBlock IP tạm thời
Login sai mật khẩu✅ Ngưỡng thấpBlock IP tạm thời
Read (readList, getRoutes, getSession…)❌ Không limit-
logout❌ Không tính quota mutation-

Khi IP bị block: HTTP 429, client map err.blocked === true. Bạn không nhận thời gian còn lại hay lý do chi tiết - UI báo chung “Thử lại sau”.

try {
  await appifio_client.appifio_appendData('contacts.json', data)
} catch (err) {
  if (err.blocked) {
    alert('IP tạm bị chặn do gọi API quá nhiều. Vui lòng thử lại sau.')
    return
  }
  if (err.httpStatus === 401) { /* phiên hết hạn - chuyển login */ }
  throw err
}

Checklist AM10:

  1. Redirect / link dùng getAbsoluteUrl, không / đầu?
  2. List rỗng = [], không coi là lỗi?
  3. Publish có addRoute; xóa có removeRoute?
  4. Form public có try/catch + err.blocked?

DINHDANH Creator · Loạt Method · AM10