You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Inside a `load`function (as well as in [form actions](the-form-element), [hooks](handle)and [API routes](get-handlers), which we'll learn about later) you have access to a `setHeaders`function, which — unsurprisingly — can be used to set headers on the response.
5
+
Bên trong một hàm `load`(cũng như trong [form actions](the-form-element), [hooks](handle)và [đường dẫn API](get-handlers), chúng ta sẽ tìm hiểu sau) bạn có quyền truy cập vào hàm `setHeaders`được sử dụng để thiết lập các header _(tiêu đề)_ trên response (phản hồi).
6
6
7
-
Most commonly, you'd use it to customise caching behaviour with the [`Cache-Control`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) response header, but for the sake of this tutorial we'll do something less advisable and more dramatic:
7
+
Thường thì bạn sẽ sử dụng nó để tùy chỉnh caching với [`Cache-Control`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control), nhưng trong bài hướng dẫn này, chúng ta sẽ làm một số điều ít được khuyến khích và kịch tính hơn:
8
8
9
9
```js
10
10
/// file: src/routes/+page.server.js
@@ -15,4 +15,4 @@ export function load(+++{ setHeaders }+++) {
15
15
}
16
16
```
17
17
18
-
(You may need to reload the iframe to see the effect.)
Copy file name to clipboardExpand all lines: content/tutorial/03-sveltekit/04-headers-and-cookies/02-cookies/README.md
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
---
2
-
title: Reading and writing cookies
2
+
title: Đọc và ghi cookies
3
3
---
4
4
5
-
The[`setHeaders`](headers)function can't be used with the`Set-Cookie` header. Instead, you should use the `cookies` API.
5
+
Hàm[`setHeaders`](headers)không thể được dùng với header`Set-Cookie`. Thay vào đó, bạn nên sử dụng API `cookies`.
6
6
7
-
In your `load`functions, you can read a cookie with`cookies.get(name, options)`:
7
+
Trong các hàm `load`của bạn, bạn có thể đọc cookie bằng cách sử dụng`cookies.get(name, options)`:
8
8
9
9
```js
10
10
/// file: src/routes/+page.server.js
@@ -17,7 +17,7 @@ export function load(+++{ cookies }+++) {
17
17
}
18
18
```
19
19
20
-
To set a cookie, use `cookies.set(name, value, options)`. It's strongly recommended that you explicitly configure the `path`when setting a cookie, since browsers' default behaviour — somewhat uselessly — is to set the cookie on the parent of the current path.
20
+
Để đặt một cookie, sử dụng `cookies.set(name, value, options)`. Khuyến khích bạn nên cấu hình rõ `path`_(đường dẫn)_ khi đặt một cookie, vì hành vi mặc định của trình duyệt — hơi vô ích — là đặt cookie ở phần cha của đường dẫn hiện tại.
21
21
22
22
```js
23
23
/// file: src/routes/+page.server.js
@@ -32,9 +32,9 @@ export function load({ cookies }) {
32
32
}
33
33
```
34
34
35
-
Now, if you reload the iframe, `Hello stranger!`becomes `Hello friend!`.
35
+
Bây giờ, nếu bạn tải lại iframe, `Chào người lạ!`sẽ trở thành `Chào bạn!`.
36
36
37
-
Calling `cookies.set(name, ...)`causes a `Set-Cookie`header to be written, but it _also_ updates the internal map of cookies, meaning any subsequent calls to `cookies.get(name)`during the same request will return the updated value. Under the hood, the`cookies`API uses the popular `cookie`package — the options passed to `cookies.get`and`cookies.set`correspond to the `parse`and`serialize`options from the `cookie`[documentation](https://github.com/jshttp/cookie#api). SvelteKit sets the following defaults to make your cookies more secure:
37
+
`cookies.set(name, ...)`làm tiêu đề `Set-Cookie`sẽ được ghi, nhưng nó _cũng_ cập nhật bản đồ nội bộ của cookies, có nghĩa là sau đó nếu `cookies.get(name)`được gọi trong cùng một yêu cầu sẽ trả về giá trị đã được cập nhật. Tiến trình này như sau, API`cookies`sử dụng package `cookie`thường gặp — các tùy chọn được truyền vào `cookies.get`và`cookies.set`tương ứng với các tùy chọn cho `parse`và`serialize`từ [tài liệu](https://github.com/jshttp/cookie#api) của `cookie`. SvelteKit đặt các giá trị mặc định sau để làm cho cookie của bạn an toàn hơn:
0 commit comments