The journey of an HTTP request through Enhance isn’t complicated, but it’s helpful to understand how the final HTML document is assembled from the code you author in
my-rad-app.net/login → app/api/login.mjs → app/pages/login.html
If an error is encountered, either because no route match was found (
404) or an internal exception (
500) occurs, Enhance calls the corresponding error function.
API Data Function
If there is an API function, but no matching Page or the request’s headers includes
Accept: application/json, the result of the API function will be returned directly to the client as JSON with the correct headers.
This is a great way to add JSON API endpoints to your Enhance app!
Even if those endpoints have an associated Page: requests that set
Accept: application/json will get plain JSON without a Page render.
Regardless of a matching API function, the matching Page will be used.
my-rad-app.net/contact → app/pages/contact.[html|mjs]
Pages are the main entry point and usually contain one or more of your app’s custom elements.
Enhance will assemble the Page by recursively expanding custom elements, using your single-file-components in the
If there is no custom element definition in
app/elements/, Enhance will render it as is and move on.
The Final Document
Enhance starts assembling HTML by running your (optional) Head function to create the top of the document and then appends all interpreted elements from your Page to the document’s body.
If your custom element definitions include
<style> tags, Enhance scopes those CSS declarations while combining them with other element styles into a single
<style> element in the
Similar to the stylesheet process, Enhance gathers
<script> tags from custom element definitions and inserts them just before the document’s closing
Enhancements & Web Components
Not Covered Here
This lifecycle description demonstrates the real-time journey of a request through Enhance’s web server.
It does not include static asset resolution (from
_public/), which has a separate lifecycle for even faster responses for non-dynamic content.