@soleil-se/app-util 4.1.1 → 4.1.3
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 +11 -0
- package/client/.eslintrc.js +4 -0
- package/client/utils/index.js +7 -1
- package/docs/1.render.md +4 -1
- package/docs/2.svelte.md +26 -0
- package/package.json +2 -2
- package/server/index.js +5 -1
- package/server/svelte/index.js +10 -6
- package/client/.eslintrc +0 -4
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
5
5
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
6
|
|
|
7
|
+
## [4.1.3] - 2021-03-31
|
|
8
|
+
### Fixed
|
|
9
|
+
- Consistent rendering when using `serverServer` and `render` for Svelte.
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
- Docs for Svelte `renderServer` function.
|
|
13
|
+
|
|
14
|
+
## [4.1.2] - 2021-02-24
|
|
15
|
+
### Fixed
|
|
16
|
+
- Escape tags when parsing data to prevent closure of script tags.
|
|
17
|
+
|
|
7
18
|
## [4.1.1] - 2021-02-17
|
|
8
19
|
### Fixed
|
|
9
20
|
- Validation error for `id` on script tags, even though it's allowed.
|
package/client/utils/index.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import getCurrentScript from './getCurrentScript';
|
|
2
2
|
|
|
3
|
+
const unescapeTags = (unsafe) => unsafe
|
|
4
|
+
.replace(/</g, '<')
|
|
5
|
+
.replace(/>/g, '>');
|
|
6
|
+
|
|
3
7
|
/**
|
|
4
8
|
* JSON decode an attribute on the currentScript element.
|
|
5
9
|
* Use if attribute contains a JSON-object.
|
|
@@ -17,5 +21,7 @@ export const getAttribute = (attribute) => {
|
|
|
17
21
|
|
|
18
22
|
export const parseJson = (type) => {
|
|
19
23
|
const id = getAttribute('data-app-id');
|
|
20
|
-
|
|
24
|
+
let json = document.querySelector(`[data-id="${type}_${id}"]`)?.textContent || '{}';
|
|
25
|
+
json = unescapeTags(json);
|
|
26
|
+
return JSON.parse(json);
|
|
21
27
|
};
|
package/docs/1.render.md
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
# Render
|
|
2
|
-
Returns HTML for a script tag with
|
|
2
|
+
Returns HTML for a script tag with the possibility to pass data from the server.
|
|
3
3
|
Framework agnostic.
|
|
4
4
|
|
|
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
|
|
7
|
+
|
|
5
8
|
## index.js
|
|
6
9
|
### render([data], [settings]) ⇒ <code>String</code>
|
|
7
10
|
`@soleil-api/webapp-util/server`
|
package/docs/2.svelte.md
CHANGED
|
@@ -14,6 +14,7 @@ If a client bundle is available the server rendered HTML will be hydrated and no
|
|
|
14
14
|
|
|
15
15
|
| Param | Type | Default | Description |
|
|
16
16
|
| --- | --- | --- | --- |
|
|
17
|
+
| [App] | <code>Svelte</code> | | Svelte app to be rendered. |
|
|
17
18
|
| [props] | <code>Object</code> | <code>{}</code> | Server data that will be available as props and as app data. |
|
|
18
19
|
| [settings] | <code>Object</code> | <code>{}</code> | Settings object. Forwarded to |
|
|
19
20
|
| [settings.async] | <code>Boolean</code> | <code>false</code> | If the app script should be loaded asynchronously. |
|
|
@@ -35,6 +36,31 @@ router.get('/', (req, res) => {
|
|
|
35
36
|
});
|
|
36
37
|
```
|
|
37
38
|
|
|
39
|
+
### `renderServer(App, [props])`
|
|
40
|
+
`@soleil-api/webapp-util/server/svelte`
|
|
41
|
+
|
|
42
|
+
Get a HTML string for rendering a server side Svelte application.
|
|
43
|
+
|
|
44
|
+
**Returns**: <code>String</code> - HTML for rendering a Svelte application.
|
|
45
|
+
|
|
46
|
+
| Param | Type | Default | Description |
|
|
47
|
+
| --- | --- | --- | --- |
|
|
48
|
+
| [App] | <code>Svelte</code> | | Svelte app to be rendered. |
|
|
49
|
+
| [props] | <code>Object</code> | <code>{}</code> | Server data that will be available as props and as app data. |
|
|
50
|
+
|
|
51
|
+
In `app_src/index.js` or `app_src/server/index.js`.
|
|
52
|
+
```javascript
|
|
53
|
+
import router from 'router';
|
|
54
|
+
import { renderServer } from '@soleil-api/webapp-util/server/svelte';
|
|
55
|
+
|
|
56
|
+
import App from './App.svelte';
|
|
57
|
+
|
|
58
|
+
router.get('/', (req, res) => {
|
|
59
|
+
const props = { foo: 'bar' };
|
|
60
|
+
res.send(render(App, data));
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
38
64
|
## main.js
|
|
39
65
|
|
|
40
66
|
### `render(App, [settings])`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soleil-se/app-util",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.3",
|
|
4
4
|
"description": "Utility functions for Webapps.",
|
|
5
5
|
"main": "./common/index.js",
|
|
6
6
|
"author": "Soleil AB",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"optionalDependencies": {
|
|
15
15
|
"vue": "^2.6.11"
|
|
16
16
|
},
|
|
17
|
-
"gitHead": "
|
|
17
|
+
"gitHead": "9fc1fba75e8419058746c7170dcf96ea6c78f4eb",
|
|
18
18
|
"dependencies": {},
|
|
19
19
|
"devDependencies": {}
|
|
20
20
|
}
|
package/server/index.js
CHANGED
|
@@ -12,6 +12,10 @@ const isIE = (req) => {
|
|
|
12
12
|
return /Trident\/|MSIE/.test(userAgent);
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
+
const escapeTags = (unsafe) => unsafe
|
|
16
|
+
.replace(/</g, '<')
|
|
17
|
+
.replace(/>/g, '>');
|
|
18
|
+
|
|
15
19
|
/**
|
|
16
20
|
* Get a HTML string for rendering an application.
|
|
17
21
|
* @param {Object} [data={}] Server data that will be available in the attribute.
|
|
@@ -37,7 +41,7 @@ export function render(data, {
|
|
|
37
41
|
|
|
38
42
|
const mountElement = `<div id="app_mount_${appId}">${html}</div>`;
|
|
39
43
|
const metaScriptTag = `<script data-id="app_meta_${appId}" type="application/json">${JSON.stringify(appMetadata)}</script>`;
|
|
40
|
-
const dataScriptTag = data ? `<script data-id="app_data_${appId}" type="application/json">${JSON.stringify(data)}</script>` : '';
|
|
44
|
+
const dataScriptTag = data ? `<script data-id="app_data_${appId}" type="application/json">${escapeTags(JSON.stringify(data))}</script>` : '';
|
|
41
45
|
|
|
42
46
|
if (isOffline) {
|
|
43
47
|
return `
|
package/server/svelte/index.js
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
/* eslint-disable import/prefer-default-export */
|
|
2
2
|
import appResource from 'appResource';
|
|
3
|
-
import { setAppData } from '../../common';
|
|
3
|
+
import { appId, setAppData } from '../../common';
|
|
4
4
|
import { render as renderClient } from '../index';
|
|
5
5
|
|
|
6
|
+
const initSSR = (App, props) => {
|
|
7
|
+
setAppData(props);
|
|
8
|
+
return App.render(props).html;
|
|
9
|
+
};
|
|
10
|
+
|
|
6
11
|
/**
|
|
7
12
|
* SSR Svelte App.
|
|
8
13
|
* @export
|
|
@@ -11,9 +16,8 @@ import { render as renderClient } from '../index';
|
|
|
11
16
|
* @return {String} - HTML for rendering a Svelte application.
|
|
12
17
|
*/
|
|
13
18
|
export function renderServer(App, props = {}) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return html;
|
|
19
|
+
const html = initSSR(App, props);
|
|
20
|
+
return `<div id="app_mount_${appId}">${html}</div>`;
|
|
17
21
|
}
|
|
18
22
|
|
|
19
23
|
/**
|
|
@@ -25,9 +29,9 @@ export function renderServer(App, props = {}) {
|
|
|
25
29
|
* @return {String} - HTML for rendering a Svelte application.
|
|
26
30
|
*/
|
|
27
31
|
export function render(App, props = {}, settings) {
|
|
28
|
-
const html =
|
|
32
|
+
const html = initSSR(App, props);
|
|
29
33
|
if (appResource.getNode('client/index.js')) {
|
|
30
34
|
return renderClient(props, { ...settings, html });
|
|
31
35
|
}
|
|
32
|
-
return html
|
|
36
|
+
return `<div id="app_mount_${appId}">${html}</div>`;
|
|
33
37
|
}
|
package/client/.eslintrc
DELETED