Skip to content

$app/forms enhance/ApplyAction does not handle endpoints that redirect #7932

@jerrythomas

Description

@jerrythomas

Describe the bug

Assuming that POST Endpoints (+server.js) or actions (+page.server.js) can both be used for the actions on forms.

api/+server.js

import { redirect } from '@sveltejs/kit'

/** @type {import('./$types').RequestHandler} */
export async function POST(event) {
	console.log('endpoint', event.url.pathname, event.request.method)
	throw redirect(303, '/')
}

form/+page.server.js

import { redirect } from '@sveltejs/kit'

/** @type {import (./$types).Actions} */
export const actions = {
	async default(event) {
		console.log('action', event.url.pathname, event.request.method)
		throw redirect(303, '/')
	}
}

Both of the above two seem to be identical, however the result of using the enhance function on the action path results in

{
    "type": "redirect",
    "status": 303,
    "location": "/"
}

and using it on the endpoint path results in

{
    "type": "error",
    "error": "SyntaxError: Unexpected token '<', \"<!DOCTYPE \"... is not valid JSON
    at JSON.parse (<anonymous>)
    at deserialize (http://localhost:5173/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@sveltejs/kit/src/runtime/app/forms.js?v=5b196e18:21:22)
    at HTMLFormElement.handle_submit (http://localhost:5173/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@sveltejs/kit/src/runtime/app/forms.js?v=5b196e18:106:13)"
}

Shouldn't either of them result in the response to be redirect.

The following error occurs when a ApplyAction is used to make a post request to an endpoint that redirects to some other page.

500
Unexpected token '<', "<!DOCTYPE "... is not valid JSON
SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
    at JSON.parse (<anonymous>)
    at deserialize (http://localhost:5173/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@sveltejs/kit/src/runtime/app/forms.js?v=5b196e18:21:22)
    at HTMLFormElement.handle_submit (http://localhost:5173/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@sveltejs/kit/src/runtime/app/forms.js?v=5b196e18:106:13)

This error does not occur with vanilla post method on the form.

Reproduction

https://github.com/jerrythomas/repro-enhance-apply

Logs

No response

System Info

System:
    OS: macOS 13.0.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 282.48 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.15.0 - ~/.nvm/versions/node/v16.15.0/bin/node
    npm: 8.5.5 - ~/.nvm/versions/node/v16.15.0/bin/npm
  Browsers:
    Brave Browser: 107.1.45.133
    Chrome: 108.0.5359.94
    Firefox: 106.0.5
    Safari: 16.1
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.90 
    @sveltejs/kit: next => 1.0.0-next.571 
    svelte: ^3.53.1 => 3.53.1 
    vite: ^3.2.4 => 3.2.4

Severity

serious, but I can work around it

Additional Information

I have been trying out auth-helpers from supabase for svelte. It works great but I feel that it might be easier to prepackage some of the endpoints (like sign in, sign out & handle protected routes) using the handle functions and the sequence helper. I hit this snag while working with this experiment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions