Rendering Lifecycle
Understanding Nex’s rendering lifecycle is the cornerstone of building efficient, stateless-feeling applications. Nex’s rendering process is straightforward and divided into two main phases.
1. Initial Render (Full Page Load)
When you access a URL directly via the browser address bar (GET request), Nex performs the following:
- Route Matching: Finds the corresponding page module based on the file system route.
-
Page ID Generation: The framework generates a unique
page_idfor this visit. -
Execute
mount(params):- Receives URL parameters and query parameters.
- Initialize data here (e.g., read from database).
-
Returns the
assignsMap.
-
Execute
render(assigns):-
Renders the HEEx template using
assigns.
-
Renders the HEEx template using
-
Inject Layout:
-
Passes the rendered result as
@inner_contenttosrc/layouts.ex. -
Automated Injection: Automatically injects scripts containing CSRF Token and Page ID before the
</body>tag.
-
Passes the rendered result as
- Respond to Client: Returns the full HTML document.
2. Asynchronous Interaction (Action Update)
When you initiate a request via HTMX (e.g., hx-post), the lifecycle changes:
- Action Matching: Finds the target Action function based on the URL path or Referer.
-
Execute Action:
-
Skip mount: For performance and locality, Actions do not re-execute the page’s
mount. - Receives request parameters.
-
Returns an HTML fragment or control directives (e.g.,
{:refresh}).
-
Skip mount: For performance and locality, Actions do not re-execute the page’s
-
Partial Update:
-
HTMX receives the response and only updates part of the page according to
hx-target. -
State Persistence: Although
mountdidn’t run, you can read previously stored state viaNex.Store.
-
HTMX receives the response and only updates part of the page according to
3. Page Refresh = State Reset
This is one of the most important design decisions in the Nex architecture:
- Full Page Refresh (F5): The browser discards all current state, Nex initiates a new GET request, and a brand new Page ID is generated.
-
Consequence: Since
Nex.Storestate is bound to thepage_id, old state becomes inaccessible (and is later cleaned up by TTL). - Design Intent: Ensures the Web application behaves according to the user’s intuition—“refreshing the page returns to the initial state,” avoiding complex stale data cleanup issues.
Summary
| Feature | Initial Render (GET) | Action Interaction (POST/…) |
|---|---|---|
| Execute mount | Yes | No |
| Generate Page ID | New | Persist |
| Layout Wrap | Yes | No (Returns fragment only) |
| State Reading | Initial State | Can read cumulative state from Store |