fix(partial): buttons outside forms should not trigger navigation (#3584)
`<button>` elements that are not inside a form and have a `f-partial` attribute should not trigger a history change.
This commit is contained in:
@@ -132,7 +132,7 @@ document.addEventListener("click", async (e) => {
|
|||||||
partial ? partial : nextUrl.href,
|
partial ? partial : nextUrl.href,
|
||||||
location.href,
|
location.href,
|
||||||
);
|
);
|
||||||
await fetchPartials(nextUrl, partialUrl);
|
await fetchPartials(nextUrl, partialUrl, true);
|
||||||
updateLinks(nextUrl);
|
updateLinks(nextUrl);
|
||||||
scrollTo({ left: 0, top: 0, behavior: "instant" });
|
scrollTo({ left: 0, top: 0, behavior: "instant" });
|
||||||
} finally {
|
} finally {
|
||||||
@@ -155,7 +155,7 @@ document.addEventListener("click", async (e) => {
|
|||||||
partial,
|
partial,
|
||||||
location.href,
|
location.href,
|
||||||
);
|
);
|
||||||
await fetchPartials(partialUrl, partialUrl);
|
await fetchPartials(partialUrl, partialUrl, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -192,7 +192,7 @@ addEventListener("popstate", async (e) => {
|
|||||||
|
|
||||||
const url = new URL(location.href, location.origin);
|
const url = new URL(location.href, location.origin);
|
||||||
try {
|
try {
|
||||||
await fetchPartials(url, url);
|
await fetchPartials(url, url, true);
|
||||||
updateLinks(url);
|
updateLinks(url);
|
||||||
scrollTo({
|
scrollTo({
|
||||||
left: state.scrollX ?? 0,
|
left: state.scrollX ?? 0,
|
||||||
@@ -254,7 +254,7 @@ document.addEventListener("submit", async (e) => {
|
|||||||
init = { body: new FormData(el, e.submitter), method: lowerMethod };
|
init = { body: new FormData(el, e.submitter), method: lowerMethod };
|
||||||
}
|
}
|
||||||
|
|
||||||
await fetchPartials(actionUrl, partialUrl, init);
|
await fetchPartials(actionUrl, partialUrl, true, init);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -282,6 +282,7 @@ function updateLinks(url: URL) {
|
|||||||
async function fetchPartials(
|
async function fetchPartials(
|
||||||
actualUrl: URL,
|
actualUrl: URL,
|
||||||
partialUrl: URL,
|
partialUrl: URL,
|
||||||
|
shouldNavigate: boolean,
|
||||||
init: RequestInit = {},
|
init: RequestInit = {},
|
||||||
) {
|
) {
|
||||||
init.redirect = "follow";
|
init.redirect = "follow";
|
||||||
@@ -296,7 +297,10 @@ async function fetchPartials(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
maybeUpdateHistory(actualUrl);
|
if (shouldNavigate) {
|
||||||
|
maybeUpdateHistory(actualUrl);
|
||||||
|
}
|
||||||
|
|
||||||
await applyPartials(res);
|
await applyPartials(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1404,10 +1404,6 @@ Deno.test({
|
|||||||
|
|
||||||
await page.locator(".update").click();
|
await page.locator(".update").click();
|
||||||
|
|
||||||
await page.waitForFunction(() => {
|
|
||||||
const url = new URL(window.location.href);
|
|
||||||
return url.pathname === "/partial";
|
|
||||||
});
|
|
||||||
await page.locator(".done").wait();
|
await page.locator(".done").wait();
|
||||||
await waitForText(page, ".output", "1");
|
await waitForText(page, ".output", "1");
|
||||||
});
|
});
|
||||||
@@ -2665,6 +2661,46 @@ Deno.test({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "partials - button should not update history",
|
||||||
|
fn: async () => {
|
||||||
|
const app = testApp()
|
||||||
|
.get("/other", (ctx) => {
|
||||||
|
return ctx.render(
|
||||||
|
<Doc>
|
||||||
|
<Partial name="foo">
|
||||||
|
<h1 class="done">other</h1>
|
||||||
|
</Partial>
|
||||||
|
</Doc>,
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.get("/", (ctx) => {
|
||||||
|
return ctx.render(
|
||||||
|
<Doc>
|
||||||
|
<div f-client-nav>
|
||||||
|
<button type="button" f-partial="/other">click</button>
|
||||||
|
<Partial name="foo">
|
||||||
|
<h1 class="init">foo</h1>
|
||||||
|
</Partial>
|
||||||
|
<SelfCounter />
|
||||||
|
</div>
|
||||||
|
</Doc>,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
await withBrowserApp(app, async (page, address) => {
|
||||||
|
await page.goto(address, { waitUntil: "load" });
|
||||||
|
await page.locator(".ready").wait();
|
||||||
|
await page.locator("button").click();
|
||||||
|
await page.locator(".done").wait();
|
||||||
|
|
||||||
|
const rawUrl = await page.evaluate(() => window.location.href);
|
||||||
|
const url = new URL(rawUrl);
|
||||||
|
expect(`${url.pathname}${url.search}`).toEqual("/");
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "partials - backwards navigation should keep URLs",
|
name: "partials - backwards navigation should keep URLs",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user