---
id: serverless-netlify
title: "Netlify"
shortTitle: "Netlify"
track: serverless
trackTitle: "Serverless"
order: 4
level: normal
status: published
estimatedMinutes: 25
prerequisites:
  - serverless-gcp
next: monitoring-prometheus
reviewedAgainst: 2026-10-12
sources:
  - title: "Netlify Functions overview"
    url: https://docs.netlify.com/build/functions/overview/
  - title: "File-based configuration"
    url: https://docs.netlify.com/build/configure-builds/file-based-configuration/
---

# Netlify

Netlify phục vụ site tĩnh và chạy function cạnh site đó. Gần Vercel ở bài edge hơn là gần một cluster Kubernetes. Bài này đọc file cấu hình và chỗ để function, không tạo site.

## Để làm gì

Bạn đọc `netlify.toml`, biết lệnh build nào sẽ chạy, và không đưa database vào function chỉ vì form cần lưu một dòng.

## Gồm những gì

- `netlify.toml`: `command`, `publish`.
- Function trong thư mục mặc định.
- Khác Cloudflare Workers: đây là nền tảng site, không phải chỗ tự viết reverse proxy cho mọi request.

Đừng đăng ký team Netlify, và đừng nối Git trong bài này.

## Dùng khi nào

Frontend tĩnh cần một function nhỏ, ví dụ nhận form. App đã có API riêng thì Netlify chỉ host file đã build. Đừng biến function thành backend thứ hai.

Đừng để secret trong biến build rồi in ra file JS gửi cho trình duyệt. Biến có tiền tố public sẽ ra client. Docs của Netlify phân biệt biến build và biến chỉ trên server.

## Kiến thức cốt lõi

`netlify.toml` ở gốc repo. `[build] command` là lệnh tạo site, ví dụ `npm run build`. `publish` là thư mục chứa file tĩnh sau build. Sai thư mục thì site lên nhưng 404 mọi trang.

Function là file trong `netlify/functions`, trừ khi cấu hình đổi thư mục. Netlify chạy file đó khi có request tới đường dẫn function. Timeout và bộ nhớ có trần, cùng kiểu các bài serverless trước: không giữ file cho request sau, và đừng mở kết nối dài tới database nếu chưa có chỗ pool.

Deploy preview tạo một URL cho pull request. Tiện để xem frontend. Đừng trỏ preview vào database production.

## Lệnh và code mẫu

`netlify.toml`:

```toml
[build]
  command = "npm run build"
  publish = "dist"
```

Một function tối thiểu, cú pháp hiện tại của docs, chỉ để đọc:

```js
export default async () => {
  return new Response("ok");
};
```

Không chạy `netlify deploy`.

## Thực hành

1. Chỉ thư mục sẽ được phục vụ.
2. Nói một việc không đưa vào function này: ghi đơn hàng vào Postgres production.
3. Không tạo site.

## Kết quả mong đợi

`publish` là `dist`. Function không phải chỗ ghi dữ liệu bền của hệ thống chính.

## Checklist

- [ ] `publish` khớp thư mục tool build thực sự ghi ra.
- [ ] Biến bí mật không có mặt trong bundle gửi cho trình duyệt.
- [ ] Preview không dùng credential production.

## Lỗi thường gặp

**`publish = "build"` trong khi Vite ghi `dist`.** Deploy xanh, site trống. Mở artifact hoặc thư mục local sau `npm run build` và sửa `publish`.

**Function gọi thẳng database bằng connection string trong repo.** String lộ là sửa được dữ liệu. Để API đã có phần xác thực nhận request, function chỉ chuyển tiếp nếu thật sự cần.

## Nguồn

1. [Functions overview](https://docs.netlify.com/build/functions/overview/) — function chạy cạnh site.
2. [File-based configuration](https://docs.netlify.com/build/configure-builds/file-based-configuration/) — `netlify.toml`.

## Tiếp theo

[Prometheus](../monitoring/01-prometheus.md)
