Repository: sveltejs/kit (JavaScript)

Issue:
Returning location from POST does not set 3xx status code
### Describe the bug

When returning a `location` path from a `+page.server.ts` sveltekit set status as 200, and does not perform a redirect.

The problem is located in `packages/kit/src/runtime/server/page/index.js` at 81:38

```ts
return redirect_response(status, result.location);
```

where status is not changed from 200 (at 57:5)

```ts
let status = 200;
```

I'd be willing to submit a PR to fix this issue.

### Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-t26lrz?file=src/routes/+page.svelte

The page contains two buttons:
- Submit form with `throw redirect()`
- Submit form with `location`

### Logs

_No response_

### System Info

```shell
System:
    OS: macOS 12.5
    CPU: (10) arm64 Apple M1 Pro
    Memory: 69.66 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.7.0 - ~/.nvm/versions/node/v18.7.0/bin/node
    npm: 8.15.0 - ~/.nvm/versions/node/v18.7.0/bin/npm
  Browsers:
    Firefox Developer Edition: 104.0
    Safari: 15.6
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.64 
    @sveltejs/adapter-node: 1.0.0-next.86 => 1.0.0-next.86 
    @sveltejs/kit: 1.0.0-next.415 => 1.0.0-next.415 
    svelte: ^3.49.0 => 3.49.0 
    vite: ^3.0.8 => 3.0.8
```


### Severity

annoyance

### Additional Information

_No response_


Discussion:
Feel free to open a PR! This should probably be a 302
Especially since it's in response to a POST, we should probably take a stand here and make a conscious decision between 303 and 307, rather than just a 302. A 302 is, in practice, treated as a 303, so I'd be in favor of using a 303.
Looks like this was merged 2 minutes ago, but I do wonder if 307 might be nicer at least in dev mode. When developing multiple apps it's really annoying to have redirects cached
303 vs 307 is whether the POSTs turn into GETs upon redirection, or whether the new URL is also POSTed to. Neither of them is 301 Moved Permanently.



Patch:
diff --git a/.changeset/gentle-news-shout.md b/.changeset/gentle-news-shout.md
new file mode 100644
index 000000000000..c5a3bcfda283
--- /dev/null
+++ b/.changeset/gentle-news-shout.md
@@ -0,0 +1,5 @@
+---
+'@sveltejs/kit': patch
+---
+
+Return a 303 response when a `POST` handler provides a `location`
diff --git a/packages/kit/src/runtime/server/page/index.js b/packages/kit/src/runtime/server/page/index.js
index 9840a417c660..9a1769bb03b8 100644
--- a/packages/kit/src/runtime/server/page/index.js
+++ b/packages/kit/src/runtime/server/page/index.js
@@ -77,7 +77,7 @@ export async function render_page(event, route, options, state, resolve_opts) {
 					}
 
 					if (event.request.method === 'POST' && result?.location) {
-						return redirect_response(status, result.location);
+						return redirect_response(303, result.location);
 					}
 				} else {
 					event.setHeaders({


Test patch:
diff --git a/packages/kit/test/apps/basics/src/routes/shadowed/post-success-redirect/+page.server.js b/packages/kit/test/apps/basics/src/routes/shadowed/post-success-redirect/+page.server.js
new file mode 100644
index 000000000000..30d0b0e3fb87
--- /dev/null
+++ b/packages/kit/test/apps/basics/src/routes/shadowed/post-success-redirect/+page.server.js
@@ -0,0 +1,5 @@
+export function POST() {
+	return {
+		location: '/shadowed/post-success-redirect/redirected'
+	};
+}
diff --git a/packages/kit/test/apps/basics/src/routes/shadowed/post-success-redirect/+page.svelte b/packages/kit/test/apps/basics/src/routes/shadowed/post-success-redirect/+page.svelte
new file mode 100644
index 000000000000..74dca32e8ce8
--- /dev/null
+++ b/packages/kit/test/apps/basics/src/routes/shadowed/post-success-redirect/+page.svelte
@@ -0,0 +1,3 @@
+<form method="POST">
+	<button>Click me</button>
+</form>
diff --git a/packages/kit/test/apps/basics/src/routes/shadowed/post-success-redirect/redirected/+page.svelte b/packages/kit/test/apps/basics/src/routes/shadowed/post-success-redirect/redirected/+page.svelte
new file mode 100644
index 000000000000..6ba792f15efc
--- /dev/null
+++ b/packages/kit/test/apps/basics/src/routes/shadowed/post-success-redirect/redirected/+page.svelte
@@ -0,0 +1,1 @@
+<h1>POST was successful</h1>
diff --git a/packages/kit/test/apps/basics/test/test.js b/packages/kit/test/apps/basics/test/test.js
index d03fa9600539..e54c9ec4905c 100644
--- a/packages/kit/test/apps/basics/test/test.js
+++ b/packages/kit/test/apps/basics/test/test.js
@@ -188,6 +188,12 @@ test.describe('Shadowed pages', () => {
 		);
 	});
 
+	test('Handles POST success with returned location', async ({ page }) => {
+		await page.goto('/shadowed/post-success-redirect');
+		await Promise.all([page.waitForNavigation(), page.click('button')]);
+		expect(await page.textContent('h1')).toBe('POST was successful');
+	});
+
 	test('Renders error page for 4xx and 5xx responses from GET', async ({ page, clicknav }) => {
 		await page.goto('/shadowed');
 		await clicknav('[href="/shadowed/error-get"]');