Skip to content

Proper responses for form requests that use Precognition #725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
benjaminkohl opened this issue Mar 24, 2025 · 0 comments
Open

Proper responses for form requests that use Precognition #725

benjaminkohl opened this issue Mar 24, 2025 · 0 comments

Comments

@benjaminkohl
Copy link

benjaminkohl commented Mar 24, 2025

Composer Package Versions

Laravel 12.3.0
Inertia 2.0.1

package.json

laravel-precognition-vue-inertia 0.7.1

Description of Problem

I am having trouble understanding what kind of response to return from requests that handle Precognition requests. All the Precog functionality works just fine, what I don't understand is what the request should return after saving the form submission. Ideally, I just want to return a 200 OK status so that the onSuccess handler does what it should do which is emit an event that the parent component is listening for then I want that component to do a partial reload of the page by only loading the comments (the controller is already set up to allow for optionally loading particular properties).

The problem I run into is that Inertia expects an Inertia response from the form request otherwise it pops open an error message window. So what kind of response should I return so that I am not rendering a page or performing a redirect that causes the entire page to reload? It does seem like the onSuccess callback is executing but I get a full page reload from the parent component even if I comment out the one line that appears in the onCommentPosted handler (see below). And that is probably because I am currently returning a redirect response just to avoid getting the error message about returning an invalid Inertia response.

Is there a way to return an Inertia response that is nothing more than empty JSON or just an empty response with a 200 status?

Samples of the Code

CommentForm.vue (child form component)

import { useForm } from 'laravel-precognition-vue-inertia';

...

const form = useForm('post', route('comments.create.submit', { task : props.task.uuid }), {
  body: ''
});

const submit = () => form.submit({
    preserveState: true,
    preserveScroll: true,
    onSuccess: () => {
        emit('comment-posted');
        form.reset();
    }
});

TaskDetail.vue (parent page component)

const onCommentPosted = () => {
    router.reload({ only: ['comments'] });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant