So I have a SvelteKit project with the following project structure:
lib/
├─ components/
│ └─ my_module/
│ └─ my_resource/
│ ├─ CreateObjectButton.svelte ← Button component (important by dashboard, don't think this is relevant)
│ └─ CreateObjectModal.svelte ← Modal component (imported by dashboard)
routes/
└─ (protected)/
├─ dashboard/
│ ├─ +page.svelte ← Contains the grid of cards
│ └─ cards/
│ └─ MyResourceCard.svelte ← Card component with button that triggers modal
│
└─ my_module/
└─ my_resource/
└─ +page.server.js ← Contains the CRUD actions, only create for now
I’m trying to have a modal (CreateObjectModal.svelte) open from /dashboard and submit a form to the create action in /my_module/my_resource/+page.server.js.
The simplified modal component:
<script>
import { createNotableModal } from '$lib/stores/modal';
import { enhance } from '$app/forms';
import { goto } from '$app/navigation';
export let num_rows = 2;
const closeModal = () => $createNotableModal.isOpen = false;
</script>
{#if $createNotableModal.isOpen}
<div class="modal-overlay" on:click={closeModal}>
<div class="modal" role="dialog" on:click|stopPropagation>
<form
method="POST"
action="/my_module/my_resource?/create"
use:enhance={({ result, update }) => {
console.log(result);
update();
}}
>
<input name="alias" type="text" required />
<input name="name" type="text" required />
<textarea name="jottings" rows={num_rows}></textarea>
<button type="submit">Create</button>
<button type="button" on:click={closeModal}>Cancel</button>
</form>
</div>
</div>
{/if}
The server action:
import { fail } from '@sveltejs/kit';
export const actions = {
create: async ({ request }) => {
console.log('Action hit!'); // This never runs
const data = await request.formData();
return { success: true, data: Object.fromEntries(data) };
}
};
What I’ve tried:
- Using action="/my_module/my_resource?/_action=create" (absolute path) and ?/create (relative)
- Removing on:submit|preventDefault
- Moving the modal temporarily into /my_module/my_resource/
- Checking for JS errors in the console — none present
- Confirming the modal opens and clicking buttons works
Observations:
- The network tab shows no POST request when I submit.
- console.log in the server action never triggers.
- Clicking the submit button does not trigger on:click handlers either.
Minimal example test: I created a standalone form directly in /my_module/my_resource/+page.svelte (no modal, one input, one button), and it works perfectly. The network request fires and the server logs the action.
Question:
Why does the form inside my modal never submit to the server action when the modal is opened from /dashboard, even when using the absolute path? How can I fix it so a modal component rendered on a different route can post to another route’s named action?