|
6 | 6 | }
|
7 | 7 | </style>
|
8 | 8 |
|
9 |
| - <h3>Lazy Loading JS Bundles</h3> |
10 |
| - <p> |
11 |
| - Vaadin Router works great with code splitting. If your build tool allows |
12 |
| - you to split your code into various bundles which can then be loaded on |
13 |
| - demand, Vaadin Router can load these bundled lazily just-in-time before |
14 |
| - rendering a route. |
15 |
| - In order to use this feature specify a bundle URL in the <code>bundle |
16 |
| - </code> property of the route object. |
17 |
| - </p> |
18 |
| - <p> |
19 |
| - By default Vaadin Router loads route bundles as regular JavaScript files (async), |
20 |
| - but it supports ES module bundles as well. In order to use a ES module as a route bundle, |
21 |
| - set the <code>bundle</code> property to an object with the following structure: |
22 |
| - <marked-element> |
23 |
| - <script type="text/markdown"> |
24 |
| - ```js |
25 |
| - { |
26 |
| - bundle: { |
27 |
| - module: '/bundle.esm.js', // for browsers supporting ES modules |
28 |
| - nomodule: '/bundle.es5.js' // fallback for older browsers |
29 |
| - } |
30 |
| - } |
31 |
| - ``` |
32 |
| - </script> |
33 |
| - </marked-element> |
34 |
| - <p> |
35 |
| - Note: If the bundle URL does not end with <code>.js</code> nor with <code> |
36 |
| - .mjs</code>, Vaadin Router throws an <code>Error</code> and does not |
37 |
| - attempt to load it. Use a custom route action as shown in the next demo if |
38 |
| - you need to load non-js bundles. |
39 |
| - </p> |
40 |
| - <p> |
41 |
| - This demo shows how to load the <code>user.bundle.js</code> |
42 |
| - script which defines the custom element for the <code>/user/:id</code> route. |
43 |
| - Check the network tab in the browser DevTools to see that the script is |
44 |
| - loaded only after clicking the <code>/user/guest</code> link. |
45 |
| - </p> |
46 |
| - <vaadin-demo-snippet id="vaadin-router-code-splitting-1" iframe-src="iframe.html"> |
47 |
| - <template preserve-content> |
48 |
| - <a href="/">Home</a> |
49 |
| - <a href="/user/guest">User Profile</a> |
50 |
| - <div id="outlet"></div> |
51 |
| - <script> |
52 |
| - // import {Router} from '@vaadin/router'; // for Webpack / Polymer CLI |
53 |
| - // const Router = Vaadin.Router; // for vaadin-router.umd.js |
54 |
| - |
55 |
| - const router = new Router(document.getElementById('outlet')); |
56 |
| - router.setRoutes([ |
57 |
| - {path: '/', component: 'x-home-view'}, |
58 |
| - { |
59 |
| - path: '/user/:id', |
60 |
| - bundle: `${Vaadin.Demo.componentsRoot}/user.bundle.js`, |
61 |
| - component: 'x-user-js-bundle-view' // <-- defined in the bundle |
62 |
| - }, |
63 |
| - ]); |
64 |
| - </script> |
65 |
| - </template> |
66 |
| - </vaadin-demo-snippet> |
67 |
| - |
68 |
| - <h3>Lazy Loading non-JS Bundles, e.g. HTML Imports</h3> |
69 |
| - <p> |
70 |
| - In cases when loading <code>.js</code> and <code>.mjs</code> is not |
71 |
| - enough—most notably, when using HTML imports in Polymer-based |
72 |
| - apps—the lazy loading feature can be implemented with a custom route |
73 |
| - action (for more details see <a href="#vaadin-router-route-actions-demos"> |
74 |
| - Route Actions</a>). |
75 |
| - </p> |
76 |
| - <p> |
77 |
| - This demo shows a way to lazily add an HTML import. The <code>user.bundle.html</code> |
78 |
| - file contains entire Polymer 2 component definition including a template, a class, |
79 |
| - and a script that defines a custom element. |
80 |
| - </p> |
81 |
| - <vaadin-demo-snippet id="vaadin-router-code-splitting-2" iframe-src="iframe.html"> |
82 |
| - <template preserve-content> |
83 |
| - <link rel="import" href="../../polymer/lib/utils/import-href.html"> |
84 |
| - <a href="/">Home</a> |
85 |
| - <a href="/user/admin">User Profile</a> |
86 |
| - <div id="outlet"></div> |
87 |
| - <script> |
88 |
| - // import {Router} from '@vaadin/router'; // for Webpack / Polymer CLI |
89 |
| - // const Router = Vaadin.Router; // for vaadin-router.umd.js |
90 |
| - |
91 |
| - const loadCustomBundle = (context) => { |
92 |
| - // This is one way of loading a bundle lazily (works for Polymer apps). |
93 |
| - // Other apps might use different ways. |
94 |
| - Polymer.importHref( |
95 |
| - `${Vaadin.Demo.componentsRoot}/user.bundle.html`, |
96 |
| - null, |
97 |
| - function(err) { |
98 |
| - throw new Error('bundle not found'); |
99 |
| - }, |
100 |
| - true |
101 |
| - ); |
102 |
| - }; |
103 |
| - |
104 |
| - const router = new Router(document.getElementById('outlet')); |
105 |
| - router.setRoutes([ |
106 |
| - {path: '/', component: 'x-home-view'}, |
107 |
| - {path: '/user/:id', action: loadCustomBundle, component: 'x-user-html-bundle-view'} |
108 |
| - ]); |
109 |
| - </script> |
110 |
| - </template> |
111 |
| - </vaadin-demo-snippet> |
112 |
| - |
113 | 9 | <h3>Using Dynamic Imports</h3>
|
114 | 10 | <p>
|
115 | 11 | Vaadin Router allows you to implement your own loading mechanism for bundles using
|
@@ -198,6 +94,52 @@ <h3>Splitting and Lazy-Loading the Routes Configuration</h3>
|
198 | 94 | </script>
|
199 | 95 | </template>
|
200 | 96 | </vaadin-demo-snippet>
|
| 97 | + |
| 98 | + <h3>Lazy Loading non-JS Bundles, e.g. HTML Imports</h3> |
| 99 | + <p> |
| 100 | + In cases when loading <code>.js</code> and <code>.mjs</code> is not |
| 101 | + enough—most notably, when using HTML imports in Polymer-based |
| 102 | + apps—the lazy loading feature can be implemented with a custom route |
| 103 | + action (for more details see <a href="#vaadin-router-route-actions-demos"> |
| 104 | + Route Actions</a>). |
| 105 | + </p> |
| 106 | + <p> |
| 107 | + This demo shows a way to lazily add an HTML import. The <code>user.bundle.html</code> |
| 108 | + file contains entire Polymer 2 component definition including a template, a class, |
| 109 | + and a script that defines a custom element. |
| 110 | + </p> |
| 111 | + <vaadin-demo-snippet id="vaadin-router-code-splitting-2" iframe-src="iframe.html"> |
| 112 | + <template preserve-content> |
| 113 | + <link rel="import" href="../../polymer/lib/utils/import-href.html"> |
| 114 | + <a href="/">Home</a> |
| 115 | + <a href="/user/admin">User Profile</a> |
| 116 | + <div id="outlet"></div> |
| 117 | + <script> |
| 118 | + // import {Router} from '@vaadin/router'; // for Webpack / Polymer CLI |
| 119 | + // const Router = Vaadin.Router; // for vaadin-router.umd.js |
| 120 | + |
| 121 | + const loadCustomBundle = (context) => { |
| 122 | + // This is one way of loading a bundle lazily (works for Polymer apps). |
| 123 | + // Other apps might use different ways. |
| 124 | + Polymer.importHref( |
| 125 | + `${Vaadin.Demo.componentsRoot}/user.bundle.html`, |
| 126 | + null, |
| 127 | + function(err) { |
| 128 | + throw new Error('bundle not found'); |
| 129 | + }, |
| 130 | + true |
| 131 | + ); |
| 132 | + }; |
| 133 | + |
| 134 | + const router = new Router(document.getElementById('outlet')); |
| 135 | + router.setRoutes([ |
| 136 | + {path: '/', component: 'x-home-view'}, |
| 137 | + {path: '/user/:id', action: loadCustomBundle, component: 'x-user-html-bundle-view'} |
| 138 | + ]); |
| 139 | + </script> |
| 140 | + </template> |
| 141 | + </vaadin-demo-snippet> |
| 142 | + |
201 | 143 | </template>
|
202 | 144 | <script>
|
203 | 145 | class VaadinRouterCodeSplittingDemos extends DemoReadyEventEmitter(ElementDemo(Polymer.Element)) {
|
|
0 commit comments