Skip to content

code-splitting #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 63 additions & 78 deletions content/docs/code-splitting.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ title: Code-Splitting
permalink: docs/code-splitting.html
---

## Bundling {#bundling}
## Paketleme {#bundling}

Most React apps will have their files "bundled" using tools like
[Webpack](https://webpack.js.org/) or [Browserify](http://browserify.org/).
Bundling is the process of following imported files and merging them into a
single file: a "bundle". This bundle can then be included on a webpage to load
an entire app at once.
Çoğu React uygulaması, dosyalarını [Webpack](https://webpack.js.org/) veya [Browserify](http://browserify.org/) gibi araçlarla "paketler." Paketleme, içe aktarılan dosyaları işleyip tek bir dosyaya, "paket" haline getirme işlemidir. Daha sonra bu paket, uygulamanın tamamını tek seferde yüklemek için kullanılabilir.

#### Example {#example}
#### Örnek {#example}

**App:**
**Uygulama:**

```js
// app.js
Expand All @@ -30,7 +26,7 @@ export function add(a, b) {
}
```

**Bundle:**
**Paket:**

```js
function add(a, b) {
Expand All @@ -40,86 +36,80 @@ function add(a, b) {
console.log(add(16, 26)); // 42
```

> Note:
> Not:
>
> Your bundles will end up looking a lot different than this.
> Paketleriniz bundan çok daha farklı gözükecektir.

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
app.
Eğer [Create React App](https://github.com/facebookincubator/create-react-app),
[Next.js](https://github.com/zeit/next.js/), [Gatsby](https://www.gatsbyjs.org/)
ya da benzeri bir araç kullanıyorsanız, uygulamanızı paketleyen bir Webpack
kurulumuna sahip olursunuz.

If you aren't, you'll need to setup bundling yourself. For example, see the
[Installation](https://webpack.js.org/guides/installation/) and
[Getting Started](https://webpack.js.org/guides/getting-started/) guides on the
Webpack docs.
Eğer bu araçlardan birini kullanmıyorsanız, paketleyiciyi kendiniz kurmanız gerekir.
Örnek için, Webpack dokümantasyonundan [Kurulum](https://webpack.js.org/guides/installation/)
ve [Başlangıç](https://webpack.js.org/guides/getting-started/) alanlarına göz atınız.

## Code Splitting {#code-splitting}
## Kod Bölümleme {#code-splitting}

Bundling is great, but as your app grows, your bundle will grow too. Especially
if you are including large third-party libraries. You need to keep an eye on
the code you are including in your bundle so that you don't accidentally make
it so large that your app takes a long time to load.
Paketleme güzeldir ama uygulamanız büyüdükçe paketiniz de büyür. Özellikle
büyük üçüncü parti kütüphaneleri dahil ediyorsanız. Paketinizin boyutunun, uygulamanızın yüklenişini
geciktirecek kadar büyük olmaması için paketinize dahil ettiğiniz kodlara
göz kulak olmanız gerekir.

To avoid winding up with a large bundle, it's good to get ahead of the problem
and start "splitting" your bundle.
[Code-Splitting](https://webpack.js.org/guides/code-splitting/) is a feature
supported by bundlers like Webpack and Browserify (via
[factor-bundle](https://github.com/browserify/factor-bundle)) which can create
multiple bundles that can be dynamically loaded at runtime.
Büyük paket boyutlarından kurtulmak için problemin üzerine gitmek ve paketinizi "bölümlemeye" başlamak iyi bir yöntemdir. [Kod Bölümleme](https://webpack.js.org/guides/code-splitting/), Webpack ve Browserify ([factor-bundle](https://github.com/browserify/factor-bundle) ile) gibi paketleyicilerin desteklediği, işleyiş süresince dinamik olarak yüklenen birden çok paket yaratmaya yarayan özelliktir.

Code-splitting your app can help you "lazy-load" just the things that are
currently needed by the user, which can dramatically improve the performance of
your app. While you haven't reduced the overall amount of code in your app,
you've avoided loading code that the user may never need, and reduced the amount
of code needed during the initial load.
Uygulamanıza kod bölümlemesi yapmak, kullanıcının anlık olarak ihtiyaç duyduğu şeylerin
"lazy yüklenmesine" yardımcı olarak uygulama performansını önemli ölçüde
arttırabilir. Uygulamanızdaki toplam kod miktarını azaltmamış olsanız da kullanıcının
hiçbir zaman ihtiyaç duymayacağı kodu yüklemekten kaçınmış ve ilk yükleme sırasında
ihtiyaç duyulan kodu azaltmış olursunuz.

## `import()` {#import}

The best way to introduce code-splitting into your app is through the dynamic
`import()` syntax.
Uygulamanıza kod bölümlemeyi getirmenin en iyi yolu dinamik `import()` sözdiziminden geçer.

**Before:**
**Önce:**

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

console.log(add(16, 26));
```

**After:**
**Sonra:**

```js
import("./math").then(math => {
console.log(math.add(16, 26));
});
```

> Note:
> Not:
>
> The dynamic `import()` syntax is a ECMAScript (JavaScript)
> [proposal](https://github.com/tc39/proposal-dynamic-import) not currently
> part of the language standard. It is expected to be accepted in the
> near future.
> Dinamik `import()` sözdizimi ECMAScript (JavaScript) [önerisi](https://github.com/tc39/proposal-dynamic-import)
> henüz dil standartlarının bir parçası değildir. Yakın gelecekte kabul edilmesi beklenmektedir.

When Webpack comes across this syntax, it automatically starts code-splitting
your app. If you're using Create React App, this is already configured for you
and you can [start using it](https://facebook.github.io/create-react-app/docs/code-splitting) immediately. It's also supported
out of the box in [Next.js](https://github.com/zeit/next.js/#dynamic-import).
Webpack bu sözdizimine denk geldiğinde, uygulamanızda otomatik olarak kod bölümlemeye başlar. Eğer Create React App kullanıyorsanız,
bu ayar sizin için halihazırda ayarlanmıştır ve [kullanmaya](https://facebook.github.io/create-react-app/docs/code-splitting) hemen
başlayabilirsiniz. Aynı zamanda [Next.js](https://github.com/zeit/next.js/#dynamic-import)'de de desteklenmektedir.

If you're setting up Webpack yourself, you'll probably want to read Webpack's
[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).
Eğer Webpack ayarlarını kendiniz yapıyorsanız, Webpack'in [kod bölümleme rehberini](https://webpack.js.org/guides/code-splitting/)
okumayı tercih edebilirsiniz. Webpack ayarınız hayal meyal [buna benzeyecektir.](https://gist.github.com/gaearon/ca6e803f5c604d37468b0091d9959269)

When using [Babel](https://babeljs.io/), you'll need to make sure that Babel can
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).
[Babel](https://babeljs.io/) kullanıyorken, Babel'ın dinamik import sözdizimini çözümleyebildiğinden
fakat dönüştürmediğinden emin olmanız gerekmekte. Bunun için [babel-plugin-syntax-dynamic-import](https://yarnpkg.com/en/package/babel-plugin-syntax-dynamic-import)'a ihtiyacınız var.

## `React.lazy` {#reactlazy}

> Note:
> Not:
>
> `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).
> `React.lazy` ve Suspense henüz server-side rendering için kullanılabilir değildir. Eğer server taraflı görüntülenen uygulamalar için
> kod bölümleme yapmak isterseniz, [Loadable Components](https://github.com/smooth-code/loadable-components)'ı tavsiye ederiz. Çok iyi bir
> [server-side rendering için paket bölümleme rehberi](https://github.com/smooth-code/loadable-components/blob/master/packages/server/README.md) var.

The `React.lazy` function lets you render a dynamic import as a regular component.
`React.lazy` fonksiyonu, dinamik import'u normal bir bileşen gibi render etmeye yarar.

**Before:**
**Önce:**

```js
import OtherComponent from './OtherComponent';
Expand All @@ -133,7 +123,7 @@ function MyComponent() {
}
```

**After:**
**Sonra:**

```js
const OtherComponent = React.lazy(() => import('./OtherComponent'));
Expand All @@ -147,29 +137,30 @@ function MyComponent() {
}
```

This will automatically load the bundle containing the `OtherComponent` when this component gets rendered.
Bu kod, bileşen render edildiğinde `OtherComponent`'ı içeren paketi otomatik olarak yükler.

`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.
`React.lazy`, dinamik `import()`'u çağıran bir fonksiyon alır. `default` ile dışarı aktarılan bir React bileşenini içeren modülü çözümleyen
`Promise` return etmelidir.

### Suspense {#suspense}

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.
`MyComponent` render edildiğinde `OtherComponent`'ı içeren modül yüklenmediyse, yüklenmesini beklerken geçirdiğimiz süre içerisinde yükleme göstergesi gibi bir yedek içerik göstermeliyiz. Bu, `Suspense` bileşeniyle yapılır.


```js
const OtherComponent = React.lazy(() => import('./OtherComponent'));

function MyComponent() {
return (
<div>
<Suspense fallback={<div>Loading...</div>}>
<Suspense fallback={<div>Yükleniyor...</div>}>
<OtherComponent />
</Suspense>
</div>
);
}
```

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.
`fallback` prop'u, bileşenin yüklenmesini beklerken göstermek istediğiniz herhangi bir React elemanını kabul eder. `Suspense` bileşenini, lazy bileşeninin üstünde herhangi bir yere yerleştirebilirsiniz. Birden fazla lazy bileşenini tek bir `Suspense` bileşeni içerisine bile alabilirsiniz.

```js
const OtherComponent = React.lazy(() => import('./OtherComponent'));
Expand All @@ -178,7 +169,7 @@ const AnotherComponent = React.lazy(() => import('./AnotherComponent'));
function MyComponent() {
return (
<div>
<Suspense fallback={<div>Loading...</div>}>
<Suspense fallback={<div>Yükleniyor...</div>}>
<section>
<OtherComponent />
<AnotherComponent />
Expand All @@ -189,9 +180,9 @@ function MyComponent() {
}
```

### Error boundaries {#error-boundaries}
### Hata Sınırları {#error-boundaries}

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.
Eğer diğer modül bir nedenden dolayı yüklenmezse (örneğin, ağ sorunu) hata fırlatacaktır. Güzel bir kullanıcı deneyimi sunmak ve kurtarmayı yönetmek için bu hataları [Hata Sınırları](/docs/error-boundaries.html) ile işleyebilirsiniz. Hata Sınırı oluşturduktan sonra, ağ sorunu olduğunda hata göstermek için Hata Sınırını lazy bileşenlerinizin üstünde herhangi bir yerde kullanabilirsiniz.

```js
import MyErrorBoundary from './MyErrorBoundary';
Expand All @@ -201,7 +192,7 @@ const AnotherComponent = React.lazy(() => import('./AnotherComponent'));
const MyComponent = () => (
<div>
<MyErrorBoundary>
<Suspense fallback={<div>Loading...</div>}>
<Suspense fallback={<div>Yükleniyor...</div>}>
<section>
<OtherComponent />
<AnotherComponent />
Expand All @@ -212,19 +203,13 @@ const MyComponent = () => (
);
```

## Route-based code splitting {#route-based-code-splitting}
## Rota bazlı kod bölümleme {#route-based-code-splitting}

Deciding where in your app to introduce code splitting can be a bit tricky. You
want to make sure you choose places that will split bundles evenly, but won't
disrupt the user experience.
Uygulamanızda nereye kod bölümleme yapacağınıza karar vermek biraz zor olabilir. Paketlerinizi eşit parçalara ayıracak ama kullanıcı deneyimini de engellemeyecek yerler seçtiğinize emin olmalısınız.

A good place to start is with routes. Most people on the web are used to
page transitions taking some amount of time to load. You also tend to be
re-rendering the entire page at once so your users are unlikely to be
interacting with other elements on the page at the same time.
Rotalar, başlamak için güzel yerlerdir. Webteki çoğu insan, yüklenmesi biraz zaman alan sayfa geçişlerine alışıktır. Aynı zamanda tüm sayfayı tek seferde yeniden render etme eğiliminiz vardır ki kullanıcınız, aynı anda sayfanın başka bir elemanıyla etkileşime girmesin.

Here's an example of how to setup route-based code splitting into your app using
libraries like [React Router](https://reacttraining.com/react-router/) with `React.lazy`.
İşte [React Router](https://reacttraining.com/react-router/) gibi kütüphaneler kullanan uygulamalarda rota bazlı kod bölümlemenin `React.lazy` ile nasıl kurulabileceğine dair bir örnek.

```js
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
Expand All @@ -235,7 +220,7 @@ const About = lazy(() => import('./routes/About'));

const App = () => (
<Router>
<Suspense fallback={<div>Loading...</div>}>
<Suspense fallback={<div>Yükleniyor...</div>}>
<Switch>
<Route exact path="/" component={Home}/>
<Route path="/about" component={About}/>
Expand All @@ -245,9 +230,9 @@ const App = () => (
);
```

## Named Exports {#named-exports}
## İsimlendirilmiş Dışa Aktarımlar {#named-exports}

`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.
`React.lazy` şu an için sadece default dışa aktarımları desteklemektedir. İçe aktarmak istediğiniz modül, isimlendirilmiş dışa aktarım kullanıyorsa; onu varsayılan olarak tekrar dışa aktaran aracı bir modül yaratabilirsiniz. Bu, ağaçlanmanın çalışmaya devam etmesini ve kullanılmayan bileşenleri çekmemenizi sağlar.

```js
// ManyComponents.js
Expand Down