Skip to content

Commit 5a79514

Browse files
committed
make page embedding and custom layouts easier and less error prone
- The `embed` property now automatically adds the `_sqlpage_embed` parameter to the embedded page URL to render it as an embeddable fragment. - When an embedded page is rendered, the `shell` component is automatically replaced by a `shell-empty` component, to avoid displaying a duplicate shell and creating invalid duplicated page metadata in the response. see #799
1 parent 817fd97 commit 5a79514

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
- In the map component, add support for map pins with a description but no title.
1919
- Improved error messages when a parameter of a sqlpage function is invalid. Error traces used to be truncated, and made it hard to understand the exact cause of the error in some cases. In particular, calls to `sqlpage.fetch` would display an unhelpful error message when the HTTP request definition was invalid. `sqlpage.fetch` now also throws an error if the HTTP request definition contains unknown fields.
2020
- Make the `headers` field of the `sqlpage.fetch` function parameter optional. It defaults to sending a User-Agent header containing the SQLPage version.
21+
- Make custom layout creations with the `card` component easier and less error-prone:
22+
- The `embed` property now automatically adds the `_sqlpage_embed` parameter to the embedded page URL to render it as an embeddable fragment.
23+
- When an embedded page is rendered, the `shell` component is automatically replaced by a `shell-empty` component, to avoid displaying a duplicate shell and creating invalid duplicated page metadata in the response.
2124

2225
## 0.32.1 (2025-01-03)
2326

sqlpage/sqlpage.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const nonce = document.currentScript.nonce;
44
function sqlpage_card() {
55
for (const c of document.querySelectorAll("[data-pre-init=card]")) {
66
c.removeAttribute("data-pre-init");
7-
fetch(c.dataset.embed)
7+
const url = new URL(c.dataset.embed, window.location.href);
8+
url.searchParams.set("_sqlpage_embed", "1");
9+
fetch(url)
810
.then((res) => res.text())
911
.then((html) => {
1012
const body = c.querySelector(".card-content");

src/render.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,16 @@ impl<W: std::io::Write> HtmlRenderContext<W> {
653653
let shell_row = rows_iter
654654
.next()
655655
.expect("shell row should exist at this point");
656+
let mut shell_component =
657+
get_object_str(&shell_row, "component").expect("shell should exist");
658+
if request_context.is_embedded && shell_component != FRAGMENT_SHELL_COMPONENT {
659+
log::warn!(
660+
"Embedded pages cannot use a shell component! Ignoring the '{shell_component}' component and its properties: {shell_row}"
661+
);
662+
shell_component = FRAGMENT_SHELL_COMPONENT;
663+
}
656664
let mut shell_renderer = Self::create_renderer(
657-
get_object_str(&shell_row, "component").expect("shell should exist"),
665+
shell_component,
658666
Arc::clone(&app_state),
659667
0,
660668
request_context.content_security_policy.nonce,

0 commit comments

Comments
 (0)