Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/fresh/src/runtime/client/partials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,21 @@ document.addEventListener("submit", async (e) => {
return;
}

const hasExplicitPartial = e.submitter?.hasAttribute(PARTIAL_ATTR) ||
e.submitter?.hasAttribute("formaction") ||
el.hasAttribute(PARTIAL_ATTR) ||
el.hasAttribute("action");

const rawPartialUrl = e.submitter?.getAttribute(PARTIAL_ATTR) ??
e.submitter?.getAttribute("formaction") ??
el.getAttribute(PARTIAL_ATTR) ?? el.action;
const rawActionUrl = e.submitter?.getAttribute("formaction") ?? el.action;

if (rawPartialUrl !== "") {
// Only intercept forms that explicitly opt in to partial navigation
// via f-partial, formaction, or an explicit action attribute. Without
// this check, every form inside f-client-nav would be intercepted
// because el.action is always non-empty (defaults to the current URL).
if (hasExplicitPartial && rawPartialUrl !== "") {
e.preventDefault();

const partialUrl = new URL(rawPartialUrl, location.href);
Expand Down
43 changes: 43 additions & 0 deletions packages/fresh/tests/partials_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1857,6 +1857,49 @@ Deno.test({
},
});

Deno.test({
name: "partials - form without action inside f-client-nav not intercepted",
fn: async () => {
const app = testApp()
.post("/", (ctx) => {
return ctx.render(
<Doc>
<p class="submitted">form submitted normally</p>
</Doc>,
);
})
.get("/", (ctx) => {
return ctx.render(
<Doc>
<div f-client-nav>
<form method="post">
<input name="name" value="foo" />
<Partial name="foo">
<p class="init">init</p>
</Partial>
<button type="submit" class="update">
update
</button>
</form>
</div>
</Doc>,
);
});

await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator(".init").wait();

// Form should do a full page navigation, not a partial update
await Promise.all([
page.waitForNavigation(),
page.locator(".update").click(),
]);
await page.locator(".submitted").wait();
});
},
});

Deno.test({
name: "partials - submit form redirect",
fn: async () => {
Expand Down
Loading