@soleil-se/app-util 4.1.4 → 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 +10 -0
- package/README.md +73 -51
- package/client/svelte/index.js +2 -2
- package/client/vue/index.js +3 -2
- package/common/index.js +23 -14
- package/docs/1.render.md +16 -12
- package/docs/2.svelte.md +21 -13
- package/docs/3.vue.md +7 -3
- package/docs/4.underscore.md +14 -5
- package/package.json +2 -2
- package/server/index.js +9 -10
- package/server/svelte/index.js +5 -6
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
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
|
+
|
|
8
18
|
## [4.1.4] - 2021-05-25
|
|
9
19
|
|
|
10
20
|
### Fixed
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
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="#
|
|
70
|
-
<dd><p>Get
|
|
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 :
|
|
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 :
|
|
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 :
|
|
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]) ⇒
|
|
101
|
+
### getNamespace([prefix]) ⇒ `String`
|
|
102
|
+
|
|
108
103
|
Get a prefixed namespace unique for app.
|
|
109
104
|
|
|
110
|
-
**Returns**:
|
|
105
|
+
**Returns**: `String` - Prefixed namespace.
|
|
111
106
|
|
|
112
107
|
| Param | Type | Default |
|
|
113
108
|
| --- | --- | --- |
|
|
114
|
-
| [prefix] |
|
|
109
|
+
| [prefix] | `string` | `"'app'"` |
|
|
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) ⇒
|
|
133
|
-
Get URI for a route, same as `getStandaloneUrl` in SiteVision template.
|
|
127
|
+
### getRouteUri(route) ⇒ `String`
|
|
134
128
|
|
|
135
|
-
|
|
129
|
+
Get URI for a route.
|
|
130
|
+
|
|
131
|
+
**Returns**: `String` - URI for route.
|
|
136
132
|
|
|
137
133
|
| Param | Type | Description |
|
|
138
134
|
| --- | --- | --- |
|
|
139
|
-
| 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) ⇒
|
|
151
|
-
|
|
146
|
+
### getViewUri(route) ⇒ `String`
|
|
147
|
+
|
|
148
|
+
Get URI for a view.
|
|
152
149
|
|
|
153
|
-
**Returns**:
|
|
150
|
+
**Returns**: `String` - URI for view.
|
|
154
151
|
|
|
155
152
|
| Param | Type | Description |
|
|
156
153
|
| --- | --- | --- |
|
|
157
|
-
| 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
|
-
|
|
164
|
+
|
|
165
|
+
### getResourceUri(resource) ⇒ `String`
|
|
166
|
+
|
|
168
167
|
Get URI for a resource.
|
|
169
168
|
|
|
170
|
-
**Returns**:
|
|
169
|
+
**Returns**: `String` - URI for a resource.
|
|
171
170
|
|
|
172
171
|
| Param | Type | Description |
|
|
173
172
|
| --- | --- | --- |
|
|
174
|
-
| 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]) ⇒
|
|
186
|
-
|
|
205
|
+
### getAppData([key]) ⇒ `*` | `Object` (DEPRECATED)
|
|
206
|
+
|
|
207
|
+
Use [getAppProps](#getAppProps) instead.
|
|
187
208
|
|
|
188
|
-
|
|
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] |
|
|
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
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
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)
|
package/client/svelte/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable import/prefer-default-export */
|
|
2
|
-
import { appId,
|
|
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 =
|
|
19
|
+
const props = getAppProps();
|
|
20
20
|
return new App({
|
|
21
21
|
hydrate,
|
|
22
22
|
target,
|
package/client/vue/index.js
CHANGED
|
@@ -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,
|
|
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 =
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
127
|
+
let appProps = process.browser ? parseJson('app_props') : {};
|
|
128
128
|
|
|
129
|
-
export function
|
|
130
|
-
|
|
129
|
+
export function setAppProps(data) {
|
|
130
|
+
appProps = data;
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
/**
|
|
134
|
-
* Get
|
|
135
|
-
* @
|
|
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}
|
|
149
|
+
* @return {*|Object} Value or object.
|
|
138
150
|
*/
|
|
139
151
|
export function getAppData(key) {
|
|
140
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
| [
|
|
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>''</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
|
|
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
|
|
35
|
-
res.send(render(
|
|
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
|
|
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(
|
|
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
|
-
|
|
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
|
-
|
|
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> |
|
|
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
|
|
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,
|
|
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
|
-
|
|
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,
|
|
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>`#app_mount_${appId}`</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
|
-
|
|
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>`#app_mount_${appId}`</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
|
-
|
package/docs/4.underscore.md
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
# Underscore
|
|
2
|
+
|
|
2
3
|
Render a Underscore template.
|
|
3
4
|
|
|
4
|
-
## `renderTemplate(template, [values])` ⇒ `String`
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
48
|
-
>
|
|
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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soleil-se/app-util",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"description": "Utility and rendering functions for WebApps.",
|
|
5
5
|
"main": "./common/index.js",
|
|
6
6
|
"author": "Soleil AB",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"license": "UNLICENSED",
|
|
12
12
|
"private": false,
|
|
13
13
|
"homepage": "https://docs.soleilit.se/03.packages/@soleil-api&app-util",
|
|
14
|
-
"gitHead": "
|
|
14
|
+
"gitHead": "56b2bb3947dc881fa4dbed17b6cb9cf90a8d0a85",
|
|
15
15
|
"dependencies": {},
|
|
16
16
|
"devDependencies": {}
|
|
17
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} [
|
|
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
|
|
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(
|
|
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
|
|
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
|
-
${
|
|
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
|
-
${
|
|
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
|
}
|
package/server/svelte/index.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
/* eslint-disable import/prefer-default-export */
|
|
2
1
|
import appResource from 'appResource';
|
|
3
|
-
import { appId,
|
|
2
|
+
import { appId, setAppProps } from '../../common';
|
|
4
3
|
import { render as renderClient } from '../index';
|
|
5
4
|
|
|
6
5
|
const initSSR = (App, props) => {
|
|
7
|
-
|
|
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] -
|
|
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] -
|
|
28
|
-
* @param {Object} [settings] - Settings
|
|
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) {
|