Skip to content

Commit a8cc570

Browse files
authored
docs:31-dịch-4-advanced-sveltekit-hooks (sveltevietnam#66)
1 parent 298efe1 commit a8cc570

File tree

14 files changed

+49
-46
lines changed

14 files changed

+49
-46
lines changed

content/tutorial/04-advanced-sveltekit/01-hooks/01-handle/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
title: handle
33
---
44

5-
SvelteKit provides several _hooks_ways to intercept and override the framework's default behaviour.
5+
SvelteKit cung cấp một vài _hooks_là cách để chặn và ghi đè lên hành vi mặc định của framework.
66

7-
The most elementary hook is `handle`, which lives in `src/hooks.server.js`. It receives an `event` object along with a `resolve` function, and returns a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response).
7+
Hook cơ bản nhất là `handle`, nằm trong `src/hooks.server.js`. Nó nhận một đối tượng `event` cùng với một hàm `resolve`, và trả về một [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response).
88

9-
`resolve` is where the magic happens: SvelteKit matches the incoming request URL to a route of your app, imports the relevant code (`+page.server.js` and `+page.svelte` files and so on), loads the data needed by the route, and generates the response.
9+
`resolve` là nơi điều kỳ diệu xảy ra: SvelteKit so khớp URL yêu cầu đến với một route của ứng dụng, imports mã tương ứng (`+page.server.js` và các tệp `+page.svelte` vv), tải dữ liệu cần thiết bởi route và tạo ra response _(phản hồi)_.
1010

11-
The default `handle` hook looks like this:
11+
`handle` hook mặc định trông như thế này:
1212

1313
```js
1414
/// file: src/hooks.server.js
@@ -17,7 +17,7 @@ export async function handle({ event, resolve }) {
1717
}
1818
```
1919

20-
For pages (as opposed to [API routes](get-handlers)), you can modify the generated HTML with `transformPageChunk`:
20+
Đối với trang (trái ngược với[API routes](get-handlers)), bạn có thể sửa đổi HTML được tạo ra bằng `transformPageChunk`:
2121

2222
```js
2323
/// file: src/hooks.server.js
@@ -31,7 +31,7 @@ export async function handle({ event, resolve }) {
3131
}
3232
```
3333

34-
You can also create entirely new routes:
34+
Bạn cũng có thể tạo ra các route hoàn toàn mới:
3535

3636
```js
3737
/// file: src/hooks.server.js
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
<h1>hello world</h1>
1+
<h1>Xin chào</h1>
22
<a href="/ping">ping</a>

content/tutorial/04-advanced-sveltekit/01-hooks/02-event/README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@
22
title: The RequestEvent object
33
---
44

5-
The `event` object passed into `handle` is the same object — an instance of a [`RequestEvent`](https://kit.svelte.dev/docs/types#public-types-requestevent) — that is passed into [API routes](get-handlers) in `+server.js` files, [form actions](the-form-element) in `+page.server.js` files, and `load` functions in `+page.server.js` and `+layout.server.js`.
5+
Đối tượng `event` (một instance của [`RequestEvent`](https://kit.svelte.dev/docs/types#public-types-requestevent)) được truyền vào `handle` cũng chính là `event` trong [API routes](get-handlers) `+server.js`, [form actions](the-form-element) `+page.server.js`, và các hàm `load` trong `+page.server.js` `+layout.server.js`.
66

7-
It contains a number of useful properties and methods, some of which we've already encountered:
7+
Nó chứa một số thuộc tính hữu ích và phương thức, một số trong số đó chúng ta đã gặp:
88

9-
* `cookies` — the [cookies API](cookies)
10-
* `fetch` — the standard [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API), with additional powers
11-
* `getClientAddress()` — a function to get the client's IP address
12-
* `isDataRequest``true` if the browser is requesting data for a page during client-side navigation, `false` if a page/route is being requested directly
13-
* `locals` — a place to put arbitrary data
14-
* `params` — the route parameters
15-
* `request` — the [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object
16-
* `route` — an object with an `id` property representing the route that was matched
17-
* `setHeaders(...)` — a function for [setting HTTP headers](headers) on the response
18-
* `url` — a [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) object representing the current request
9+
* `cookies`[API cookies](cookies)
10+
* `fetch`[Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) tiêu chuẩn, với các tính năng bổ sung
11+
* `getClientAddress()` — một hàm để lấy địa chỉ IP của client
12+
* `isDataRequest``true` nếu trình duyệt đang yêu cầu dữ liệu cho một trang trong quá trình điều hướng ở phía client, `false` nếu một trang/route đang được yêu cầu trực tiếp
13+
* `locals` — nơi để đặt dữ liệu tùy ý
14+
* `params` — các tham số route
15+
* `request`[Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object
16+
* `route` — một object với một thuộc tính `id` đại diện cho tuyến đường đã được khớp
17+
* `setHeaders(...)` — một hàm để [setting HTTP headers](headers) cho response
18+
* `url` — một [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) object đại diện cho request hiện tại
19+
20+
Để thuận tiện, ta có thể thêm dữ liệu vào `event.locals` trong `handle` để truy cập được trong các hàm `load` tiếp theo:
1921

20-
A useful pattern is to add some data to `event.locals` in `handle` so that it can be read in subsequent `load` functions:
2122

2223
```js
2324
/// file: src/hooks.server.js
@@ -31,7 +32,7 @@ export async function handle({ event, resolve }) {
3132
/// file: src/routes/+page.server.js
3233
export function load(+++event+++) {
3334
return {
34-
message: `the answer is ${+++event.locals.answer+++}`
35+
message: `Câu trả lời là ${+++event.locals.answer+++}`
3536
};
3637
}
3738
```
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export function load() {
22
return {
3-
message: `the answer is ???`
3+
message: `câu trả lời là ???`
44
};
55
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export function load(event) {
22
return {
3-
message: `the answer is ${event.locals.answer}`
3+
message: `câu trả lời là ${event.locals.answer}`
44
};
55
}

content/tutorial/04-advanced-sveltekit/01-hooks/03-handlefetch/README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
title: handleFetch
33
---
44

5-
The `event` object has a `fetch` method that behaves like the standard [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API), but with superpowers:
5+
Đối tượng `event` chứa phương thức `fetch` hoạt động giống như [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) tiêu chuẩn, nhưng với những tính năng đặc biệt:
66

7-
- it can be used to make credentialed requests on the server, as it inherits the `cookie` and `authorization` headers from the incoming request
8-
- it can make relative requests on the server (ordinarily, `fetch` requires a URL with an origin when used in a server context)
9-
- internal requests (e.g. for `+server.js` routes) go directly to the handler function when running on the server, without the overhead of an HTTP call
7+
- có thể thực hiện các yêu cầu có chứng thực trên server, vì nó kế thừa các header `cookie``authorization` từ yêu cầu đầu vào.
8+
- có thể thực hiện các yêu cầu với đường dẫn tương đối (relative requests) trên server (thông thường, `fetch` chỉ nhận URL có gốc (origin) khi sử dụng trong bối cảnh server).
9+
- internal requests _(yêu cầu nội bộ)_ (ví dụ: đối với route `+server.js`) được chuyển trực tiếp đến hàm handler khi chạy trên server, mà không phải cấu hình gọi HTTP.
10+
11+
Hành vi của nó có thể được sửa đổi bằng `handleFetch` hook, mặc định nó trông như thế này:
1012

11-
Its behaviour can be modified with the `handleFetch` hook, which by default looks like this:
1213

1314
```js
1415
/// file: src/hooks.server.js
@@ -17,7 +18,7 @@ export async function handleFetch({ event, request, fetch }) {
1718
}
1819
```
1920

20-
For example, we could respond to requests for `src/routes/a/+server.js` with responses from `src/routes/b/+server.js` instead:
21+
Ví dụ, thay vì phản hồi các yêu cầu đến `src/routes/a/+server.js` chúng ta có thể sử dụng phản hồi từ `src/routes/b/+server.js`:
2122

2223
```js
2324
/// file: src/hooks.server.js
@@ -31,4 +32,4 @@ export async function handleFetch({ event, request, fetch }) {
3132
}
3233
```
3334

34-
Later, when we cover [universal `load` functions](universal-load-functions), we'll see that `event.fetch` can also be called from the browser. In that scenario, `handleFetch` is useful if you have requests to a public URL like `https://api.yourapp.com` from the browser, that should be redirected to an internal URL (bypassing whatever proxies and load balancers sit between the API server and the public internet) when running on the server.
35+
Sau này, khi đề cập đến [universal `load` functions](universal-load-functions), chúng ta sẽ thấy rằng `event.fetch` cũng có thể được gọi từ trình duyệt. Trong tình huống đó, `handleFetch` sẽ hữu ích nếu bạn gởi yêu cầu từ trình duyệt đến một URL công khai như `https://api.yourapp.com` và các yêu cầu này cần được chuyển hướng đến một URL nội bộ (bypass qua bất kỳ proxy và load balancer nào đứng giữa server API và internet công cộng) khi chạy trên server.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export function GET() {
2-
return new Response('hello from a');
2+
return new Response('xin chào từ a');
33
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export function GET() {
2-
return new Response('hello from b');
2+
return new Response('xin chào từ b');
33
}

content/tutorial/04-advanced-sveltekit/01-hooks/04-handleerror/README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
title: handleError
33
---
44

5-
The `handleError` hook lets you intercept unexpected errors and trigger some behaviour, like pinging a Slack channel or sending data to an error logging service.
5+
Hook `handleError` giúp bạn chặn các lỗi không mong muốn _(unexpected errors)_ và kích hoạt một số hành vi, như gửi ping tới một kênh Slack hoặc gửi dữ liệu đến một dịch vụ theo dõi lỗi.
6+
7+
Như bạn nhớ từ [bài tập trước đó](error-basics), một lỗi là _unexpected_ nếu nó không được tạo ra với helper `error` từ `@sveltejs/kit`. Thông thường, điều này có nghĩa là có điều gì đó trong ứng dụng của bạn cần sửa. Hành vi mặc định là ghi lại lỗi:
68

7-
As you'll recall from an [earlier exercise](error-basics), an error is _unexpected_ if it wasn't created with the `error` helper from `@sveltejs/kit`. It generally means something in your app needs fixing. The default behaviour is to log the error:
89

910
```js
1011
/// file: src/hooks.server.js
@@ -13,32 +14,32 @@ export function handleError({ event, error }) {
1314
}
1415
```
1516

16-
If you navigate to `/the-bad-place`, you'll see this in action — the error page is shown, and if you open the terminal (using the button to the right of the URL bar), you'll see the message from `src/routes/the-bad-place/+page.server.js`.
17+
Nếu bạn chuyển đến `/the-bad-place`, bạn sẽ thấy điều này diễn ra - trang lỗi được hiển thị, và nếu bạn mở terminal (sử dụng nút ở bên phải của thanh URL), bạn sẽ thấy thông báo từ `src/routes/the-bad-place/+page.server.js`.
1718

18-
Notice that we're _not_ showing the error message to the user. That's because error messages can include sensitive information that at best will confuse your users, and at worst could benefit evildoers. Instead, the error object available to your application — represented as `$page.error` in your `+error.svelte` pages, or `%sveltekit.error%` in your `src/error.html` fallback — is just this:
19+
Lưu ý rằng chúng ta không hiển thị thông báo lỗi cho người dùng. Lý do là thông báo lỗi có thể bao gồm thông tin nhạy cảm,làm người dùng bối rối, và trong tình huống xấu có thể bị lợi dụng bởi kẻ xấu. Thay vào đó, đối tượng error sẵn có từ `$page.error` trong các trang `+error.svelte`, hoặc `%sveltekit.error%` trong tệp `src/error.html` dự phòng - chỉ là như thế này:
1920

2021
```js
2122
/// no-file
2223
{
23-
message: 'Internal Error' // or 'Not Found' for a 404
24+
message: 'Internal Error' // hoặc 'Not Found' cho 404
2425
}
2526
```
2627

27-
In some situations you may want to customise this object. To do so, you can return an object from `handleError`:
28+
Trong một số tình huống, bạn có thể muốn tùy chỉnh đối tượng này. Để làm điều đó, bạn có thể trả về một đối tượng từ `handleError`:
2829

2930
```js
3031
/// file: src/hooks.server.js
3132
export function handleError({ event, error }) {
3233
console.error(error.stack);
3334

3435
return {
35-
message: 'everything is fine',
36+
message: 'mọi thứ đều ổn',
3637
code: 'JEREMYBEARIMY'
3738
};
3839
}
3940
```
4041

41-
You can now reference properties other than `message` in a custom error page. Create `src/routes/+error.svelte`:
42+
Bây giờ bạn có thể tìm hiểu các thuộc tính khác ngoài `message` trong trang lỗi tùy chỉnh. Tạo `src/routes/+error.svelte`:
4243

4344
```svelte
4445
/// file: src/routes/+error.svelte
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<nav>
22
<a href="/">home</a>
3-
<a href="/the-good-place">the good place</a>
4-
<a href="/the-bad-place">the bad place</a>
3+
<a href="/the-good-place">nơi tốt</a>
4+
<a href="/the-bad-place">nơi xấu</a>
55
</nav>
66

77
<slot />
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export function load() {
2-
throw new Error('this is the bad place!');
2+
throw new Error('đây là nơi xấu!');
33
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<h1>you are in the bad place</h1>
1+
<h1>bạn đang ở nơi xấu</h1>
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<h1>you are in the good place</h1>
1+
<h1>bạn đang ở nơi tốt</h1>

content/tutorial/04-advanced-sveltekit/01-hooks/04-handleerror/app-b/src/hooks.server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export function handleError({ event, error }) {
22
console.error(error.stack);
33

44
return {
5-
message: 'everything is fine',
5+
message: 'mọi thứ đều ổn',
66
code: 'JEREMYBEARIMY'
77
};
88
}

0 commit comments

Comments
 (0)