Utility functions for WebApps.
Install
Section titled “Install”yarn add @soleil-se/app-util@^3.0.0
Migration 1.x.x to 2.x.x
Section titled “Migration 1.x.x to 2.x.x”Now using currentScript
to reference the script element the app is running in to pass data.
Remove appName from the renderApp
call in ./app_src/server/index.js
.
Remove export of App
and render
and replace with a call to render(App)
in ./app_src/client/index.js
.
See example.
import { renderApp, getRouteUri, getViewUri, getResourceUri, renderTemplate, isOffline, isOnline } from '@soleil-se/app-util';
renderApp([config], [settings])
⇨ String
Section titled “renderApp([config], [settings]) ⇨ String”Get HTML string for rendering a client side application.
Can be used together with @soleil-se/app-util/render-vue
, @soleil-se/app-util/svelte-client
or a custom render function in the client code.
Returns: String
- HTML for rendering an application.
Param | Type | Default | Description |
---|---|---|---|
[config] | Object | {} | Server config or data that will be available in the attribute data-app on currentScript. In Vue it will also be available in the $options object. |
[settings] | Object | {} | Settings object. |
[settings.noScript] | String | ” | HTML that will be rendered when JavaScript is not available. |
[settings.selector] | String | `[data-portlet-id=”${portletId}”]` | Query selector for where the app should be mounted. |
[settings.async] | Boolean | false | If the app script should be loaded asynchronously. Read more about async and defer. |
[settings.defer] | Boolean | true | If the app script should be loaded after DOM is ready. Read more about async and defer. |
[settings.req] | Object | The req object from SiteVision, pass this to optimize browser specific script loading if you have multiple instances of the app. |
Example
Section titled “Example”In app_src/server/index.js
.
router.get('/', (req, res) => { const data = { foo: 'bar' }; // Most common usage. res.send(renderApp(data));
// With all settings. res.send(renderApp(data, { noScript: 'You need JS for this!', selector: '#mount_me_here', async: false, defer: true, req, }));});
In app_src/client/index.js
or app_src/main.js
.
import render from '@soleil-se/app-util/render-vue';import App from './App.vue';
render(App);
getRouteUri(route)
⇨ String
Section titled “getRouteUri(route) ⇨ String”Get URI for a route, same as getStandaloneUrl
in SiteVision template.
https://developer.sitevision.se/docs/webapps/template#h-Methods
Returns: String
- URI for route.
Param | Type | Description |
---|---|---|
route | String | A route. |
Example
Section titled “Example”const routeUri = getRouteUri('/my-route');
getViewUri(route)
⇨ String
Section titled “getViewUri(route) ⇨ String”Get URI for a view, same as getUrl
in SiteVision template.
https://developer.sitevision.se/docs/webapps/template#h-Methods
Returns: String
- URI for view.
Param | Type | Description |
---|---|---|
route | String | A route. |
Example
Section titled “Example”const viewUri = getViewUri('/my-route');
getResourceUri(resource)
⇨ String
Section titled “getResourceUri(resource) ⇨ String”Get URI for a resource.
Returns: String
- URI for a resource.
Param | Type | Description |
---|---|---|
resource | String | A resource. |
Example
Section titled “Example”const resourceUri = getResourceUri('file/in/resource.png');
renderTemplate(template, [values])
⇨ String
Section titled “renderTemplate(template, [values]) ⇨ String”Renders a Underscore template and returns a string. This function is also available inside the template.
Returns: String
- Rendered template
Param | Type | Default | Description |
---|---|---|---|
template | String | Underscore template. | |
[values] | Object | {} | Values. |
Example
Section titled “Example”Simple example
const string = renderTemplate('<div><%= foo %></div>', { foo: 'bar',});
Multiple templates
<li> <%- name %></li>
<ul> <% items.forEach(function(item) { %> <%= renderTemplate(itemTemplate, item) %> <% }); %></ul>
import { renderTemplate } from '@soleil-se/app-util';import mainTemplate from './views/main.html';import itemTemplate from './views/item.html';
const items = [{ name: 'Foo' }, { name: 'Bar' }, { name: 'Baz' }];const string = renderTemplate(mainTemplate, { items, itemTemplate });
NOTE Remember that the second argument must be an object and that objects properties are accessed directly in any child templates!
isOffline
Section titled “isOffline”If the webapp is running in offline mode or not. This value is also passed to AppData.
if(isOffline) { // Do something}
isOnline
Section titled “isOnline”If the webapp is running in online mode or not. This value is also passed to AppData.
if(isOnline) { // Do something}
uniqueId
Section titled “uniqueId”A unique ID for the WebApp. This value is also passed to AppData.
AppData
Section titled “AppData”@soleil-se/app-util/app-data
Data that is available for the WebApp.
import AppData from '@soleil-se/app-util/app-data';
// These are avaliable per default:const { isOffline, isOnline, uniqueId } = AppData;
All data passed to the app is available in AppData: index.js
res.send(renderApp({ foo: 'bar' }))
In app:
import AppData from '@soleil-se/app-util/app-data';
console.log(AppData.foo);
// Or descructuredconst { foo } = AppData;console.log(foo);
Svelte
Section titled “Svelte”Functions for rendering a Svelte application.
Server
Section titled “Server”@soleil-se/app-util/svelte-server
render(App, [data], [settings])
⇨ String
Section titled “render(App, [data], [settings]) ⇨ String”Get HTML string for rendering a universal Svelte application.
To be used together with @soleil-se/app-util/svelte-client
.
Returns: String
- HTML for rendering an application.
Param | Type | Default | Description |
---|---|---|---|
[App] | Svelte | Svelte application. | |
[data] | Object | {} | Server data or config that will be available from @soleil-se/app-util/app-data |
[settings] | Object | {} | Settings object. |
[settings.async] | Boolean | false | If the app script should be loaded asynchronously. Read more about async and defer. |
[settings.defer] | Boolean | true | If the app script should be loaded after DOM is ready. Read more about async and defer. |
[settings.req] | Object | The req object from SiteVision, pass this to optimize browser specific script loading if you have multiple instances of the app. |
In app_src/server/index.js
or app_src/index.js
.
import router from 'router';import { render } from '@soleil-se/app-util/svelte-server';
import App from './App.svelte';
router.get('/', (req, res) => { const data = { foo: 'bar' }; res.send(renderSvelte(App, data));});
renderServer(App, [data])
⇨ String
Section titled “renderServer(App, [data]) ⇨ String”Get HTML string for a server only rendered Svelte application.
Returns: String
- HTML for rendering an application.
Param | Type | Default | Description |
---|---|---|---|
[App] | Svelte | Svelte application. | |
[data] | Object | {} | Server data or config that will be available from @soleil-se/app-util/app-data |
In app_src/server/index.js
or app_src/index.js
.
import router from 'router';import { renderServer } from '@soleil-se/app-util/svelte-server';
import App from './App.svelte';
router.get('/', (req, res) => { const data = { foo: 'bar' }; res.send(renderServer(App, data));});
renderClient(App, [data], [settings])
⇨ String
Section titled “renderClient(App, [data], [settings]) ⇨ String”Get HTML string for a client only rendered Svelte application.
Can be used together with @soleil-se/app-util/svelte-client
.
Returns: String
- HTML for rendering an application.
Param | Type | Default | Description |
---|---|---|---|
[App] | Svelte | Svelte application. | |
[data] | Object | {} | Server data or config that will be available from @soleil-se/app-util/app-data |
[settings] | Object | {} | Settings object. |
[settings.async] | Boolean | false | If the app script should be loaded asynchronously. Read more about async and defer. |
[settings.defer] | Boolean | true | If the app script should be loaded after DOM is ready. Read more about async and defer. |
[settings.req] | Object | The req object from SiteVision, pass this to optimize browser specific script loading if you have multiple instances of the app. |
In app_src/server/index.js
or app_src/index.js
.
import router from 'router';import { renderClient } from '@soleil-se/app-util/svelte-server';
import App from './App.svelte';
router.get('/', (req, res) => { const data = { foo: 'bar' }; res.send(renderSvelte(App, data));});
Client
Section titled “Client”@soleil-se/app-util/svelte-client
render(App)
Section titled “render(App)”In app_src/client/index.js
or app_src/main.js
.
import { render } from '@soleil-se/app-util/svelte-client';import App from './App.svelte';
render(App);