Skip to content

src/content/guides/progressive-web-application.md 翻译 #441

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 136 commits into from
Jan 14, 2018
Merged
Changes from all commits
Commits
Show all changes
136 commits
Select commit Hold shift + click to select a range
f2a258a
update /content/loaders & /content/plugins
dear-lizhihua Sep 3, 2017
a5725bb
Merge remote-tracking branch 'upstream/translation' into translation
dear-lizhihua Sep 3, 2017
248be26
update /content/loaders & /content/plugins
dear-lizhihua Sep 19, 2017
36c53b8
Merge remote-tracking branch 'upstream/translation' into translation
dear-lizhihua Sep 19, 2017
25a3d6d
Merge branch 'master' into cn
dear-lizhihua Oct 10, 2017
14bff9f
update /content/loaders & /content/plugins
dear-lizhihua Oct 10, 2017
debfddd
Merge remote-tracking branch 'upstream/cn' into cn
dear-lizhihua Oct 10, 2017
57fb4d4
update contributors
dear-lizhihua Oct 10, 2017
99bf48d
Merge branch 'master' into translation
dear-lizhihua Oct 10, 2017
7fcd796
Merge remote-tracking branch 'upstream/translation' into translation
dear-lizhihua Oct 11, 2017
78e3cee
update /content/loaders & /content/plugins
dear-lizhihua Oct 15, 2017
8a1b2c9
Merge branch 'master' into translation
dear-lizhihua Oct 15, 2017
34575cd
Merge remote-tracking branch 'upstream/translation' into translation
dear-lizhihua Oct 15, 2017
3251e8a
Merge remote-tracking branch 'upstream/cn' into cn
dear-lizhihua Oct 15, 2017
7396c0d
Merge branch 'master' into cn
dear-lizhihua Oct 15, 2017
c0dc1be
fix LinkDropdown
dear-lizhihua Oct 15, 2017
4809e4a
Merge branch 'translation' into cn
dear-lizhihua Oct 15, 2017
178eeeb
修复 npm 命令错误导致编译不成功的问题
dear-lizhihua Oct 17, 2017
8ec33ad
update /content/loaders & /content/plugins
dear-lizhihua Oct 17, 2017
cc7b5e1
Merge remote-tracking branch 'upstream/translation' into translation
dear-lizhihua Oct 17, 2017
93ebe40
Merge remote-tracking branch 'upstream/cn' into cn
dear-lizhihua Oct 29, 2017
1ca293a
update /content/loaders & /content/plugins
dear-lizhihua Oct 30, 2017
a132154
Merge branch 'master' into translation
dear-lizhihua Oct 30, 2017
9fbf758
Merge remote-tracking branch 'upstream/translation' into translation
dear-lizhihua Oct 30, 2017
a40704a
update /content/loaders & /content/plugins
dear-lizhihua Nov 1, 2017
53d47d1
Merge remote-tracking branch 'upstream/translation' into translation
dear-lizhihua Nov 1, 2017
68c4bdd
Merge remote-tracking branch 'upstream/cn' into cn
dear-lizhihua Nov 1, 2017
d622470
docs(plugins): fix typo in module-concatenation-plugin.md (#1683)
mattdsteele Nov 1, 2017
52eb2a4
docs(concepts): simplify the introduction (#1673)
TheDutchCoder Nov 1, 2017
6e8d65d
docs(plugins): add “scope hoisting” intro in module-concatenation-plu…
iamakulov Nov 3, 2017
66443e6
docs(api): fix some method signatures in loaders.md (#1685)
aparajita Nov 3, 2017
b13613e
update /content/loaders & /content/plugins
Nov 4, 2017
0e407de
Merge remote-tracking branch 'upstream/translation' into translation
Nov 4, 2017
d6295cc
Merge remote-tracking branch 'upstream/master'
Nov 4, 2017
955b221
docs(guides): consistent quoute use in typescript.md (#1687)
ulrikstrid Nov 5, 2017
ac739ee
docs(api/guides): document new --concatenate-modules flag (#1686)
iamakulov Nov 5, 2017
6d30cac
docs(guides): fix issues with examples in shimming.md (#1680)
svyandun Nov 5, 2017
ccac872
docs(guides): add middleware tip to the hmr guide
skipjack Nov 5, 2017
5dcd8ac
Merge remote-tracking branch 'upstream/cn' into cn
dear-lizhihua Nov 5, 2017
0f539cb
Merge remote-tracking branch 'upstream/master'
dear-lizhihua Nov 5, 2017
c71abf9
Merge remote-tracking branch 'upstream/cn' into cn
dear-lizhihua Nov 5, 2017
c9cdb7e
Revert "A new --concatenate-modules flag" (#1692)
skipjack Nov 5, 2017
ebd64c9
update master
dear-lizhihua Nov 6, 2017
796d087
Merge remote-tracking branch 'upstream/master'
dear-lizhihua Nov 6, 2017
4a2422b
Merge branch 'master' into cn
dear-lizhihua Nov 6, 2017
fe130ff
update master
dear-lizhihua Nov 6, 2017
19fc4f6
update /content/loaders & /content/plugins
dear-lizhihua Nov 7, 2017
b9d59a4
Merge remote-tracking branch 'upstream/translation' into translation
dear-lizhihua Nov 7, 2017
38fc0d4
Merge branch 'translation' into cn
dear-lizhihua Nov 7, 2017
34dd7b4
docs(concepts): fix grammar in output.md (#1694)
reergymerej Nov 10, 2017
29899d8
docs(contribute): update writing-a-loader (#1691)
ThaNarie Nov 10, 2017
5a7cbe7
docs(plugins): add external example in SourceMapDevToolPlugin (#1676)
hkrutzer Nov 10, 2017
4760409
docs(config): update dev-server open option (#1693)
applemate Nov 11, 2017
63574ba
fix bugs
dear-lizhihua Nov 12, 2017
192c38e
update /content/loaders & /content/plugins
dear-lizhihua Nov 12, 2017
1511650
docs(api): improve formatting and grammar in loaders.md
skipjack Nov 11, 2017
ab6f188
docs(api): clarify fourth parameter of `this.callback` in loaders.md
skipjack Nov 11, 2017
e6c94a4
docs(api): populate missing link in loaders.md
skipjack Nov 12, 2017
4b80c8b
docs(plugins): correct example in html-webpack-plugin (#1698)
Jocs Nov 13, 2017
b24ea5e
docs(guides): update an example in production.md (#1696)
ogonkov Nov 13, 2017
0fb8a35
fix(markdown): fix overflowing inline code (#1701)
kamleshchandnani Nov 15, 2017
251f704
docs(concepts): update concepts wording (#1702)
TheLarkInn Nov 15, 2017
4f2f269
Merge remote-tracking branch 'upstream/cn' into cn
dear-lizhihua Nov 18, 2017
c5142de
Merge remote-tracking branch 'upstream/master'
dear-lizhihua Nov 18, 2017
5a59bb7
update /content/loaders & /content/plugins
dear-lizhihua Nov 18, 2017
8597638
Merge remote-tracking branch 'upstream/translation' into translation
dear-lizhihua Nov 18, 2017
4d2e2b8
docs(config): fix dead link to webpack-dev-server example (#1704)
loilo Nov 19, 2017
549d14c
docs(concepts): use fragment links in usage instructions (#1705)
CentroDL Nov 19, 2017
9f473f5
docs(guides): add windows usage tip in getting-started (#1671)
Vanguard90 Nov 23, 2017
b8e8365
doc(guides): fix grammatical error in build-performance (#1709)
davesidious Nov 23, 2017
067cb0d
docs(guides): correct two small typos
mbsrc Nov 21, 2017
e57a11b
docs(api): remove inadvertent double verb (#1714)
MajorBreakfast Nov 23, 2017
2efe399
docs(contribute): fix grammar in writing-a-plugin (#1715)
MajorBreakfast Nov 23, 2017
05714b5
docs(config): add semicolon for consistency (#1716)
connorholyday Nov 26, 2017
93e4b4d
docs(contributing): add note about debian OS (#1721)
sukrosono Dec 2, 2017
4eefcee
docs(guides): add output example to shimming doc (#1720)
agudulin Dec 2, 2017
afe298d
docs(plugins): use `.includes` over `.indexOf` (#1719)
mattvagni Dec 2, 2017
955ff3f
docs(guides): use `npx` in getting-started (#1708)
MajorBreakfast Dec 2, 2017
efcce14
Merge remote-tracking branch 'upstream/cn' into cn
dear-lizhihua Dec 3, 2017
0f36909
Merge remote-tracking branch 'upstream/master'
dear-lizhihua Dec 3, 2017
27991dc
update /src/content/loaders & /src/content/plugins
dear-lizhihua Dec 3, 2017
3ff5dfe
Merge remote-tracking branch 'upstream/translation' into translation
dear-lizhihua Dec 3, 2017
1e898cb
Merge remote-tracking branch 'upstream/cn' into cn
dear-lizhihua Dec 3, 2017
46e6781
Merge branch 'translation' into cn
dear-lizhihua Dec 3, 2017
4fe9b6f
Merge remote-tracking branch 'upstream/cn' into cn
dear-lizhihua Dec 4, 2017
b5f821d
Merge branch 'master' into cn
dear-lizhihua Dec 4, 2017
8a3c989
docs(contribute): link compiler docs in writing-a-plugin (#1700)
chrmod Dec 5, 2017
968c0d1
docs(guides): fix `source-map` discrepancy in production.md (#1711)
mbsrc Dec 5, 2017
e911daf
docs(guides): rename 'runtime' to 'manifest' in caching.md (#1713)
mbsrc Dec 5, 2017
3d3ab97
docs(guides): update output to webpack 3.9.1 (#1724)
plug-n-play Dec 5, 2017
a4bdaa2
docs(plugins): update the source-map-devtool-plugin (#1707)
EugeneHlushko Dec 5, 2017
5ff8058
docs(contribute): correct an example in the writers-guide (#1727)
lhoff Dec 5, 2017
4d82b1c
docs(config): improve formatting and add note about library entry poi…
skipjack Dec 6, 2017
f0b140a
docs(guides): mention complex entry point configuration in author-lib…
skipjack Dec 6, 2017
099d928
docs(config): improve formatting and clarify the string usage in exte…
skipjack Dec 6, 2017
d6dee86
docs(config): fix minor proselint error in externals
skipjack Dec 6, 2017
c0db521
docs(api): clarify the `pitch` method and how it can be used in loade…
skipjack Dec 6, 2017
9e77d93
docs(contribute): fix `import` statement in writing-a-loader.md (#1732)
sanchitnevgi Dec 8, 2017
2c47976
docs(config): update devtool production recommendations
neonick Dec 6, 2017
9b8653f
docs(api): remove obsolete cli flag (#1733)
ooflorent Dec 9, 2017
8b5fd79
docs(concepts): add warning about incompatible `import` statements
skipjack Dec 9, 2017
4902981
docs(config): fix typescript setup in configuration-languages (#1734)
youta1119 Dec 11, 2017
093722e
docs: use full `npm install` over `npm i` (#1740)
Chocobo1 Dec 14, 2017
1c17320
docs(guides): fix diff display for webpack.config.js entry object
JGJP Dec 14, 2017
4405fee
docs: fix typos
tbroadley Dec 16, 2017
df39d5f
docs: add tbroadley to contributor lists
tbroadley Dec 16, 2017
7018c23
docs(contribute): correct schema example in `writing-a-loader.md` (#1…
warmrobot Dec 18, 2017
f0ffd1f
docs(guides): add pwa guide (#1737)
johnnyreilly Dec 18, 2017
41615e4
docs(guides): highlight css splitting in production
skipjack Dec 18, 2017
1b5cf43
docs(plugins): use `.includes` over `.indexOf`
baldurh Dec 18, 2017
41a5096
docs(config): omit invalid `detailed` option in stats.md (#1757)
Dec 20, 2017
7078bd7
fix(mobile): correctly sort pages in mobile sidebar (#1759)
pierreneter Dec 20, 2017
a5566a1
chore(vote): port voting app, update deps, and simplify config (#1717)
skipjack Dec 23, 2017
679ed0a
Merge remote-tracking branch 'upstream/cn' into cn
dear-lizhihua Dec 23, 2017
e8b2e06
Merge remote-tracking branch 'upstream/master'
dear-lizhihua Dec 23, 2017
0d6cb2e
docs(readme): reformat at 80 characters and include browserstack link
skipjack Dec 22, 2017
aceaf6e
refactor(notification): simplify notification message and bump the ve…
skipjack Dec 26, 2017
fd4548f
fix(splash): fix visualization artifacts (#1762)
pierreneter Dec 27, 2017
1ec37d9
chore: pick up vote fix and update yarn lock file
skipjack Dec 27, 2017
6e06115
chore(vote): pick up another vote patch
skipjack Dec 28, 2017
f375e60
Merge remote-tracking branch 'upstream/cn' into cn
dear-lizhihua Dec 28, 2017
eb02652
Update index.md (#423)
DanielXH Dec 28, 2017
cd39b03
Update .travis.yml
dear-lizhihua Dec 29, 2017
9a14935
docs: fix formatting and grammar (#1765)
yangshun Jan 7, 2018
232bf00
docs(concepts): change loader order from chonological to reverse (#1767)
Jan 7, 2018
e5a36ae
Merge remote-tracking branch 'upstream/master'
dear-lizhihua Jan 13, 2018
17e1fc8
Merge branch 'master' into cn
dear-lizhihua Jan 13, 2018
c46b22a
update UPDATE.md
dear-lizhihua Jan 13, 2018
7d79c30
https://github.com/webpack-china/webpack.js.org/pull/422
dear-lizhihua Jan 13, 2018
48d38a0
Merge remote-tracking branch 'upstream/cn' into cn
dear-lizhihua Jan 13, 2018
8685372
https://github.com/webpack-china/webpack.js.org/issues/40
dear-lizhihua Jan 13, 2018
7e71b5d
Merge remote-tracking branch 'upstream/cn' into cn
dear-lizhihua Jan 13, 2018
8fb61a3
您 -> 你 & 统一为:本指南继续沿用[xxx](/guides/xxx)中的代码示例
dear-lizhihua Jan 13, 2018
be132a2
Merge remote-tracking branch 'upstream/cn' into cn
dear-lizhihua Jan 13, 2018
67101fb
src/content/guides/progressive-web-application.md 翻译
dear-lizhihua Jan 14, 2018
4c82ee9
Merge remote-tracking branch 'upstream/cn' into cn
dear-lizhihua Jan 14, 2018
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
52 changes: 28 additions & 24 deletions src/content/guides/progressive-web-application.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ contributors:

T> 本指南继续沿用[管理输出](/guides/output-management)中的代码示例。

Progressive Web Applications (or PWAs) are web apps that deliver an experience similar to native applications. There are many things that can contribute to that. Of these, the most significant is the ability for an app to be able to function when __offline__. This is achieved through the use of a web technology called [Service Workers](https://developers.google.com/web/fundamentals/primers/service-workers/).
渐进式网络应用程序(Progressive Web Application - PWA),是一种可以提供类似于原生应用程序(native app)体验的网络应用程序(web app)。PWA 可以用来做很多事。其中最重要的是,在__离线(offline)__时应用程序能够继续运行功能。这是通过使用名为 [Service Workers](https://developers.google.com/web/fundamentals/primers/service-workers/) 的网络技术来实现的。

This section will focus on adding an offline experience to our app. We'll achieve this using a Google project called [Workbox](https://github.com/GoogleChrome/workbox) which provides tools that help make offline support for web apps easier to setup.
本章将重点介绍,如何为我们的应用程序添加离线体验。我们将使用名为 [Workbox](https://github.com/GoogleChrome/workbox) 的 Google 项目来实现此目的,该项目提供的工具可帮助我们更轻松地配置 web app 的离线支持。


## We Don't Work Offline Now
## 现在我们并没有离线环境下运行过

So far, we've been viewing the output by going directly to the local file system. Typically though, a real user accesses a web app over a network; their browser talking to a __server__ which will serve up the required assets (e.g. `.html`, `.js`, and `.css` files).
到目前为止,我们一直是直接查看本地文件系统的输出结果。通常情况下,真正的用户是通过网络访问网络应用程序;用户的浏览器会与一个提供所需资源(例如,`.html`, `.js` `.css` 文件)的__服务器__通讯。

So let's test what the current experience is like using a simple server. Let's use the [http-server](https://www.npmjs.com/package/http-server) package: `npm install http-server --save-dev`. We'll also amend the `scripts` section of our `package.json` to add in a `start` script:
那么让我们来使用一个简易服务器,搭建出我们所需的离线体验。我们将使用 [http-server](https://www.npmjs.com/package/http-server) package 包:`npm install http-server --save-dev`。还要修改 `package.json` 的 `scripts` 部分,来添加一个 `start` 脚本:

__package.json__

Expand All @@ -32,27 +32,27 @@ __package.json__
}
```

If you haven't previously done so, run the command `npm run build` to build your project. Then run the command `npm start`. This should produce the following output:
如果你之前没有操作过,请运行命令 `npm run build` 来构建你的项目。然后运行命令 `npm start`。这应该输出以下:

``` bash
> http-server dist

Starting up http-server, serving dist
Available on:
启动 http-server,服务目录是 dist
可以访问:
http://xx.x.x.x:8080
http://127.0.0.1:8080
http://xxx.xxx.x.x:8080
Hit CTRL-C to stop the server
按下 CTRL-C 停止服务
```

If you open your browser to `http://localhost:8080` (i.e. `http://127.0.0.1`) you should see your webpack application being served up from the `dist` directory. If you stop the server and refresh, the webpack application is no longer available.
如果你打开浏览器访问 `http://localhost:8080` ( `http://127.0.0.1`),你应该会看到在 `dist` 目录创建出服务,并可以访问 webpack 应用程序。如果停止服务器然后刷新,则 webpack 应用程序不再可访问。

This is what we aim to change. Once we reach the end of this module we should be able to stop the server, hit refresh and still see our application.
这就是我们最终要改变的现状。在本章结束时,我们应该要实现的是,停止服务器然后刷新,仍然可以查看应用程序正常运行。


## Adding Workbox
## 添加 Workbox

Let's add the Workbox webpack plugin and adjust the `webpack.config.js` file:
添加 workbox-webpack-plugin 插件,并调整 `webpack.config.js` 文件:

``` bash
npm install workbox-webpack-plugin --save-dev
Expand All @@ -79,8 +79,8 @@ __webpack.config.js__
- })
+ }),
+ new WorkboxPlugin({
+ // these options encourage the ServiceWorkers to get in there fast
+ // and not allow any straggling "old" SWs to hang around
+ // 这些选项帮助 ServiceWorkers 快速启用
+ // 不允许遗留任何“旧的” ServiceWorkers
+ clientsClaim: true,
+ skipWaiting: true
+ })
Expand All @@ -92,7 +92,7 @@ __webpack.config.js__
};
```

With that in place, let's see what happens when we do an `npm run build`:
有了 Workbox,我们再看下执行 `npm run build` 时会发生什么:

``` bash
clean-webpack-plugin: /mnt/c/Source/webpack-follow-along/dist has been removed.
Expand All @@ -117,14 +117,14 @@ Child html-webpack-plugin for "index.html":
+ 2 hidden modules
```

As you can see, we now have 2 extra files being generated; `sw.js` and the more verbose `precache-manifest.b5ca1c555e832d6fbf9462efd29d27eb.js`. `sw.js` is the Service Worker file and `precache-manifest.b5ca1c555e832d6fbf9462efd29d27eb.js` is a file that `sw.js` requires so it can run. Your own generated files will likely be different; but you should have an `sw.js` file there.
现在你可以看到,生成了 2 个额外的文件:`sw.js` 和体积很大的 `precache-manifest.b5ca1c555e832d6fbf9462efd29d27eb.js``sw.js` Service Worker 文件,`precache-manifest.b5ca1c555e832d6fbf9462efd29d27eb.js` `sw.js` 引用的文件,所以它也可以运行。可能在你本地生成的文件会有所不同;但是你那里应该会有一个 `sw.js` 文件。

So we're now at the happy point of having produced a Service Worker. What's next?
所以,值得高兴的是,我们现在已经创建出 Service Worker 的高兴点。接下来该做什么?


## Registering Our Service Worker
## 注册我们的 Service Worker

Let's allow our Service Worker to come out and play by registering it. We'll do that by adding the registration code below:
让我们将注册 Service Worker,使其出场并开始表演。通过添加以下注册代码来完成此操作:

__index.js__

Expand All @@ -143,15 +143,19 @@ __index.js__
+ }
```

Once more `npm run build` to build a version of the app including the registration code. Then serve it with `npm start`. Navigate to `http://localhost:8080` and take a look at the console. Somewhere in there you should see:
再次运行 `npm build build` 来构建包含注册代码版本的应用程序。然后用 `npm start` 启动服务。访问 `http://localhost:8080` 并查看 console 控制台。在那里你应该看到:

``` bash
SW registered
```

Now to test it. Stop your server and refresh your page. If your browser supports Service Workers then you should still be looking at your application. However, it has been served up by your Service Worker and __not__ by the server.
现在来进行测试。停止服务器并刷新页面。如果浏览器能够支持 Service Worker,你应该可以看到你的应用程序还在正常运行。然而,服务器已经__停止__了服务,此刻是 Service Worker 在提供服务。


## Conclusion
## 结论

You have built an offline app using the Workbox project. You've started the journey of turning your web app into a PWA. You may now want to think about taking things further. A good resource to help you with that can be found [here](https://developers.google.com/web/progressive-web-apps/).
你已经使用 Workbox 项目构建了一个离线应用程序。开始进入将 web app 改造为 PWA 的旅程。你现在可能想要考虑下一步做什么。在这里的[谷歌文档](https://developers.google.com/web/progressive-web-apps/)中可以找到一些不错的资源。

***

> 原文:https://webpack.js.org/guides/progressive-web-application/