Cross-site POST form submissions are forbidden SvelteKit

January 15th 2025 11 views • 1 comments

I went through an error in my SvelteKit web app, that whenever I tried to POST request to the SvelteKit server, it returned a message of

Cross-site POST form submissions are forbidden SvelteKit

and I came to know that the issue was because of ORIGIN not be defined on the server.

So, below is the solution to this issue…

Firstly, I changed my adapter from auto to @sveltejs/adapter-node.

To change your adapter, you can go to svelte.config.js , because only with this adapter setting origin was possible …

import adapter from '@sveltejs/adapter-node';
import preprocess from 'svelte-preprocess';

/** @type {import('@sveltejs/kit').Config} */

const config = {
    preprocess: [
        preprocess({
                postcss: true
        })
    ],

    kit: {
        adapter: adapter()
    }
};

export default config;

after that, you have to build the app with npm run build

Now, you can run your server with

ORIGIN=https://yoursite.com node ./build/index.js

You can also set PORT and HOST if you want to.

Now, your site is ready to receive POST requests from your SvelteKit server.

And if you use pm2 to host your web app, you can try this:

ORIGIN=https://yoursite.com pm2 start your_svelte_pm2_name