@soleil-se/app-util 4.1.3 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,107 +1,172 @@
1
1
  # Changelog
2
+
2
3
  All notable changes to this project will be documented in this file.
3
4
 
4
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
7
 
8
+ ## [4.1.4] - 2021-08-20
9
+
10
+ ### Added
11
+
12
+ - `getAppProps` for getting props passed to the app when rendering.
13
+
14
+ ### Changed
15
+
16
+ - `getAppData` deprecated due to confusion, use `getAppProps` instead.
17
+
18
+ ## [4.1.4] - 2021-05-25
19
+
20
+ ### Fixed
21
+
22
+ - Return empty object instead of undefined when app data isn't set.
23
+
24
+ ### Removed
25
+
26
+ - Remove `vue` as optional dependency.
27
+
7
28
  ## [4.1.3] - 2021-03-31
29
+
8
30
  ### Fixed
31
+
9
32
  - Consistent rendering when using `serverServer` and `render` for Svelte.
10
33
 
11
34
  ### Added
35
+
12
36
  - Docs for Svelte `renderServer` function.
13
37
 
14
38
  ## [4.1.2] - 2021-02-24
39
+
15
40
  ### Fixed
41
+
16
42
  - Escape tags when parsing data to prevent closure of script tags.
17
43
 
18
44
  ## [4.1.1] - 2021-02-17
45
+
19
46
  ### Fixed
47
+
20
48
  - Validation error for `id` on script tags, even though it's allowed.
21
49
 
22
50
  ## [4.1.0] - 2021-02-17
51
+
23
52
  ### Changed
53
+
24
54
  - Put app data and metadata in script elements instead of data attributes.
25
55
 
26
56
  ## [4.0.1] - 2021-01-28
57
+
27
58
  ### Fixed
59
+
28
60
  - Crashes when no data is passed to app and then trying to call `getAppData`.
29
61
 
30
62
  ## [4.0.0] - 2021-01-27
31
- A much needed major overhaul of the package.
63
+
64
+ A much needed major overhaul of the package.
32
65
  See [MIGRATION](./MIGRATION.md).
33
66
 
34
67
  ### Changed
68
+
35
69
  - Major refactoring of package.
36
- - Exports for render functions are moved.
70
+ - Exports for render functions are moved.
37
71
  - All documented constants and functions from base import (`@soleil-api/webapp-util`) now works both in a server and client context.
38
72
  - Settings for selector is moved from server to client rendering functions.
39
73
 
40
74
  ### Removed
75
+
41
76
  - `noScript` option in `render` (formerly `renderApp`) has been removed. If a no script message is needed use the `html` option wrapped in `<noscript>`.
42
77
  - `@soleil-api/webapp-util/app-data` has been removed, use [getAppData](./README.md#getAppData) from `@soleil-api/webapp-util`.
43
78
 
44
79
  ## [3.0.3] - 2020-11-03
80
+
45
81
  - Nothing, Lerna wanted to publish...
46
82
 
47
83
  ## [3.0.2] - 2020-10-19
84
+
48
85
  - Nothing, Lerna wanted to publish...
49
86
 
50
87
  ## [3.0.1] - 2020-09-28
88
+
51
89
  ### Added
90
+
52
91
  - Homepage link
53
92
 
54
93
  ## [3.0.0] - 2020-09-28
94
+
55
95
  ### Added
96
+
56
97
  - Svelte support.
57
98
  - New exports:
58
99
  - `uniqueId` - A unique id for the app instance.
59
100
 
60
101
  ## [2.4.1] - 2020-09-01
102
+
61
103
  ### Fixed
104
+
62
105
  - App data with single quotes crashes app in edit mode, use double quotes instead.
63
106
 
64
107
  ## [2.4.0] - 2020-05-26
108
+
65
109
  ### Fixed
110
+
66
111
  - `getViewUri` returns correct URI when in offline mode and in the Addons view.
67
112
 
68
113
  ## [2.3.0] - 2020-05-22
114
+
69
115
  ### Added
70
- - `renderApp` now accepts `req` as a setting to opimize script loading when multiple instances of the app exists. If `req` is present the script will only be loaded once in all browsers except IE11 that needs a unique script src for the currentScript polyfill to work.
116
+
117
+ - `renderApp` now accepts `req` as a setting to opimize script loading when multiple instances of the app exists. If `req` is present the script will only be loaded once in all browsers except IE11 that needs a unique script src for the currentScript polyfill to work.
71
118
 
72
119
  ### Fixed
120
+
73
121
  - `noScript` in `renderApp` was not wrapped in a `<noscript>` tag.
74
122
 
75
123
  ## [2.2.0] - 2020-05-20
124
+
76
125
  ### Added
126
+
77
127
  - `renderTemplate` function is now available inside templates rendered by the function.
128
+
78
129
  ## [2.1.4] - 2020-04-07
130
+
79
131
  ### Fixed
132
+
80
133
  - `getViewUri` had incorrect portlet ID.
134
+
81
135
  ## [2.1.3] - 2020-02-25
136
+
82
137
  ### Fixed
138
+
83
139
  - Add timestamp to script url in edit mode as well.
84
140
 
85
141
  ## [2.1.2] - 2020-02-21
142
+
86
143
  ### Fixed
144
+
87
145
  - Wrapped `renderApp` script in svDocReady in edit mode to be sure it executes when other bundles are loaded.
88
146
 
89
147
  ## [2.1.1] - 2020-02-14
148
+
90
149
  ### Changed
150
+
91
151
  - Updated readme, heading levels and examples.
92
152
 
93
153
  ## [2.1.0] - 2020-02-14
154
+
94
155
  ### Added
156
+
95
157
  - App data import in client, `@soleil-api/webapp-util/app-data`.
96
158
  - `getViewUri` to get the URI that also renders the page.
97
159
  - `isOnline` to see if the app is online.
98
160
 
99
161
  ### Changed
162
+
100
163
  - `vue` is now an optional depedency.
101
164
 
102
165
  ## [2.0.0] - 2020-02-12
166
+
103
167
  ### Changed
168
+
104
169
  - Now using `currentScript` to reference the script element the app is running in to pass data when using `renderApp`.
105
170
  - `renderApp` is no longer using appName.
106
- - `render` is now called directly in `./app_src/client/index.js`.
107
- - `render` and `App` no longer needs to be exported from `./app_src/client/index.js`.
171
+ - `render` is now called directly in `./app_src/client/index.js`.
172
+ - `render` and `App` no longer needs to be exported from `./app_src/client/index.js`.
package/README.md CHANGED
@@ -1,40 +1,28 @@
1
+
1
2
  # `@soleil-se/webapp-util@^4.0.0`
3
+
2
4
  Utility functions for Webapps.
3
5
 
4
- [GitHub](https://github.com/soleilit/server-monorepo/tree/master/packages/webapp-util)
6
+ [GitHub](https://github.com/soleilit/server-monorepo/tree/master/packages/webapp-util)
5
7
  [NPM](https://www.npmjs.com/package/@soleil-se/webapp-util)
6
8
 
7
- <!-- TOC -->
8
-
9
- - [Requirements](#requirements)
10
- - [Install](#install)
11
- - [Migration](#migration)
12
- - [API](#api)
13
- - [Constants](#constants)
14
- - [Functions](#functions)
15
- - [appId : <code>String</code>](#appid--codestringcode)
16
- - [isOffline : <code>Boolean</code>](#isoffline--codebooleancode)
17
- - [isOnline : <code>Boolean</code>](#isonline--codebooleancode)
18
- - [getNamespace([prefix]) ⇒ <code>String</code>](#getnamespaceprefix-⇒-codestringcode)
19
- - [getRouteUri(route) ⇒ <code>String</code>](#getrouteuriroute-⇒-codestringcode)
20
- - [getViewUri(route) ⇒ <code>String</code>](#getviewuriroute-⇒-codestringcode)
21
- - [getResourceUri(resource) ⇒ <code>String</code>](#getresourceuriresource-⇒-codestringcode)
22
- - [getAppData([key]) ⇒ <code>\*</code> \| <code>Object</code>](#getappdatakey-⇒-code\code-\-codeobjectcode)
23
- - [Rendering](#rendering)
24
-
25
- <!-- /TOC -->
9
+
26
10
 
27
11
  ## Requirements
12
+
28
13
  `@soleil-se/sv-gulp-build` 4 or later (Works best with ^4.3.0).
29
14
 
30
15
  ## Install
16
+
31
17
  `yarn add @soleil-se/webapp-util`
32
18
 
33
19
  ## Migration
34
- Migrating from version 2 or 3?
20
+
21
+ Migrating from version 2 or 3?
35
22
  See [MIGRATION](./MIGRATION.md).
36
23
 
37
24
  ## API
25
+
38
26
  All imports from base package are available both on the server and client.
39
27
 
40
28
  ### Constants
@@ -58,22 +46,26 @@ All imports from base package are available both on the server and client.
58
46
  <dd><p>Get a prefixed namespace unique for app.</p>
59
47
  </dd>
60
48
  <dt><a href="#getRouteUri">getRouteUri(route)</a> ⇒ <code>String</code></dt>
61
- <dd><p>Get URI for a route, same as <code>getStandaloneUrl</code> in SiteVision template.</p>
49
+ <dd><p>Get URI for a route.
62
50
  </dd>
63
51
  <dt><a href="#getViewUri">getViewUri(route)</a> ⇒ <code>String</code></dt>
64
- <dd><p>Get URI for a view, same as <code>getUrl</code> in SiteVision template.</p>
52
+ <dd><p>Get URI for a view.
65
53
  </dd>
66
54
  <dt><a href="#getResourceUri">getResourceUri(resource)</a> ⇒ <code>String</code></dt>
67
55
  <dd><p>Get URI for a resource.</p>
68
56
  </dd>
69
- <dt><a href="#getAppData">getAppData([key])</a> ⇒ <code>*</code> | <code>Object</code></dt>
70
- <dd><p>Get appData value or object.</p>
57
+ <dt><a href="#getAppProps">getAppProps([key])</a> ⇒ <code>*</code> | <code>Object</code></dt>
58
+ <dd><p>Get props that are passed to app when rendering.</p>
59
+ </dd>
60
+ <dt><del><a href="#getAppData">getAppData([key])</a> ⇒ <code>*</code> | <code>Object</code></del></dt>
61
+ <dd><p>Get props that are passed to app when rendering.</p>
71
62
  </dd>
72
63
  </dl>
73
64
 
74
65
  <a name="appId"></a>
75
66
 
76
- ### appId : <code>String</code>
67
+ ### appId : `String`
68
+
77
69
  DOM friendly unique identifier for the WebApp.
78
70
 
79
71
  ```js
@@ -84,7 +76,8 @@ console.log(appId); // For example: 12_682d461b1708a9bb1ea13efd
84
76
 
85
77
  <a name="isOffline"></a>
86
78
 
87
- ### isOffline : <code>Boolean</code>
79
+ ### isOffline : `Boolean`
80
+
88
81
  If the WebApp is running in offline mode or not.
89
82
 
90
83
  ```js
@@ -95,7 +88,8 @@ console.log(isOffline); // true or false
95
88
 
96
89
  <a name="isOnline"></a>
97
90
 
98
- ### isOnline : <code>Boolean</code>
91
+ ### isOnline : `Boolean`
92
+
99
93
  If the WebApp is running in online mode or not.
100
94
 
101
95
  ```js
@@ -104,14 +98,15 @@ import { isOnline } from '@soleil-se/webapp-util';
104
98
  console.log(isOnline); // true or false
105
99
  ```
106
100
 
107
- ### getNamespace([prefix]) ⇒ <code>String</code>
101
+ ### getNamespace([prefix]) ⇒ `String`
102
+
108
103
  Get a prefixed namespace unique for app.
109
104
 
110
- **Returns**: <code>String</code> - Prefixed namespace.
105
+ **Returns**: `String` - Prefixed namespace.
111
106
 
112
107
  | Param | Type | Default |
113
108
  | --- | --- | --- |
114
- | [prefix] | <code>string</code> | <code>&quot;&#x27;app&#x27;&quot;</code> |
109
+ | [prefix] | `string` | `&quot;&#x27;app&#x27;&quot;` |
115
110
 
116
111
  ```js
117
112
  import { getNamespace } from '@soleil-se/webapp-util';
@@ -129,14 +124,15 @@ console.log(getNamespace('decoration'));
129
124
 
130
125
  <a name="getRouteUri"></a>
131
126
 
132
- ### getRouteUri(route) ⇒ <code>String</code>
133
- Get URI for a route, same as `getStandaloneUrl` in SiteVision template.
127
+ ### getRouteUri(route) ⇒ `String`
134
128
 
135
- **Returns**: <code>String</code> - URI for route.
129
+ Get URI for a route.
130
+
131
+ **Returns**: `String` - URI for route.
136
132
 
137
133
  | Param | Type | Description |
138
134
  | --- | --- | --- |
139
- | route | <code>String</code> | A route. |
135
+ | route | `String` | A route. |
140
136
 
141
137
  ```js
142
138
  import { getRouteUri } from '@soleil-se/webapp-util';
@@ -147,14 +143,15 @@ console.log(getRouteUri('/items'));
147
143
 
148
144
  <a name="getViewUri"></a>
149
145
 
150
- ### getViewUri(route) ⇒ <code>String</code>
151
- Get URI for a view, same as `getUrl` in SiteVision template.
146
+ ### getViewUri(route) ⇒ `String`
147
+
148
+ Get URI for a view.
152
149
 
153
- **Returns**: <code>String</code> - URI for view.
150
+ **Returns**: `String` - URI for view.
154
151
 
155
152
  | Param | Type | Description |
156
153
  | --- | --- | --- |
157
- | route | <code>String</code> | A route. |
154
+ | route | `String` | A route. |
158
155
 
159
156
  ```js
160
157
  import { getViewUri } from '@soleil-se/webapp-util';
@@ -164,14 +161,16 @@ console.log(getViewUri('/items'));
164
161
  ```
165
162
 
166
163
  <a name="getResourceUri"></a>
167
- ### getResourceUri(resource) ⇒ <code>String</code>
164
+
165
+ ### getResourceUri(resource) ⇒ `String`
166
+
168
167
  Get URI for a resource.
169
168
 
170
- **Returns**: <code>String</code> - URI for a resource.
169
+ **Returns**: `String` - URI for a resource.
171
170
 
172
171
  | Param | Type | Description |
173
172
  | --- | --- | --- |
174
- | resource | <code>String</code> | A resource. |
173
+ | resource | `String` | A resource. |
175
174
 
176
175
  ```js
177
176
  import { getResourceUri } from '@soleil-se/webapp-util';
@@ -180,16 +179,40 @@ console.log(getResourceUri('/image.png'));
180
179
  // URI structure: /webapp-files/<webappname>/<webappversion>/image.png
181
180
  ```
182
181
 
182
+ <a name="getAppProps"></a>
183
+
184
+ ### getAppProps([key]) ⇒ `*` | `Object`
185
+
186
+ Get props that are passed to app when rendering.
187
+
188
+ **Returns**: `*` | `Object` - Value or object.
189
+
190
+ | Param | Type | Description |
191
+ | --- | --- | --- |
192
+ | [key] | `String` | Key for value. |
193
+
194
+ ```js
195
+ import { getAppProps } from '@soleil-se/webapp-util';
196
+
197
+ // Get value with key
198
+ const myValue = getAppProps('myValue');
199
+ // Or with destructuring
200
+ const { myValue } = getAppProps();
201
+ ```
202
+
183
203
  <a name="getAppData"></a>
184
204
 
185
- ### getAppData([key]) ⇒ <code>\*</code> \| <code>Object</code>
186
- Get appData value or object that is passed to app when rendering.
205
+ ### getAppData([key]) ⇒ `*` | `Object` (DEPRECATED)
206
+
207
+ Use [getAppProps](#getAppProps) instead.
187
208
 
188
- **Returns**: <code>\*</code> \| <code>Object</code> - Value or object.
209
+ Get props that are passed to app when rendering.
210
+
211
+ **Returns**: `*` | `Object` - Value or object.
189
212
 
190
213
  | Param | Type | Description |
191
214
  | --- | --- | --- |
192
- | [key] | <code>String</code> | Key for value. |
215
+ | [key] | `String` | Key for value. |
193
216
 
194
217
  ```js
195
218
  import { getAppData } from '@soleil-se/webapp-util';
@@ -202,8 +225,7 @@ const { myValue } = getAppData();
202
225
 
203
226
  ## Rendering
204
227
 
205
- * [Render](./docs/1.render.md)
206
- * [Svelte](./docs/2.svelte.md)
207
- * [Vue](./docs/3.vue.md)
208
- * [Underscore](./docs/4.underscore.md)
209
-
228
+ - [Render](./docs/1.render.md)
229
+ - [Svelte](./docs/2.svelte.md)
230
+ - [Vue](./docs/3.vue.md)
231
+ - [Underscore](./docs/4.underscore.md)
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable import/prefer-default-export */
2
- import { appId, getAppData } from '../../common';
2
+ import { appId, getAppProps } from '../../common';
3
3
 
4
4
  /**
5
5
  * Renders a client side Svelte application.
@@ -16,7 +16,7 @@ export function render(App, {
16
16
  } = {}) {
17
17
  const target = document.querySelector(selector);
18
18
  const hydrate = target.childElementCount > 0;
19
- const props = getAppData();
19
+ const props = getAppProps();
20
20
  return new App({
21
21
  hydrate,
22
22
  target,
@@ -2,7 +2,7 @@
2
2
  /* eslint-disable-next-line import/no-unresolved, import/no-extraneous-dependencies */
3
3
  import Vue from 'vue';
4
4
 
5
- import { appId, getAppData, isOffline } from '../../common';
5
+ import { appId, getAppProps, isOffline } from '../../common';
6
6
 
7
7
  const offlineModeMixin = {
8
8
  mounted() {
@@ -22,9 +22,10 @@ const offlineModeMixin = {
22
22
  * @param {String} [settings.selector=`#app_mount_${appId}`] Query selector for mount element.
23
23
  */
24
24
  export function render(App, { selector = `#app_mount_${appId}` } = {}) {
25
- const options = getAppData();
25
+ const options = getAppProps();
26
26
 
27
27
  Object.assign(App, options, {
28
+ propsData: options,
28
29
  mixins: isOffline ? [offlineModeMixin] : [],
29
30
  });
30
31
 
package/common/index.js CHANGED
@@ -62,7 +62,7 @@ export function getNamespace(prefix = 'app') {
62
62
  }
63
63
 
64
64
  /**
65
- * Get URI for a route, same as `getStandaloneUrl` in SiteVision template.
65
+ * Get URI for a route.
66
66
  * @param {String} route A route.
67
67
  * @returns {String} URI for route.
68
68
  */
@@ -75,14 +75,14 @@ export function getRouteUri(route = '') {
75
75
  .getStandaloneUrl(path !== '' ? `/${path}` : path)
76
76
  .replace(trailingSlashes, '');
77
77
  }
78
- // If SiteVision 7 or older.
78
+ // If Sitevision 7 or older.
79
79
  return getLegacyRouteUri(path);
80
80
  }
81
81
  return `${appMetadata.baseRouteUri}/${path}`;
82
82
  }
83
83
 
84
84
  /**
85
- * Get URI for a view, same as `getUrl` in SiteVision template.
85
+ * Get URI for a view.
86
86
  * @param {String} route A route.
87
87
  * @returns {String} URI for view.
88
88
  */
@@ -92,7 +92,7 @@ export function getViewUri(route = '') {
92
92
  if (router.getUrl) {
93
93
  return router.getUrl(route);
94
94
  }
95
- // If SiteVision 7 or older.
95
+ // If Sitevision 7 or older.
96
96
  return getLegacyViewUri(route, { isOffline });
97
97
  }
98
98
  return appMetadata.baseViewUri + route;
@@ -124,21 +124,30 @@ export function getAppMetadata() {
124
124
  };
125
125
  }
126
126
 
127
- let appData = process.browser ? parseJson('app_data') : undefined;
127
+ let appProps = process.browser ? parseJson('app_props') : {};
128
128
 
129
- export function setAppData(data) {
130
- appData = data;
129
+ export function setAppProps(data) {
130
+ appProps = data;
131
131
  }
132
132
 
133
133
  /**
134
- * Get appData value or object that is passed to app when rendering.
135
- * @export
134
+ * Get props that are passed to app when rendering.
135
+ * @param {String} [key] Key for value.
136
+ * @returns {*|Object} Value or object.
137
+ */
138
+ export function getAppProps(key) {
139
+ if (process.browser && !appProps) {
140
+ setAppProps(parseJson('app_props'));
141
+ }
142
+ return key ? appProps[key] : appProps;
143
+ }
144
+
145
+ /**
146
+ * Get props that are passed to app when rendering.
147
+ * @deprecated Use `getAppProps` instead.
136
148
  * @param {String} [key] - Key for value.
137
- * @return {*|Object} - Value or object.
149
+ * @return {*|Object} Value or object.
138
150
  */
139
151
  export function getAppData(key) {
140
- if (process.browser && !appData) {
141
- setAppData(parseJson('app_data'));
142
- }
143
- return key ? appData[key] : appData;
152
+ return getAppProps(key);
144
153
  }
package/docs/1.render.md CHANGED
@@ -1,12 +1,15 @@
1
1
  # Render
2
- Returns HTML for a script tag with the possibility to pass data from the server.
2
+
3
+ Returns HTML for a script tag with the possibility to pass data from the server.
3
4
  Framework agnostic.
4
5
 
5
- Server data is embedded as JSON in script tags.
6
- https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#embedding_data_in_html
6
+ Server data is embedded as JSON in script tags.
7
+ <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#embedding_data_in_html>
7
8
 
8
9
  ## index.js
9
- ### render([data], [settings]) ⇒ <code>String</code>
10
+
11
+ ### render([props], [settings]) ⇒ <code>String</code>
12
+
10
13
  `@soleil-api/webapp-util/server`
11
14
 
12
15
  Get a HTML string for rendering an application.
@@ -15,40 +18,41 @@ Get a HTML string for rendering an application.
15
18
 
16
19
  | Param | Type | Default | Description |
17
20
  | --- | --- | --- | --- |
18
- | [data] | <code>Object</code> | <code>{}</code> | Server data that will be available in the attribute. `data-app-data` on the script tag. |
21
+ | [props] | <code>Object</code> | <code>{}</code> | Props that will be passed to the app |
19
22
  | [settings] | <code>Object</code> | <code>{}</code> | Settings object. |
20
23
  | [settings.html] | <code>String</code> | <code>&#x27;&#x27;</code> | HTML that will be rendered inside mount element. |
21
24
  | [settings.async] | <code>Boolean</code> | <code>false</code> | If the app script should be loaded asynchronously. |
22
25
  | [settings.defer] | <code>Boolean</code> | <code>true</code> | If the app script should be loaded after DOM is ready. |
23
- | [settings.req] | <code>Object</code> | | The req object from SiteVision, pass this to optimize browser specific script loading if you have multiple instances of the app. |
26
+ | [settings.req] | <code>Object</code> | | The req object from Sitevision, pass this to optimize browser specific script loading if you have multiple instances of the app. |
24
27
 
25
28
  [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/)
26
29
 
27
30
  ### Example
28
31
 
29
32
  ```js
30
- import router from 'router';
33
+ import router from '@Sitevision/api/common/router';
31
34
  import { render } from '@soleil-api/webapp-util/server';
32
35
 
33
36
  router.get('/', (req, res) => {
34
- const data = { foo: 'bar' };
35
- res.send(render(data));
37
+ const props = { foo: 'bar' };
38
+ res.send(render(props));
36
39
  };
37
40
  ```
38
41
 
39
42
  **All settings**
43
+
40
44
  ```js
41
- import router from 'router';
45
+ import router from '@Sitevision/api/common/router';
42
46
  import { render } from '@soleil-api/webapp-util/server';
43
47
 
44
48
  router.get('/', (req, res) => {
45
- const data = { foo: 'bar' };
49
+ const props = { foo: 'bar' };
46
50
  const settings = {
47
51
  html: '<noscript>You can put a noscript message here for example!</noscript>',
48
52
  async: false,
49
53
  defer: true,
50
54
  req,
51
55
  }
52
- res.send(render(data, settings));
56
+ res.send(render(props, settings));
53
57
  };
54
58
  ```
package/docs/2.svelte.md CHANGED
@@ -1,13 +1,16 @@
1
1
  # Svelte
2
- SiteVision supports both server and client side rendering with Svelte.
3
2
 
4
- ## index.js
3
+ Sitevision supports both server and client side rendering with Svelte.
4
+
5
+ ## index.js
6
+
5
7
  For rendering a client side only app use the framework agnostic [client renderer](./1.render.md).
6
8
 
7
9
  ### `render(App, [props], [settings])`
8
- `@soleil-api/webapp-util/server/svelte`
9
10
 
10
- Get a HTML string for rendering a universal Svelte application.
11
+ `@soleil-api/webapp-util/server/svelte`
12
+
13
+ Get a HTML string for rendering a universal Svelte application.
11
14
  If a client bundle is available the server rendered HTML will be hydrated and not completely re-rendered.
12
15
 
13
16
  **Returns**: <code>String</code> - HTML for rendering a Svelte application.
@@ -15,31 +18,33 @@ If a client bundle is available the server rendered HTML will be hydrated and no
15
18
  | Param | Type | Default | Description |
16
19
  | --- | --- | --- | --- |
17
20
  | [App] | <code>Svelte</code> | | Svelte app to be rendered. |
18
- | [props] | <code>Object</code> | <code>{}</code> | Server data that will be available as props and as app data. |
21
+ | [props] | <code>Object</code> | <code>{}</code> | Props that will be passed to the app. |
19
22
  | [settings] | <code>Object</code> | <code>{}</code> | Settings object. Forwarded to |
20
23
  | [settings.async] | <code>Boolean</code> | <code>false</code> | If the app script should be loaded asynchronously. |
21
24
  | [settings.defer] | <code>Boolean</code> | <code>true</code> | If the app script should be loaded after DOM is ready. |
22
- | [settings.req] | <code>Object</code> | | The req object from SiteVision, pass this to optimize browser specific script loading if you have multiple instances of the app. |
25
+ | [settings.req] | <code>Object</code> | | The req object from Sitevision, pass this to optimize browser specific script loading if you have multiple instances of the app. |
23
26
 
24
27
  [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/)
25
28
 
26
29
  In `app_src/index.js` or `app_src/server/index.js`.
30
+
27
31
  ```javascript
28
- import router from 'router';
32
+ import router from '@Sitevision/api/common/router';
29
33
  import { render } from '@soleil-api/webapp-util/server/svelte';
30
34
 
31
35
  import App from './App.svelte';
32
36
 
33
37
  router.get('/', (req, res) => {
34
38
  const props = { foo: 'bar' };
35
- res.send(render(App, data));
39
+ res.send(render(App, props));
36
40
  });
37
41
  ```
38
42
 
39
43
  ### `renderServer(App, [props])`
40
- `@soleil-api/webapp-util/server/svelte`
41
44
 
42
- Get a HTML string for rendering a server side Svelte application.
45
+ `@soleil-api/webapp-util/server/svelte`
46
+
47
+ Get a HTML string for rendering a server side Svelte application.
43
48
 
44
49
  **Returns**: <code>String</code> - HTML for rendering a Svelte application.
45
50
 
@@ -49,21 +54,23 @@ Get a HTML string for rendering a server side Svelte application.
49
54
  | [props] | <code>Object</code> | <code>{}</code> | Server data that will be available as props and as app data. |
50
55
 
51
56
  In `app_src/index.js` or `app_src/server/index.js`.
57
+
52
58
  ```javascript
53
- import router from 'router';
59
+ import router from '@Sitevision/api/common/router';
54
60
  import { renderServer } from '@soleil-api/webapp-util/server/svelte';
55
61
 
56
62
  import App from './App.svelte';
57
63
 
58
64
  router.get('/', (req, res) => {
59
65
  const props = { foo: 'bar' };
60
- res.send(render(App, data));
66
+ res.send(render(App, props));
61
67
  });
62
68
  ```
63
69
 
64
70
  ## main.js
65
71
 
66
72
  ### `render(App, [settings])`
73
+
67
74
  `@soleil-api/webapp-util/client/svelte`
68
75
 
69
76
  Renders a client side Svelte application.
@@ -77,8 +84,8 @@ Renders a client side Svelte application.
77
84
  | [settings.selector] | <code>String</code> | <code>&#x60;#app_mount_${appId}&#x60;</code> | Query selector for mount element. |
78
85
  | [settings.intro] | <code>String</code> | <code>false</code> | If true, will play transitions on initial render, rather than waiting for subsequent state changes. |
79
86
 
80
-
81
87
  In `app_src/main.js` or `app_src/client/index.js`.
88
+
82
89
  ```javascript
83
90
  import { render } from '@soleil-api/webapp-util/client/svelte';
84
91
  import App from './App.svelte';
@@ -87,6 +94,7 @@ render(App);
87
94
  ```
88
95
 
89
96
  Mount the app in another element:
97
+
90
98
  ```javascript
91
99
  import { render } from '@soleil-api/webapp-util/client/svelte';
92
100
  import App from './App.svelte';
package/docs/3.vue.md CHANGED
@@ -1,12 +1,15 @@
1
1
  # Vue
2
- SiteVision supports client side rendering with Vue.
3
2
 
4
- ## index.js
3
+ Sitevision supports client side rendering with Vue.
4
+
5
+ ## index.js
6
+
5
7
  For rendering a client side only app use the framework agnostic [client renderer](./1.render.md).
6
8
 
7
9
  ## main.js
8
10
 
9
11
  ### `render(App, [settings])`
12
+
10
13
  `@soleil-api/webapp-util/client/vue`
11
14
  Renders a client side Vue application.
12
15
 
@@ -17,6 +20,7 @@ Renders a client side Vue application.
17
20
  | [settings.selector] | <code>String</code> | <code>&#x60;#app_mount_${appId}&#x60;</code> | Query selector for mount element. |
18
21
 
19
22
  In `app_src/main.js` or `app_src/client/index.js`.
23
+
20
24
  ```javascript
21
25
  import { render } from '@soleil-api/webapp-util/client/vue';
22
26
  import App from './App.vue';
@@ -25,10 +29,10 @@ render(App);
25
29
  ```
26
30
 
27
31
  Mount the app in another element:
32
+
28
33
  ```javascript
29
34
  import { render } from '@soleil-api/webapp-util/client/vue';
30
35
  import App from './App.vue';
31
36
 
32
37
  render(App, { selector: '#mount_app_here' });
33
38
  ```
34
-
@@ -1,9 +1,11 @@
1
1
  # Underscore
2
+
2
3
  Render a Underscore template.
3
4
 
4
- ## `renderTemplate(template, [values])` ⇒ `String`
5
- Renders a Underscore template and returns a string.
6
- This function is also available inside the template.
5
+ ## `renderTemplate(template, [values])` ⇒ `String`
6
+
7
+ Renders a Underscore template and returns a string.
8
+ This function is also available inside the template.
7
9
 
8
10
  **Returns**: `String` - Rendered template
9
11
 
@@ -13,7 +15,9 @@ This function is also available inside the template.
13
15
  | [values] | `Object` | `{}` | Values. |
14
16
 
15
17
  ### Examples
18
+
16
19
  **Simple example**
20
+
17
21
  ```javascript
18
22
  import { renderTemplate } from '@soleil-api/webapp-util/server';
19
23
 
@@ -21,13 +25,16 @@ const string = renderTemplate('<div><%= foo %></div>', {
21
25
  foo: 'bar',
22
26
  });
23
27
  ```
28
+
24
29
  **Multiple templates**
30
+
25
31
  ```html
26
32
  /* views/item.html */
27
33
  <li>
28
34
  <%- name %>
29
35
  </li>
30
36
  ```
37
+
31
38
  ```html
32
39
  /* views/main.html */
33
40
  <ul>
@@ -36,6 +43,7 @@ const string = renderTemplate('<div><%= foo %></div>', {
36
43
  <% }); %>
37
44
  </ul>
38
45
  ```
46
+
39
47
  ```javascript
40
48
  import { renderTemplate } from '@soleil-api/webapp-util/server';
41
49
  import mainTemplate from './views/main.html';
@@ -44,5 +52,6 @@ import itemTemplate from './views/item.html';
44
52
  const items = [{ name: 'Foo' }, { name: 'Bar' }, { name: 'Baz' }];
45
53
  const string = renderTemplate(mainTemplate, { items, itemTemplate });
46
54
  ```
47
- > **NOTE**
48
- > Remember that the second argument must be an object and that objects properties are accessed directly in any child templates!
55
+
56
+ > **NOTE**
57
+ > Remember that the second argument must be an object and that objects properties are accessed directly in any child templates!
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@soleil-se/app-util",
3
- "version": "4.1.3",
4
- "description": "Utility functions for Webapps.",
3
+ "version": "4.2.0",
4
+ "description": "Utility and rendering functions for WebApps.",
5
5
  "main": "./common/index.js",
6
6
  "author": "Soleil AB",
7
7
  "contributors": [
@@ -11,10 +11,7 @@
11
11
  "license": "UNLICENSED",
12
12
  "private": false,
13
13
  "homepage": "https://docs.soleilit.se/03.packages/@soleil-api&app-util",
14
- "optionalDependencies": {
15
- "vue": "^2.6.11"
16
- },
17
- "gitHead": "9fc1fba75e8419058746c7170dcf96ea6c78f4eb",
14
+ "gitHead": "56b2bb3947dc881fa4dbed17b6cb9cf90a8d0a85",
18
15
  "dependencies": {},
19
16
  "devDependencies": {}
20
17
  }
package/server/index.js CHANGED
@@ -1,7 +1,4 @@
1
1
  import { appImportDate } from 'appInfo';
2
- /* Underscore is provided by SiteVision */
3
- /* eslint-disable-next-line import/no-extraneous-dependencies */
4
- import _ from 'underscore';
5
2
  import {
6
3
  appId, isOffline, getResourceUri, getAppMetadata,
7
4
  } from '../common';
@@ -18,8 +15,7 @@ const escapeTags = (unsafe) => unsafe
18
15
 
19
16
  /**
20
17
  * Get a HTML string for rendering an application.
21
- * @param {Object} [data={}] Server data that will be available in the attribute.
22
- * `data-app-data` on the script tag.
18
+ * @param {Object} [props={}] Props that will be passed to the app.
23
19
  * @param {Object} [settings={}] Settings object.
24
20
  * @param {String} [settings.html=''] HTML that will be rendered inside mount element.
25
21
  * where the app should be mounted.
@@ -27,11 +23,11 @@ const escapeTags = (unsafe) => unsafe
27
23
  * [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/)
28
24
  * @param {Boolean} [settings.defer=true] If the app script should be loaded after DOM is ready.
29
25
  * [Read more about async and defer.](https://flaviocopes.com/javascript-async-defer/)
30
- * @param {Object} [settings.req] The req object from SiteVision, pass this to optimize browser
26
+ * @param {Object} [settings.req] The req object from Sitevision, pass this to optimize browser
31
27
  * specific script loading if you have multiple instances of the app.
32
28
  * @returns {String} HTML for rendering an application.
33
29
  */
34
- export function render(data, {
30
+ export function render(props, {
35
31
  html = '',
36
32
  async = false,
37
33
  defer = true,
@@ -41,13 +37,13 @@ export function render(data, {
41
37
 
42
38
  const mountElement = `<div id="app_mount_${appId}">${html}</div>`;
43
39
  const metaScriptTag = `<script data-id="app_meta_${appId}" type="application/json">${JSON.stringify(appMetadata)}</script>`;
44
- const dataScriptTag = data ? `<script data-id="app_data_${appId}" type="application/json">${escapeTags(JSON.stringify(data))}</script>` : '';
40
+ const propsScriptTag = props ? `<script data-id="app_props_${appId}" type="application/json">${escapeTags(JSON.stringify(props))}</script>` : '';
45
41
 
46
42
  if (isOffline) {
47
43
  return `
48
44
  ${mountElement}
49
45
  ${metaScriptTag}
50
- ${dataScriptTag}
46
+ ${propsScriptTag}
51
47
  <script>
52
48
  window.svDocReady(function() {
53
49
  var targetElement = document.getElementById('app_mount_${appId}');
@@ -61,7 +57,7 @@ ${dataScriptTag}
61
57
  return `
62
58
  ${mountElement}
63
59
  ${metaScriptTag}
64
- ${dataScriptTag}
60
+ ${propsScriptTag}
65
61
  <script
66
62
  src="${getResourceUri('client/index.js')}?${appImportDate}${isIE(req) ? appId : ''}"
67
63
  data-app-id="${appId}"
@@ -77,6 +73,9 @@ ${dataScriptTag}
77
73
  * @returns {String} Rendered template
78
74
  */
79
75
  export function renderTemplate(template, values = {}) {
76
+ // Import undercore dynamically in the rare case this function is still used.
77
+ // eslint-disable-next-line global-require
78
+ const _ = require('undercore');
80
79
  if (typeof template === 'function') {
81
80
  return template({ ...values, renderTemplate });
82
81
  }
@@ -1,10 +1,9 @@
1
- /* eslint-disable import/prefer-default-export */
2
1
  import appResource from 'appResource';
3
- import { appId, setAppData } from '../../common';
2
+ import { appId, setAppProps } from '../../common';
4
3
  import { render as renderClient } from '../index';
5
4
 
6
5
  const initSSR = (App, props) => {
7
- setAppData(props);
6
+ setAppProps(props);
8
7
  return App.render(props).html;
9
8
  };
10
9
 
@@ -12,7 +11,7 @@ const initSSR = (App, props) => {
12
11
  * SSR Svelte App.
13
12
  * @export
14
13
  * @param {SvelteComponent} App - Svelte component that is root of app.
15
- * @param {Object} [props] - Properties available on root component. Also forwarded as appData.
14
+ * @param {Object} [props] - Props available on root component. Also avaliable with getAppProps.
16
15
  * @return {String} - HTML for rendering a Svelte application.
17
16
  */
18
17
  export function renderServer(App, props = {}) {
@@ -24,8 +23,8 @@ export function renderServer(App, props = {}) {
24
23
  * Render a Svelte App.
25
24
  * @export
26
25
  * @param {SvelteComponent} App - Svelte component that is root of app.
27
- * @param {Object} [props] - Properties available on root component. Also forwarded as appData.
28
- * @param {Object} [settings] - Settings forwarded to render function for client app.
26
+ * @param {Object} [props] - Props available on root component. Also avaliable with getAppProps.
27
+ * @param {Object} [settings] - Settings for rendering a client app.
29
28
  * @return {String} - HTML for rendering a Svelte application.
30
29
  */
31
30
  export function render(App, props = {}, settings) {