From fd235e877c4e2a071bd6bb8784719d0d7afc417e Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Thu, 5 Oct 2023 10:47:37 +0200 Subject: [PATCH] feat: only enable partials when attribute is set (#1861) --- src/runtime/entrypoints/main.ts | 110 ++++++++++++++++--------- tests/fixture_partials/routes/_app.tsx | 2 +- tests/partials_test.ts | 6 +- 3 files changed, 77 insertions(+), 41 deletions(-) diff --git a/src/runtime/entrypoints/main.ts b/src/runtime/entrypoints/main.ts index 56a49a39..ec8bcc13 100644 --- a/src/runtime/entrypoints/main.ts +++ b/src/runtime/entrypoints/main.ts @@ -5,6 +5,7 @@ import { ComponentType, Fragment, h, + isValidElement, options, render, VNode, @@ -720,7 +721,7 @@ export async function applyPartials(res: Response): Promise { } else { // deno-lint-ignore no-explicit-any const mode = (vnode.props as any).mode; - const children = vnode.props.children; + let children = vnode.props.children; // Modify children depending on the replace mode if (mode === PartialMode.REPLACE) { @@ -734,8 +735,45 @@ export async function applyPartials(res: Response): Promise { if (mode === PartialMode.APPEND) { newChildren.push(children); } else { + // Workaround for missing keys + if (!isValidElement(children)) { + children = h(Fragment, null, children); + } + + if ((children as VNode).key == null) { + (children as VNode).key = newChildren.length; + } + + // Update rendered children keys if necessary + // deno-lint-ignore no-explicit-any + const renderedChildren = (instance as any).__v.__k as VNode[] | null; + if (Array.isArray(renderedChildren)) { + for (let i = 0; i < renderedChildren.length; i++) { + const child = renderedChildren[i]; + if (child.key == null) { + child.key = i; + } else { + // Assume list is keyed. We don't support mixed + // keyed an unkeyed + break; + } + } + } + + for (let i = 0; i < newChildren.length; i++) { + const child = newChildren[i]; + if (child.key == null) { + child.key = i; + } else { + // Assume list is keyed. We don't support mixed + // keyed an unkeyed + break; + } + } + newChildren.unshift(children); } + instance.props.children = newChildren; } @@ -762,6 +800,10 @@ export interface FreshHistoryState { scrollY: number; } +function checkClientNavEnabled() { + return document.querySelector(`[${CLIENT_NAV_ATTR}]`) !== null; +} + // Keep track of history state to apply forward or backward animations let index = history.state?.index || 0; if (!history.state) { @@ -798,15 +840,9 @@ document.addEventListener("click", async (e) => { const partial = el.getAttribute(PARTIAL_ATTR); // Check if the user opted out of client side navigation. - // There are two cases to account for: - // 1. Partial request - // 2. Normal request (turbolink-style) - const settingEl = el.closest(`[${CLIENT_NAV_ATTR}]`); if ( - partial === null && ( - settingEl === null || - settingEl.getAttribute(CLIENT_NAV_ATTR) !== "true" - ) + !checkClientNavEnabled() || + el.closest(`[${CLIENT_NAV_ATTR}="true"]`) === null ) { return; } @@ -871,36 +907,34 @@ addEventListener("popstate", async (e) => { const nextIdx = state.index ?? index + 1; index = nextIdx; - // If we have the body partial, then we assume that we can - // do a full client-side navigation. Otherwise do a full - // page navigation. - if (partials.has("body")) { - // We need to keep track of that ourselves since we do client side - // navigation. - if (history.scrollRestoration) { - history.scrollRestoration = "manual"; - } - - const url = new URL(location.href, location.origin); - try { - await fetchPartials(url, url); - scrollTo({ - left: state.scrollX ?? 0, - top: state.scrollY ?? 0, - behavior: "instant", - }); - } catch (err) { - // If the response didn't contain a partial, then we can only - // do a reload. - if (err instanceof NoPartialsError) { - location.reload(); - return; - } - - throw err; - } - } else { + if (!checkClientNavEnabled()) { location.reload(); + return; + } + + // We need to keep track of that ourselves since we do client side + // navigation. + if (history.scrollRestoration) { + history.scrollRestoration = "manual"; + } + + const url = new URL(location.href, location.origin); + try { + await fetchPartials(url, url); + scrollTo({ + left: state.scrollX ?? 0, + top: state.scrollY ?? 0, + behavior: "instant", + }); + } catch (err) { + // If the response didn't contain a partial, then we can only + // do a reload. + if (err instanceof NoPartialsError) { + location.reload(); + return; + } + + throw err; } }); diff --git a/tests/fixture_partials/routes/_app.tsx b/tests/fixture_partials/routes/_app.tsx index bdfa0118..8bd4ed3c 100644 --- a/tests/fixture_partials/routes/_app.tsx +++ b/tests/fixture_partials/routes/_app.tsx @@ -8,7 +8,7 @@ export default defineApp((res, ctx) => { test - + diff --git a/tests/partials_test.ts b/tests/partials_test.ts index 94bdab66..9b1fd169 100644 --- a/tests/partials_test.ts +++ b/tests/partials_test.ts @@ -630,7 +630,10 @@ Deno.test("partial navigation", async () => { document.querySelectorAll(".island").length === 1 ); assertEquals(page.url(), initialUrl); - await waitForText(page, "pre", "mount Counter A\n"); + await waitFor(async () => { + const doc = parseHtml(await page.content()); + return /mount Counter A/.test(doc.querySelector("pre")!.textContent!); + }); await assertNoPageComments(page); }, ); @@ -1024,4 +1027,3 @@ Deno.test("throws an error when response contains no partials", async () => { }); // TODO Head merging -// TODO Children Keys