├── .dockerignore ├── .eslintignore ├── .eslintrc ├── .gitignore ├── CONTRIBUTING.md ├── Dockerfile ├── LICENSE ├── README.md ├── app.js ├── app.yaml ├── gulp-tasks ├── bump.js ├── clean.js ├── copy.js ├── html.js ├── images.js ├── nodemon.js ├── scripts.js ├── service-worker.js ├── styles.js └── watch.js ├── gulpfile.js ├── ifixit-pwa ├── README.md ├── gulpfile.js ├── package-lock.json ├── package.json └── src │ ├── routes.jsx │ ├── static │ └── service-worker.js │ └── views │ └── index.handlebars ├── package.json ├── server ├── controllers │ ├── api-controller.js │ ├── server-controller.js │ └── static-page-controller.js ├── models │ └── path-config.js └── views │ ├── index.handlebars │ ├── layouts │ ├── app-shell.handlebars │ └── default.handlebars │ ├── partials │ ├── async-css.handlebars │ ├── close-page.handlebars │ └── open-page.handlebars │ ├── url-1.handlebars │ └── url-2.handlebars └── src ├── favicon.ico ├── images ├── apple-touch-icon.png ├── chrome-splashscreen-icon-384x384.png ├── chrome-touch-icon-192x192.png ├── ic_add_24px.svg ├── ic_info_outline_24px.svg ├── ic_menu_24px.svg ├── icon-128x128.png ├── ms-touch-icon-144x144-precomposed.png └── side-nav-bg@2x.jpg ├── manifest.json ├── old-sw.es6.js ├── scripts ├── controller │ ├── ApplicationController.js │ ├── Controller.js │ ├── PageController.js │ └── StaticPageController.js ├── core.es6.js ├── libs │ ├── RouterSingleton.js │ └── ToasterSingleton.js ├── static-page.es6.js └── view │ └── NavDrawerView.js ├── styles ├── core.scss └── core │ ├── _card.scss │ ├── _colors.scss │ ├── _core.scss │ ├── _dialog.scss │ ├── _header.scss │ ├── _loader.scss │ ├── _main.scss │ ├── _side-nav.scss │ ├── _toast.scss │ └── _z-index.scss └── third_party └── serviceworker-cache-polyfill.es5.js /.dockerignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .dockerignore 3 | Dockerfile 4 | .git 5 | .hg 6 | .svn -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | src/third_party 2 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { "modules": true }, 3 | "rules": { 4 | "array-bracket-spacing": 2, 5 | "block-spacing": [2, "never"], 6 | "brace-style": [2, "1tbs", { "allowSingleLine": false }], 7 | "camelcase": [2, {"properties": "always"}], 8 | "curly": 2, 9 | "default-case": 2, 10 | "dot-notation": 2, 11 | "eqeqeq": 2, 12 | "indent": [ 13 | 2, 14 | 2 15 | ], 16 | "key-spacing": [2, {"beforeColon": false, "afterColon": true}], 17 | "max-len": [2, 80, 2], 18 | "new-cap": 2, 19 | "no-console": 0, 20 | "no-else-return": 2, 21 | "no-eval": 2, 22 | "no-multi-spaces": 2, 23 | "no-multiple-empty-lines": [2, {"max": 2}], 24 | "no-shadow": 2, 25 | "no-trailing-spaces": 2, 26 | "no-unused-expressions": 2, 27 | "object-curly-spacing": [2, "never"], 28 | "padded-blocks": [2, "never"], 29 | "quotes": [ 30 | 2, 31 | "single" 32 | ], 33 | "semi": [ 34 | 2, 35 | "always" 36 | ], 37 | "space-after-keywords": 2, 38 | "space-before-blocks": 2, 39 | "space-before-function-paren": [2, "never"], 40 | "spaced-comment": 2, 41 | "valid-typeof": 2, 42 | "no-unused-vars": [2, {"args": "none"}], 43 | "no-empty-class": 0, 44 | "no-extra-strict": 0, 45 | "no-reserved-keys": 0, 46 | "no-space-before-semi": 0, 47 | "no-wrap-func": 0 48 | }, 49 | "env": { 50 | "es6": true, 51 | "browser": true, 52 | "node": true 53 | }, 54 | "extends": "eslint:recommended", 55 | "globals": { 56 | "importScripts": true 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | *.pyc 4 | dist 5 | tests 6 | npm-debug.log 7 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to become a contributor and submit your own code 2 | 3 | ## Contributor License Agreements 4 | 5 | We'd love to accept your sample apps and patches! Before we can take them, we have to jump a couple of legal hurdles. 6 | 7 | Please fill out either the individual or corporate Contributor License Agreement (CLA). 8 | * If you are an individual writing original source code and you're sure you own the intellectual property, then you'll need to sign an [individual CLA](https://developers.google.com/open-source/cla/individual). 9 | * If you work for a company that wants to allow you to contribute your work, then you'll need to sign a [corporate CLA](https://developers.google.com/open-source/cla/corporate). 10 | 11 | Follow either of the two links above to access the appropriate CLA and instructions for how to sign and return it. Once we receive it, we'll be able to 12 | accept your pull requests. 13 | 14 | ## Contributing A Patch 15 | 16 | 1. Submit an issue describing your proposed change to the repo in question. 17 | 1. The repo owner will respond to your issue promptly. 18 | 1. If your proposed change is accepted, and you haven't already done so, sign a Contributor License Agreement (see details above). 19 | 1. Fork the desired repo, develop and test your code changes. 20 | 1. Ensure that your code adheres to the existing style in the sample to which you are contributing. 21 | 1. Submit a pull request. 22 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Dockerfile extending the generic Node image with application files for a 2 | # single application. 3 | FROM beta.gcr.io/google_appengine/nodejs 4 | # Check to see if the the version included in the base runtime satisfies \ 5 | # >=4.1.0, if not then do an npm install of the latest available \ 6 | # version that satisfies it. \ 7 | RUN npm install https://storage.googleapis.com/gae_node_packages/semver.tar.gz && \ 8 | (node -e 'var semver = require("semver"); \ 9 | if (!semver.satisfies(process.version, ">=4.1.0")) \ 10 | process.exit(1);' || \ 11 | (version=$(curl -L https://storage.googleapis.com/gae_node_packages/node_versions | \ 12 | node -e ' \ 13 | var semver = require("semver"); \ 14 | var http = require("http"); \ 15 | var spec = process.argv[1]; \ 16 | var latest = ""; \ 17 | var versions = ""; \ 18 | var selected_version; \ 19 | \ 20 | function verifyBinary(version) { \ 21 | var options = { \ 22 | "host": "storage.googleapis.com", \ 23 | "method": "HEAD", \ 24 | "path": "/gae_node_packages/node-" + version + \ 25 | "-linux-x64.tar.gz" \ 26 | }; \ 27 | var req = http.request(options, function (res) { \ 28 | if (res.statusCode == 404) { \ 29 | console.error("Binaries for Node satisfying version " + \ 30 | version + " are not available."); \ 31 | process.exit(1); \ 32 | } \ 33 | }); \ 34 | req.end(); \ 35 | } \ 36 | function satisfies(version) { \ 37 | if (semver.satisfies(version, spec)) { \ 38 | process.stdout.write(version); \ 39 | verifyBinary(version); \ 40 | return true; \ 41 | } \ 42 | } \ 43 | process.stdin.on("data", function(data) { \ 44 | versions += data; \ 45 | }); \ 46 | process.stdin.on("end", function() { \ 47 | versions = \ 48 | versions.split("\n").sort().reverse(); \ 49 | if (!versions.some(satisfies)) { \ 50 | console.error("No version of Node found satisfying: " + \ 51 | spec); \ 52 | process.exit(1); \ 53 | } \ 54 | });' \ 55 | ">=4.1.0") && \ 56 | rm -rf /nodejs/* && \ 57 | (curl https://storage.googleapis.com/gae_node_packages/node-$version-linux-x64.tar.gz | \ 58 | tar xzf - -C /nodejs --strip-components=1 \ 59 | ) \ 60 | ) \ 61 | ) 62 | COPY . /app/ 63 | # You have to specify "--unsafe-perm" with npm install 64 | # when running as root. Failing to do this can cause 65 | # install to appear to succeed even if a preinstall 66 | # script fails, and may have other adverse consequences 67 | # as well. 68 | RUN npm --unsafe-perm install 69 | CMD npm start 70 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2014 Google Inc. 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Application Shell Architecture 2 | 3 | A modern web application architecture leveraging [Service Worker](http://www.html5rocks.com/en/tutorials/service-worker/introduction/) to offline cache your application 'shell' and populate the content using JS. This means you can get pixels on the screen without the network, even if the content eventually comes from the network - a great performance win. In browsers without SW, we gracefully degrade to server-side rendering (e.g iOS). [Demo](https://app-shell.appspot.com/). 4 | 5 | Full details of the architecture can be found in [Instant Loading Web Apps With An Application Shell Architecture](https://medium.com/@addyosmani/instant-loading-web-apps-with-an-application-shell-architecture-7c0c2f10c73#.a4d09g3j4) and [Instant Loading with Service Workers](https://www.youtube.com/watch?v=jCKZDTtUA2A&feature=youtu.be). 6 | 7 | ## Goals 8 | 9 | * Time to first paint is extremely fast 10 | * Content is rendered. App shell can be a placeholder. 11 | * User can scroll, but doesn’t necessarily need to be able to navigate or deeply interact. 12 | * First pageload < 1000ms 13 | * App shell is progressively enhanced in. 14 | * User can now navigate within the app. 15 | * Second pageload 16 | * Shell is loaded from SW cache 17 | * Content loads off the network 18 | 19 | ## Installation 20 | 21 | Install dependencies using npm: 22 | 23 | ```sh 24 | $ npm install -g gulp nodemon && npm install 25 | ``` 26 | 27 | ## Usage 28 | 29 | ### Production Build 30 | 31 | ```sh 32 | $ gulp 33 | ``` 34 | 35 | ### Development Build with Watch 36 | 37 | ```sh 38 | $ gulp dev 39 | ``` 40 | 41 | ### Serve/watch 42 | 43 | Once you've got a production build or development build of gulp done, start the 44 | server with: 45 | 46 | ```sh 47 | $ nodemon app.js 48 | ``` 49 | 50 | Alternatively, you can just run `npm run monitor`. The application shell should now be available on port `8080`. 51 | 52 | ### Deployment 53 | 54 | We've deployed the project to Node.js on [Google Cloud](https://cloud.google.com/nodejs/). To do the same, follow the steps in their Node.js deployment [getting started](https://cloud.google.com/nodejs/getting-started/hello-world) guide and after running `npm install` run `gcloud preview app deploy app.yaml --promote`. If everything works correctly, you should have the project deployed to your custom AppSpot endpoint. 55 | 56 | ## Notes 57 | 58 | ## Tips for your application shell 59 | 60 | In a [Progressive Web App](https://infrequently.org/2015/06/progressive-apps-escaping-tabs-without-losing-our-soul/), everything necessary to load the the simplest "shell" of your UI consists of HTML, CSS and JavaScript. Keep this shell as lean as possible. Some of it will come from your application’s index file (inline DOM, styles) and the rest may be loaded from external scripts and stylesheets. Together, they are all you need to display a simple, static app. It’s important to keep the shell of your webapp lean to ensure that some inline static structural content can be displayed as soon as the webapp is opened, regardless of the network being available or not. 61 | 62 | A static webapp that always displays the same content may not be what your users expect - it may well be quite dynamic. This means the app may need to fetch data specific to the user’s current needs so this data can come from the network / a server-side API but we logically separate this work for our app from the application shell. When it comes to offline support, structuring your app so that there's a clear distinction between the page shell and the dynamic or state-specific resources will come in very handy. 63 | 64 | ## Gotchas 65 | 66 | There are no hard and fast rules with this architecture, but there are a few gotchas you should be aware of. 67 | 68 | * Requests for application content may be delayed by various processes such loading of the app shell, loading of JavaScript or fetch requests. Jake Archibald hacked around this by initiating the data request in his Wikipedia offline web app as he [served the shell](https://github.com/jakearchibald/offline-wikipedia/blob/master/public/js/sw/index.js#L59). 69 | 70 | * In the application shell architecture, downloading and adding content can interfere with progressive rendering. This can be an issue for larger JavaScript bundles or longer pieces of content on slow connections. It might even cause performance issues when reading content from the disk. Where possible *include* meaningful page content with the initial download rather than making a separate request for it. In the Wikipedia application, Jake was loading third party content and had to work around this, which is why he used the [Streams API](https://github.com/jakearchibald/offline-wikipedia/blob/master/public/js/page/views/article.js#L86). We strongly recommend reducing the number of requests made for your page content if at all possible. 71 | 72 | ## FAQs 73 | 74 | * Why is there a noscript tag for CSS files? 75 | 76 | There is a great deal of content surrounding how to optimize for the 77 | [critical render path](https://developers.google.com/web/fundamentals/performance/critical-rendering-path/?hl=en). 78 | This boils down to, prioritize CSS for the visible viewport on first load 79 | by inlining those styles and then asynchronously load in additional styles. 80 | At the moment the web doesn't have any way to asynchronously load extra CSS 81 | files. The only way you can do it is to use JavaScript to add the CSS files 82 | after the page has loaded / started to render. 83 | 84 | The reason we have a `