Skip to content

Commit b53adfb

Browse files
committed
Redirect problem in SPA mode. Fixes #360.
1 parent 92ff5ea commit b53adfb

File tree

7 files changed

+105
-1
lines changed

7 files changed

+105
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717

1818
- `defaults` didn't generate correct `SuperValidated` data, making `superForm` confused. Also fixed type signature and removed the `jsonSchema` option that wasn't applicable.
1919
- A successful `PageData` result from `invalidateAll` was overwritten by the `ActionData` result.
20+
- Using `goto` in events didn't work when the target page redirected.
2021

2122
## [2.5.0] - 2024-02-21
2223

src/lib/client/superForm.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1781,15 +1781,22 @@ export function superForm<
17811781
};
17821782

17831783
currentRequest = null;
1784+
17841785
let cancelled = false;
1786+
const cancel = () => (cancelled = true);
17851787

17861788
const data = {
17871789
result,
17881790
formEl: FormElement,
17891791
formElement: FormElement,
1790-
cancel: () => (cancelled = true)
1792+
cancel
17911793
};
17921794

1795+
// Check for goto in the events
1796+
const unsubCheckforNav = navigating.subscribe(($nav) => {
1797+
if ($nav) cancel();
1798+
});
1799+
17931800
for (const event of formEvents.onResult) {
17941801
await event(data);
17951802
}
@@ -1914,6 +1921,8 @@ export function superForm<
19141921
}
19151922
});
19161923
}
1924+
1925+
unsubCheckforNav();
19171926
}
19181927

19191928
return validationResponse;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<script lang="ts">
2+
import { page } from '$app/stores';
3+
import { defaults, superForm } from '$lib/index.js';
4+
import { zod } from '$lib/adapters/zod.js';
5+
import SuperDebug from '$lib/index.js';
6+
import { goto } from '$app/navigation';
7+
8+
import { schema } from './schema.js';
9+
10+
export let data;
11+
12+
const form = superForm(defaults(zod(schema)), {
13+
SPA: true,
14+
validators: zod(schema),
15+
async onUpdate() {
16+
// uncomment out this line to fix the redirect
17+
//cancel();
18+
await goto('/v2/issue-360/target');
19+
// navigating directly to the second target (no redirect) works fine
20+
// await goto('/v2/issue-360/second-target');
21+
}
22+
});
23+
24+
const { form: formStore, message, enhance } = form;
25+
</script>
26+
27+
<SuperDebug data={$formStore} />
28+
29+
<h3>Superforms testing ground - Zod</h3>
30+
31+
{#if $message}
32+
<!-- eslint-disable-next-line svelte/valid-compile -->
33+
<div class="status" class:error={$page.status >= 400} class:success={$page.status == 200}>
34+
{$message}
35+
</div>
36+
{/if}
37+
38+
<form method="POST" use:enhance>
39+
<button>Submit</button>
40+
</form>
41+
42+
<hr />
43+
<p><a target="_blank" href="https://superforms.rocks/api">API Reference</a></p>
44+
45+
<style>
46+
.invalid {
47+
color: red;
48+
}
49+
50+
.status {
51+
color: white;
52+
padding: 4px;
53+
padding-left: 8px;
54+
border-radius: 2px;
55+
font-weight: 500;
56+
}
57+
58+
.status.success {
59+
background-color: seagreen;
60+
}
61+
62+
.status.error {
63+
background-color: #ff2a02;
64+
}
65+
66+
input {
67+
background-color: #ddd;
68+
}
69+
70+
a {
71+
text-decoration: underline;
72+
}
73+
74+
hr {
75+
margin-top: 4rem;
76+
}
77+
78+
form {
79+
padding-top: 1rem;
80+
padding-bottom: 1rem;
81+
}
82+
</style>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { z } from 'zod';
2+
3+
export const schema = z.object({});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<p>Second redirect successful</p>
2+
<a href="/">Go home</a>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<p>First redirect target</p>
2+
<a href="/">Go home</a>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { redirect } from '@sveltejs/kit';
2+
3+
export function load() {
4+
redirect(303, '/v2/issue-360/second-target');
5+
}

0 commit comments

Comments
 (0)