fix: partial form redirect not working (#2497)
Fixes https://github.com/denoland/fresh/issues/2404
This commit is contained in:
@@ -124,7 +124,7 @@ document.addEventListener("click", async (e) => {
|
||||
partial ? partial : nextUrl.href,
|
||||
location.href,
|
||||
);
|
||||
await fetchPartials(partialUrl);
|
||||
await fetchPartials(nextUrl, partialUrl);
|
||||
updateLinks(nextUrl);
|
||||
scrollTo({ left: 0, top: 0, behavior: "instant" });
|
||||
} finally {
|
||||
@@ -157,7 +157,7 @@ document.addEventListener("click", async (e) => {
|
||||
partial,
|
||||
location.href,
|
||||
);
|
||||
await fetchPartials(partialUrl);
|
||||
await fetchPartials(partialUrl, partialUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -192,7 +192,7 @@ addEventListener("popstate", async (e) => {
|
||||
|
||||
const url = new URL(location.href, location.origin);
|
||||
try {
|
||||
await fetchPartials(url);
|
||||
await fetchPartials(url, url);
|
||||
updateLinks(url);
|
||||
scrollTo({
|
||||
left: state.scrollX ?? 0,
|
||||
@@ -257,8 +257,7 @@ document.addEventListener("submit", async (e) => {
|
||||
init = { body: new FormData(el), method: lowerMethod };
|
||||
}
|
||||
|
||||
maybeUpdateHistory(actionUrl);
|
||||
await fetchPartials(partialUrl, init);
|
||||
await fetchPartials(actionUrl, partialUrl, init);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -283,10 +282,23 @@ function updateLinks(url: URL) {
|
||||
});
|
||||
}
|
||||
|
||||
async function fetchPartials(url: URL, init: RequestInit = {}) {
|
||||
async function fetchPartials(
|
||||
actualUrl: URL,
|
||||
partialUrl: URL,
|
||||
init: RequestInit = {},
|
||||
) {
|
||||
init.redirect = "follow";
|
||||
url.searchParams.set(PARTIAL_SEARCH_PARAM, "true");
|
||||
const res = await fetch(url, init);
|
||||
partialUrl.searchParams.set(PARTIAL_SEARCH_PARAM, "true");
|
||||
const res = await fetch(partialUrl, init);
|
||||
|
||||
if (res.redirected) {
|
||||
const nextUrl = new URL(res.url);
|
||||
if (nextUrl.origin === actualUrl.origin) {
|
||||
actualUrl = nextUrl;
|
||||
}
|
||||
}
|
||||
|
||||
maybeUpdateHistory(actualUrl);
|
||||
await applyPartials(res);
|
||||
}
|
||||
|
||||
|
||||
@@ -1538,6 +1538,60 @@ Deno.test({
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "partials - submit form redirecct",
|
||||
fn: async () => {
|
||||
const app = testApp()
|
||||
.get("/done", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
<Partial name="foo">
|
||||
<h1 class="done">success</h1>
|
||||
</Partial>
|
||||
</Doc>,
|
||||
);
|
||||
})
|
||||
.post("/partial", async (ctx) => {
|
||||
const data = await ctx.req.formData();
|
||||
const name = String(data.get("name"));
|
||||
|
||||
return new Response(null, {
|
||||
status: 303,
|
||||
headers: { Location: `/done?name=${encodeURIComponent(name)}` },
|
||||
});
|
||||
})
|
||||
.get("/", (ctx) => {
|
||||
return ctx.render(
|
||||
<Doc>
|
||||
<div f-client-nav>
|
||||
<form action="/partial" method="post">
|
||||
<input name="name" value="foo" />
|
||||
<Partial name="foo">
|
||||
<p class="init">init</p>
|
||||
</Partial>
|
||||
<SelfCounter />
|
||||
<button class="update">
|
||||
update
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</Doc>,
|
||||
);
|
||||
});
|
||||
|
||||
await withBrowserApp(app, async (page, address) => {
|
||||
await page.goto(address, { waitUntil: "load" });
|
||||
await page.locator(".ready").wait();
|
||||
|
||||
await page.locator(".update").click();
|
||||
await page.locator(".done").wait();
|
||||
|
||||
const pathname = await page.evaluate(() => window.location.pathname);
|
||||
expect(pathname).toEqual("/done");
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "partials - submit form via external submitter",
|
||||
fn: async () => {
|
||||
|
||||
Reference in New Issue
Block a user