admin管理员组

文章数量:1327843

I have a SvelteKit application, just following the example from the docs /tutorial/named-form-actions, the problem is that everything works until I try to write an action:

at: +page.server.js

export const actions = {
    default: async () => {
        console.log('test')
    }
};

vite immediately fails with: "Cannot prerender pages with actions"

Error: Cannot prerender pages with actions
    at render_page (file:///mydir/node_modules/@sveltejs/kit/src/runtime/server/page/index.js:87:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async resolve (file:///mydir/node_modules/@sveltejs/kit/src/runtime/server/index.js:356:17)
    at async respond (file:///mydir/node_modules/@sveltejs/kit/src/runtime/server/index.js:229:20)
    at async file:///mydir/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:444:22

Probably I'm missing some configuration or forgot some basics, any idea?

I have a SvelteKit application, just following the example from the docs https://learn.svelte.dev/tutorial/named-form-actions, the problem is that everything works until I try to write an action:

at: +page.server.js

export const actions = {
    default: async () => {
        console.log('test')
    }
};

vite immediately fails with: "Cannot prerender pages with actions"

Error: Cannot prerender pages with actions
    at render_page (file:///mydir/node_modules/@sveltejs/kit/src/runtime/server/page/index.js:87:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async resolve (file:///mydir/node_modules/@sveltejs/kit/src/runtime/server/index.js:356:17)
    at async respond (file:///mydir/node_modules/@sveltejs/kit/src/runtime/server/index.js:229:20)
    at async file:///mydir/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:444:22

Probably I'm missing some configuration or forgot some basics, any idea?

Share Improve this question edited Feb 4, 2023 at 10:53 Gudarzi 5753 gold badges8 silver badges26 bronze badges asked Jan 8, 2023 at 1:06 Ziur OlpaZiur Olpa 2,1332 gold badges17 silver badges33 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

The docs simply state:

Pages with actions cannot be prerendered, because a server must be able to handle the action POST requests.

The assumption is probably that a form action should have an effect on the page when submitted, which would not be possible when a static HTML page is served every time.

You could try to separate any logic to an API endpoint that is not associated with your prerendered page. It depends on what you are trying to do here, maybe the page should simply not be prerendered at all.

This means that +page.ts should set:

export const prerender = false;

本文标签: javascriptWhy actions in SveltKit give quotError Cannot prerender pages with actionsquotStack Overflow