Add optional Datastar plumbing
This commit is contained in:
@@ -33,3 +33,9 @@ The example uses query-string routing in the same style as the PHP starter, but
|
|||||||
Direct requests to `/examples/uce-starter/index.uce` still work, but self-links are canonicalized back to `/examples/uce-starter/`.
|
Direct requests to `/examples/uce-starter/index.uce` still work, but self-links are canonicalized back to `/examples/uce-starter/`.
|
||||||
|
|
||||||
The demo account pages use a small file-backed user store under `/tmp/uce-starter-data/` with session-based login state.
|
The demo account pages use a small file-backed user store under `/tmp/uce-starter-data/` with session-based login state.
|
||||||
|
|
||||||
|
|
||||||
|
## Optional Datastar Assets
|
||||||
|
|
||||||
|
This starter vendors Datastar v1.0.2 as `js/datastar.js` for future server-rendered interactive islands. It is not loaded by the default page shell. `lib/datastar.uce` contains small Datastar SSE formatting helpers, and `views/datastar-example.uce` is an unlinked opt-in example route.
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,38 @@
|
|||||||
|
String datastar_data_lines(String field, String value)
|
||||||
|
{
|
||||||
|
String out = "";
|
||||||
|
for(String line : split(value, "\n"))
|
||||||
|
out += "data: " + field + " " + line + "\n";
|
||||||
|
return(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
void datastar_sse_headers(Request& context)
|
||||||
|
{
|
||||||
|
context.call["app"]["page_type"] = "blank";
|
||||||
|
context.header["Content-Type"] = "text/event-stream; charset=utf-8";
|
||||||
|
context.header["Cache-Control"] = "no-cache, no-store, must-revalidate";
|
||||||
|
context.header["X-Accel-Buffering"] = "no";
|
||||||
|
}
|
||||||
|
|
||||||
|
String datastar_event(String event_name, StringMap fields)
|
||||||
|
{
|
||||||
|
String out = "event: " + event_name + "\n";
|
||||||
|
for(const auto& field : fields)
|
||||||
|
out += datastar_data_lines(field.first, field.second);
|
||||||
|
out += "\n";
|
||||||
|
return(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
String datastar_patch_elements(String html)
|
||||||
|
{
|
||||||
|
StringMap fields;
|
||||||
|
fields["elements"] = html;
|
||||||
|
return(datastar_event("datastar-patch-elements", fields));
|
||||||
|
}
|
||||||
|
|
||||||
|
String datastar_patch_signals(String signals_json)
|
||||||
|
{
|
||||||
|
StringMap fields;
|
||||||
|
fields["signals"] = signals_json;
|
||||||
|
return(datastar_event("datastar-patch-signals", fields));
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#load "../lib/app.uce"
|
||||||
|
#load "../lib/datastar.uce"
|
||||||
|
|
||||||
|
COMPONENT(Request& context)
|
||||||
|
{
|
||||||
|
if(context.get["stream"] == "1")
|
||||||
|
{
|
||||||
|
datastar_sse_headers(context);
|
||||||
|
print(datastar_patch_elements("<div id=\"datastar-example-result\">Server-rendered UCE Datastar fragment</div>"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
<>
|
||||||
|
<h1>Datastar optional example</h1>
|
||||||
|
<p>Datastar is vendored beside the starter client libraries for future server-rendered islands. It is not loaded globally.</p>
|
||||||
|
<script type="module" src="<?= app_asset_url("js/datastar.js", context) ?>"></script>
|
||||||
|
<button class="btn" data-on:click="@get('<?= app_link("datastar-example", context) ?>&stream=1')">Patch a fragment</button>
|
||||||
|
<div id="datastar-example-result">Waiting for a server patch.</div>
|
||||||
|
</>;
|
||||||
|
}
|
||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user