Skip to content
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

POST request responds with pre-rendered page instead of a body in newer SvelteKit #4480

Closed
kvetoslavnovak opened this issue Apr 1, 2022 · 1 comment

Comments

@kvetoslavnovak
Copy link
Contributor

kvetoslavnovak commented Apr 1, 2022

Describe the bug

POST request returns pre-rendered page instead of response with the body in newer SvelteKit.In older version this worked just fine.

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-eds4xq?file=src/routes/index.svelte

or just use these two basic files for the project

// src/routes/index.svelte
<script>
  async function reqToPost() {
    const reqBodyToPost = {
      reqBodyToPost: "reqBodyToPost",
    };
    await fetch("/", {
      method: "POST",
      body: JSON.stringify(reqBodyToPost),
    });
  }
</script>

Sending request to POST endpoint in body

<button on:click={reqToPost}>reqToPost</button>
// src/routes/index.js
export async function post() {
	return {
		status: 200,
		body: {
			resBodyFromPost: 'resBodyFromPost'
		}
	};
}

Logs

1.0.0-next.192 etwork responds logs:

{"resBodyFromPost":"resBodyFromPost"}
1.0.0-next.304 network responds logs:
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8" />
		<meta name="description" content="" />
		<link rel="icon" href="./favicon.png" />
		<meta name="viewport" content="width=device-width, initial-scale=1" />
		
	</head>
	<body>
		<div>
// src/routes/index.svelte

Sending request to POST endpoint in body

<button>reqToPost</button>

		<script type="module" data-hydrate="1p3q768">
		import { start } from "/@fs/home/developer/Documents/EURLEX_PREPARATION/TEST/.svelte-kit/runtime/client/start.js";
		start({
			target: document.querySelector('[data-hydrate="1p3q768"]').parentNode,
			paths: {"base":"","assets":""},
			session: {},
			route: true,
			spa: false,
			trailing_slash: "never",
			hydrate: {
				status: 200,
				error: null,
				nodes: [
					import("/.svelte-kit/runtime/components/layout.svelte"),
						import("/src/routes/index.svelte")
				],
				params: {},
				routeId: ""
			}
		});
	</script><script type="application/json" sveltekit:data-type="props">{"resBodyFromPost":"resBodyFromPost"}</script></div>
	</body>
</html>

System Info

System:
    OS: Linux 5.16 Pop!_OS 21.10
    CPU: (4) x64 Intel(R) Pentium(R) CPU 3825U @ 1.90GHz
    Memory: 315.41 MB / 3.75 GB
    Container: Yes
    Shell: 5.1.8 - /bin/bash
  Binaries:
    Node: 16.13.2 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 8.1.2 - /usr/local/bin/npm
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.34 
    @sveltejs/kit: next => 1.0.0-next.304 
    svelte: ^3.44.0 => 3.46.6

Severity

blocking an upgrade

Additional Information

None

@kvetoslavnovak kvetoslavnovak changed the title POST request returns pre-rendered page instead of body in newer SvelteKit POST request responds with pre-rendered page instead of a body in newer SvelteKit Apr 1, 2022
@Conduitry
Copy link
Member

This is the intended behavior. Since #3679, endpoints with the same URL as a page are treated specially. See https://kit.svelte.dev/docs/routing#endpoints-page-endpoints

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

2 participants