Skip to content

Commit fbeaa30

Browse files
authored
Merge pull request #24 from ThinhNu/master
Translate code-spliting page
2 parents c7f8b46 + a390235 commit fbeaa30

File tree

1 file changed

+71
-70
lines changed

1 file changed

+71
-70
lines changed

content/docs/code-splitting.md

+71-70
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ title: Code-Splitting
44
permalink: docs/code-splitting.html
55
---
66

7-
## Bundling {#bundling}
7+
## Đóng Gói (Bundling) {#bundling}
88

9-
Most React apps will have their files "bundled" using tools like
10-
[Webpack](https://webpack.js.org/) or [Browserify](http://browserify.org/).
11-
Bundling is the process of following imported files and merging them into a
12-
single file: a "bundle". This bundle can then be included on a webpage to load
13-
an entire app at once.
9+
Hầu hết files trong các ứng dụng React sẽ được "đóng gói" bằng cách
10+
sử dụng những công cụ như [Webpack](https://webpack.js.org/) hay [Browserify](http://browserify.org/).
11+
Đóng gói là quá trình xử lý những files đã được import và kết hợp chúng thành một file duy nhất: File đóng gói này
12+
sau đó có thể được trang web tải lên chỉ một lần.
1413

15-
#### Example {#example}
14+
#### Ví Dụ {#example}
1615

1716
**App:**
1817

@@ -40,86 +39,81 @@ function add(a, b) {
4039
console.log(add(16, 26)); // 42
4140
```
4241

43-
> Note:
42+
> Chú ý:
4443
>
45-
> Your bundles will end up looking a lot different than this.
44+
> Bundle của bạn có thể sẽ trông rất khác bên trên.
4645
47-
If you're using [Create React App](https://github.com/facebookincubator/create-react-app), [Next.js](https://github.com/zeit/next.js/), [Gatsby](https://www.gatsbyjs.org/), or a similar tool, you will have a Webpack setup out of the box to bundle your
48-
app.
46+
Nếu bạn đang sử dụng [Create React App](https://github.com/facebookincubator/create-react-app), [Next.js](https://github.com/zeit/next.js/), [Gatsby](https://www.gatsbyjs.org/), hay một công cụ tương tự,
47+
bạn sẽ được thiết lập sẵn webpack để đóng gói ứng dụng của mình.
4948

50-
If you aren't, you'll need to setup bundling yourself. For example, see the
51-
[Installation](https://webpack.js.org/guides/installation/) and
52-
[Getting Started](https://webpack.js.org/guides/getting-started/) guides on the
53-
Webpack docs.
49+
Nếu không, bạn sẽ cần phải tự thiết lập. Ví dụ, tham khảo
50+
[Installation](https://webpack.js.org/guides/installation/)
51+
[Getting Started](https://webpack.js.org/guides/getting-started/)
52+
hướng dẫn ở tài liệu Webpack.
5453

55-
## Code Splitting {#code-splitting}
54+
## Phân chia Code {#code-splitting}
5655

57-
Bundling is great, but as your app grows, your bundle will grow too. Especially
58-
if you are including large third-party libraries. You need to keep an eye on
59-
the code you are including in your bundle so that you don't accidentally make
60-
it so large that your app takes a long time to load.
56+
Đóng gói hẵn rất tuyệt vời, nhưng khi ứng dụng của bạn trở nên lớn hơn,
57+
file đóng gói của bạn cũng sẽ lớn theo. Đặc biệt khi bạn sử dụng third-party library (thư viện bên thứ 3) lớn.
58+
Bạn cần phải cẩn thận với những đoạn code bạn đang include vào bundle của mình, bằng cách đó bạn sẽ không
59+
vô tình làm nó trở nên quá lớn khiến ứng dụng mất nhiều thời gian để tải.
6160

62-
To avoid winding up with a large bundle, it's good to get ahead of the problem
63-
and start "splitting" your bundle.
64-
[Code-Splitting](https://webpack.js.org/guides/code-splitting/) is a feature
65-
supported by bundlers like Webpack and Browserify (via
66-
[factor-bundle](https://github.com/browserify/factor-bundle)) which can create
67-
multiple bundles that can be dynamically loaded at runtime.
61+
Để tránh việc hỏng bundle vì file đóng gói quá lớn. Bạn nên chủ động bắt đầu "phân chia" bundle của mình ngay từ đầu.
62+
[Code-Splitting](https://webpack.js.org/guides/code-splitting/) là một tính năng được hỗ trợ bởi nhiều bundlers như Webpack và
63+
Browserfy (bằng [factor-bundle](https://github.com/browserify/factor-bundle)) có thể tạo ra nhiều bundles để load động tại thời điểm
64+
runtime.
6865

69-
Code-splitting your app can help you "lazy-load" just the things that are
70-
currently needed by the user, which can dramatically improve the performance of
71-
your app. While you haven't reduced the overall amount of code in your app,
72-
you've avoided loading code that the user may never need, and reduced the amount
73-
of code needed during the initial load.
66+
Phân chia code cho ứng dụng giúp "lazy-load" chỉ những phần người dùng đang cần, tăng đáng kể hiệu suất mà không cần phải giảm số lượng
67+
code trong ứng dụng, bạn đã tránh phải tải những đoạn code người dùng có thể sẽ không bao giờ cần đến,
68+
và giảm số lượng code cần tải lên trong lần đầu tiên.
7469

7570
## `import()` {#import}
7671

77-
The best way to introduce code-splitting into your app is through the dynamic
78-
`import()` syntax.
72+
Phương pháp tốt nhất để sử dụng code-splitting trong ứng dụng là thông qua cú pháp `import()` động.
7973

80-
**Before:**
74+
**Trước:**
8175

8276
```js
8377
import { add } from './math';
8478

8579
console.log(add(16, 26));
8680
```
8781

88-
**After:**
82+
**Sau:**
8983

9084
```js
9185
import("./math").then(math => {
9286
console.log(math.add(16, 26));
9387
});
9488
```
9589

96-
> Note:
90+
> Chú ý:
9791
>
98-
> The dynamic `import()` syntax is a ECMAScript (JavaScript)
99-
> [proposal](https://github.com/tc39/proposal-dynamic-import) not currently
100-
> part of the language standard. It is expected to be accepted in the
101-
> near future.
92+
> Cú pháp `import()` động là một đoạn ECMAScript (JavaScript)
93+
> [proposal](https://github.com/tc39/proposal-dynamic-import) hiện tại chưa được xem
94+
> như một phần tiêu chuẩn của ngôn ngữ. Nó mong đợi sẽ được chấp nhận trong tương lai gần.
10295
103-
When Webpack comes across this syntax, it automatically starts code-splitting
104-
your app. If you're using Create React App, this is already configured for you
105-
and you can [start using it](https://facebook.github.io/create-react-app/docs/code-splitting) immediately. It's also supported
106-
out of the box in [Next.js](https://github.com/zeit/next.js/#dynamic-import).
96+
Khi Webpack chạy đến cú pháp này, nó sẽ tự động phân chia code trong ứng dụng của bạn. Nếu bạn sử dụng Create React App,
97+
việc này đã được thiết lập sẵn cho bạn và bạn có thể [bắt đầu sử dụng](https://facebook.github.io/create-react-app/docs/code-splitting)
98+
ngay. Nó cũng được hỗ trợ sẵn trong [Next.js](https://github.com/zeit/next.js/#dynamic-import).
10799

108-
If you're setting up Webpack yourself, you'll probably want to read Webpack's
109-
[guide on code splitting](https://webpack.js.org/guides/code-splitting/). Your Webpack config should look vaguely [like this](https://gist.github.com/gaearon/ca6e803f5c604d37468b0091d9959269).
100+
Nếu bạn đang tự mình cấu hình Webpack, bạn có thể sẽ muốn tham khảo Webpack's
101+
[hướng dẫn phân chia code](https://webpack.js.org/guides/code-splitting/). Cấu hình Webpack của bạn có thể sẽ trông mơ hồ [như thế này](https://gist.github.com/gaearon/ca6e803f5c604d37468b0091d9959269).
110102

111-
When using [Babel](https://babeljs.io/), you'll need to make sure that Babel can
112-
parse the dynamic import syntax but is not transforming it. For that you will need [babel-plugin-syntax-dynamic-import](https://yarnpkg.com/en/package/babel-plugin-syntax-dynamic-import).
103+
Khi sử dụng [Babel](https://babeljs.io/), bạn sẽ cần phải chắc chắn rằng Babel có thể phân giải cú pháp import động nhưng không làm nó
104+
bị biến đổi. Bạn sẽ cần [babel-plugin-syntax-dynamic-import](https://yarnpkg.com/en/package/babel-plugin-syntax-dynamic-import).
113105

114106
## `React.lazy` {#reactlazy}
115107

116-
> Note:
108+
> Chú ý:
117109
>
118-
> `React.lazy` and Suspense is not yet available for server-side rendering. If you want to do code-splitting in a server rendered app, we recommend [Loadable Components](https://github.com/smooth-code/loadable-components). It has a nice [guide for bundle splitting with server-side rendering](https://github.com/smooth-code/loadable-components/blob/master/packages/server/README.md).
110+
> `React.lazy` và Suspense chưa có sẵn cho server-side rendering. Nếu bạn muốn phân chia code ở những ứng dụng render tại server,
111+
chúng tôi xin giới thiệu [Loadable Components](https://github.com/smooth-code/loadable-components). Nó có [hướng dẫn phân chia code với
112+
server-side rendering](https://github.com/smooth-code/loadable-components/blob/master/packages/server/README.md).
119113

120-
The `React.lazy` function lets you render a dynamic import as a regular component.
114+
Chức năng `React.lazy` cho phép bạn render một import động như một component bình thường.
121115

122-
**Before:**
116+
**Trước:**
123117

124118
```js
125119
import OtherComponent from './OtherComponent';
@@ -133,7 +127,7 @@ function MyComponent() {
133127
}
134128
```
135129

136-
**After:**
130+
**Sau:**
137131

138132
```js
139133
const OtherComponent = React.lazy(() => import('./OtherComponent'));
@@ -147,13 +141,16 @@ function MyComponent() {
147141
}
148142
```
149143

150-
This will automatically load the bundle containing the `OtherComponent` when this component gets rendered.
144+
Nó sẽ tự động tải bundle có chứa `OtherComponent` khi component này được gọi đến.
151145

152-
`React.lazy` takes a function that must call a dynamic `import()`. This must return a `Promise` which resolves to a module with a `default` export containing a React component.
146+
`React.lazy` chỉ lấy một function mà nó được gọi `import()` động. Nó phải trả về một `Promise` và phân giải thành một module với một
147+
`default` export có chứa một React component.
153148

154149
### Suspense {#suspense}
155150

156-
If the module containing the `OtherComponent` is not yet loaded by the time `MyComponent` renders, we must show some fallback content while we're waiting for it to load - such as a loading indicator. This is done using the `Suspense` component.
151+
Nếu module có chứa `OtherComponent` mà nó chưa được tải lên tại thời điểm `MyComponent` renders,
152+
chúng ta phải hiển thị một số nội dung fallback trong khi chờ đợi nó hiển thị - ví dụ như một loading indicator.
153+
Việc này có thể thực hiện nhờ vào `Suspense` component.
157154

158155
```js
159156
const OtherComponent = React.lazy(() => import('./OtherComponent'));
@@ -169,7 +166,9 @@ function MyComponent() {
169166
}
170167
```
171168

172-
The `fallback` prop accepts any React elements that you want to render while waiting for the component to load. You can place the `Suspense` component anywhere above the lazy component. You can even wrap multiple lazy components with a single `Suspense` component.
169+
Thuộc tính `fallback` chấp nhận bất kỳ React elements nào bạn muốn render trong khi chờ component được tải lên.
170+
Bạn có thể đặt `Suspense` component bất kỳ nơi nào bên trên lazy component.
171+
Bạn thậm chí có thể bọc nhiều lazy component với duy nhất một `Suspense` component.
173172

174173
```js
175174
const OtherComponent = React.lazy(() => import('./OtherComponent'));
@@ -191,7 +190,9 @@ function MyComponent() {
191190

192191
### Error boundaries {#error-boundaries}
193192

194-
If the other module fails to load (for example, due to network failure), it will trigger an error. You can handle these errors to show a nice user experience and manage recovery with [Error Boundaries](/docs/error-boundaries.html). Once you've created your Error Boundary, you can use it anywhere above your lazy components to display an error state when there's a network error.
193+
Nếu `OtherComponent` không thể tải lên (Ví dụ, lỗi mạng), nó sẽ kích hoạt lỗi.
194+
Bạn có thể điều khiển những lỗi đó để hiển thị một trải nghiệm người dùng tốt hơn và quản lý phục hồi với [Error Boundaries](/docs/error-boundaries.html). Một khi bạn đã tạo Error Boundary, bạn có thể sử dụng nó bất kỳ nơi nào bên trên lazy components của bạn
195+
để hiển thị thông báo lỗi khi có sự cố về mạng.
195196

196197
```js
197198
import MyErrorBoundary from './MyErrorBoundary';
@@ -212,19 +213,19 @@ const MyComponent = () => (
212213
);
213214
```
214215

215-
## Route-based code splitting {#route-based-code-splitting}
216+
## Phân chia code dựa vào định tuyến(Route-based) {#route-based-code-splitting}
216217

217-
Deciding where in your app to introduce code splitting can be a bit tricky. You
218-
want to make sure you choose places that will split bundles evenly, but won't
219-
disrupt the user experience.
218+
Việc quyết định nơi nào cần phân chia code trong ứng dụng của bạn có thể sẽ gặp một chút khó khăn.
219+
Bạn muốn chắc chắn những nơi bạn chọn sẽ đều nhau, nhưng không phá vỡ
220+
trải nghiệm người dùng.
220221

221-
A good place to start is with routes. Most people on the web are used to
222-
page transitions taking some amount of time to load. You also tend to be
223-
re-rendering the entire page at once so your users are unlikely to be
224-
interacting with other elements on the page at the same time.
222+
Một nơi tốt để bắt đầu là với routes. Hầu hết mọi người trên web
223+
đã quen với việc chuyển trang sẽ mất một khoảng thời gian nhất định.
224+
Bạn cũng có xu hướng render lại cả trang cùng một lần để ngăn người dùng
225+
không tương tác với những elements khác trong trang cùng một lúc.
225226

226-
Here's an example of how to setup route-based code splitting into your app using
227-
libraries like [React Router](https://reacttraining.com/react-router/) with `React.lazy`.
227+
Đây là một ví dụ hướng dẫn cách cài đặt ứng dụng của bạn phân chia code dựa trên route
228+
bằng cách sử dụng những thư viện như [React Router](https://reacttraining.com/react-router/) with `React.lazy`.
228229

229230
```js
230231
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
@@ -245,9 +246,9 @@ const App = () => (
245246
);
246247
```
247248

248-
## Named Exports {#named-exports}
249+
## Named Exports (Đặt tên Export) {#named-exports}
249250

250-
`React.lazy` currently only supports default exports. If the module you want to import uses named exports, you can create an intermediate module that reexports it as the default. This ensures that treeshaking keeps working and that you don't pull in unused components.
251+
`React.lazy` hiện tại chỉ hỗ trợ default export. Nếu module bạn muốn import sử dụng named export, bạn có thể tạo một module trung gian và sau đó export dưới dạng export default. Điều này chắc đảm bảo rằng treeshaking vẫn hoạt động và bạn không kéo những component chưa được sử dụng.
251252

252253
```js
253254
// ManyComponents.js

0 commit comments

Comments
 (0)