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,
|
partial ? partial : nextUrl.href,
|
||||||
location.href,
|
location.href,
|
||||||
);
|
);
|
||||||
await fetchPartials(partialUrl);
|
await fetchPartials(nextUrl, partialUrl);
|
||||||
updateLinks(nextUrl);
|
updateLinks(nextUrl);
|
||||||
scrollTo({ left: 0, top: 0, behavior: "instant" });
|
scrollTo({ left: 0, top: 0, behavior: "instant" });
|
||||||
} finally {
|
} finally {
|
||||||
@@ -157,7 +157,7 @@ document.addEventListener("click", async (e) => {
|
|||||||
partial,
|
partial,
|
||||||
location.href,
|
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);
|
const url = new URL(location.href, location.origin);
|
||||||
try {
|
try {
|
||||||
await fetchPartials(url);
|
await fetchPartials(url, url);
|
||||||
updateLinks(url);
|
updateLinks(url);
|
||||||
scrollTo({
|
scrollTo({
|
||||||
left: state.scrollX ?? 0,
|
left: state.scrollX ?? 0,
|
||||||
@@ -257,8 +257,7 @@ document.addEventListener("submit", async (e) => {
|
|||||||
init = { body: new FormData(el), method: lowerMethod };
|
init = { body: new FormData(el), method: lowerMethod };
|
||||||
}
|
}
|
||||||
|
|
||||||
maybeUpdateHistory(actionUrl);
|
await fetchPartials(actionUrl, partialUrl, init);
|
||||||
await fetchPartials(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";
|
init.redirect = "follow";
|
||||||
url.searchParams.set(PARTIAL_SEARCH_PARAM, "true");
|
partialUrl.searchParams.set(PARTIAL_SEARCH_PARAM, "true");
|
||||||
const res = await fetch(url, init);
|
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);
|
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({
|
Deno.test({
|
||||||
name: "partials - submit form via external submitter",
|
name: "partials - submit form via external submitter",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user