- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Hi Community!
At our Zürich developer meetup, I gave a talk on how Lux works under the hood, and I'd like to share what I learned. I spent about a month on the framework and wrote everything by hand to understand how it works.
By coincidence, @Martin Rudack published something similar, so I'm linking his video here for a deeper dive:
https://lnkd.in/p/e2SgexW3
1) What is Lux, and where does it fit?
Lux is ServiceNow's new UI framework. There's already plenty of content here on how to build with it, and it's great. But how does it actually work, and what technology is underneath? That's what this article is about.
Everything here comes from my own experiments, so please read it as observations rather than official facts.
Lux uses a technique called SSR (more on that below). We already had something similar for the UI: Jelly. It just works differently.
2) What is SSR?
The Lit documentation describes it as the following:
SSR stands for server-side rendering, a technique for generating and serving the HTML of your components, including shadow DOM and styles, before their JavaScript implementations have loaded and executed. (lit.dev)
Yes, Jelly is server-side rendering too: the instance renders every list and form. The difference is what happens after the first load. Lux renders only the first page on the server, with the data already in it, so there's no blank screen and no spinner. After that, the browser handles navigation without reloading the whole page. This works through hydration: the server sends finished HTML, and when the JavaScript arrives, it attaches to that existing HTML instead of building the page again. (Wikipedia)
A big advantage is that the user never sees loading widgets. The page arrives complete, with all the data in place, loaded as the logged-in user (so I'd expect ACLs to apply).
Is it faster? My tests can't tell. The first load can take a moment, but after that it's lightning fast.
3) Layers
Three layers work together:
- Lux, the framework from ServiceNow: pages, loaders, widgets, file-based routing, themes, server scripts,
sys_aix_*records - Lit, a standards-based library:
html``templates, reactive properties, the update cycle, SSR and hydration (lit.dev) - The browser: custom elements, shadow DOM, ES modules, events
Lux adds the ServiceNow part: routes, data fetching and so on. Lit provides the core technology, so the Lit documentation is a great place to learn how to build components. ServiceNow also ships a linter that points out Lux-specific rules, for example to use aiuxFetch instead of fetch.
4) Okay, but what is a Lit component?
In short, it's an HTML element, a custom one.
Lit describes a component as a piece of state that renders a UI from that state. It can react to user input and fire events. The code above is our hello-world widget, shortened, with the five concepts numbered.
Every page and widget is a class with its own properties. The custom element itself comes from the browser; Lit adds rendering, reactive properties and the update cycle.
Note: according to the Lux documentation, we extend AIUXElement or AIUXWidgetElement, not LitElement directly.
You may recognize the html tag. Lit templates are JavaScript template literals tagged with html, and they tell the page what to render.
Inside the template you can embed JavaScript expressions with
${...}; that's what makes your page interactive.
Notice the second html tag inside map(). Without it, the <li> would be rendered as plain text instead of HTML, so you have to tell Lit that it's a template.
The other key parts Lux adds are the loader and the render() method.
What happens here?
The loader receives ctx, which holds useful information about the page, such as params (route parameters, more on those below) and query (the URL query parameters).
Here's an example of ctx logged to the console:
render() returns the HTML template.
Be careful what you put in render(), because it runs twice: once on the server and once in the browser, during hydration. I tested this by rendering the current time, and the value differed between the server and the browser.
Strictly speaking, the browser doesn't build the page a second time. According to the Lit documentation, hydration re-associates the template with the HTML the server already rendered. That's why render() has to produce the same output on the server and in the browser. (lit.dev)
The Lit documentation says render() should:
- avoid changing the component's state,
- avoid side effects,
- use only the component's properties as input,
- return the same result for the same property values.
This keeps rendering deterministic and the code easier to reason about. (lit.dev)
5) How are pages built?
There's no route table: the folders are the routes. Only page.js becomes a page; other files in the folder are ordinary modules that page.js imports. I built my whole presentation in Lux, so the presentation itself was a page structure.
A page is a folder (or folder tree) containing a page.js file. A widget is a subfolder of widgets/ containing an index.js file.
Folders can also be placeholders, like
[id]. Their values arrive as params in ctx, which is how you make a page dynamic.
So how did I pass parameters to the subpage? First, I defined an event handler in the parent page (problems/page.js😞
_openRecord = e => {
navigate(`/problems/${e.detail.listTable}/${e.detail.listSysId}`);
}
Then I bound it to the list component's event, so whenever a reference link is clicked, it passes the table and sys_id, and the subpage's loader picks them up:
render() {
const {table, query, data} = this.loaderData ?? {};
return html`
<h1>Problems</h1>
<aiux-list-connected
table=${table}
.query=${query || 'active=true'}
.data=${data}
@aiux-list-connected:reference-link-clicked=${this._openRecord}
@aiux-list-connected:query-updated=${this._handleQueryUpdated}
@aiux-list-connected:group-sort-updated=${this._handleGroupSortUpdated}
@aiux-list-connected:split-view-changed=${this._handleSplitViewChanged}
@aiux-list-connected:view-updated=${this._handleViewUpdated}
></aiux-list-connected>
`;
}
6) Styling
According to the ServiceNow documentation, Lux uses Tailwind and daisyUI, so the components look somewhat like these: daisyUI components
You set the overall styling in lux/theme.js, but it has limits:
export const AppTheme = {
primary_color: '#1F5FD1',
neutral_colors: {
use_hex_value: true,
hex_value: '#1F5FD1'
},
shape: {
selector_radius: '0.75rem',
field_radius: '0.5rem',
boxes_radius: '1rem',
border_width: '1px'
}
};
I picked a very strong blue, but Lux has an open-source library built in, Leonardo, that generates colours from a target contrast ratio. So you can't fully control your brand colour: Lux adjusts it to some extent.
7) Components
I couldn't find official documentation for the components, but ServiceNow ships them all in the npm package, so you can read the source yourself: node_modules/@servicenow/aiux/components/
So what think?
It's a very interesting new framework that could change the ServiceNow UI experience. However, I don't yet see how workspaces and existing, heavily customized portals could be migrated.
SSR is an advantage, but the first load could become a user-experience problem when the underlying data is heavily customized.
Disclaimer: I used AI to check my grammar, format the document and the slides shown here were generated with AI. The research, experiments and conclusions are my own.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.