Skip to content

Commit

Permalink
fixes #8209 - redirects from actions don't honor the provided status …
Browse files Browse the repository at this point in the history
…code.
  • Loading branch information
coryvirok committed Dec 18, 2022
1 parent 33dd655 commit c720293
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export async function render_page(event, route, page, options, state, resolve_op
// (this also determines status code)
action_result = await handle_action_request(event, leaf_node.server);
if (action_result?.type === 'redirect') {
return redirect_response(303, action_result.location);
return redirect_response(action_result.status ?? 303, action_result.location);
}
if (action_result?.type === 'error') {
const error = action_result.error;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { redirect } from '@sveltejs/kit';

/** @type {import('./$types').PageServerLoad} */
export function load() {
return {
initial: 'initial'
};
}

/** @type {import('./$types').Actions} */
export const actions = {
redirect_302: async () => {
throw redirect(302, '/actions/redirect/basic?redirected');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<form method="post" action="/actions/redirect/basic?/redirect_302">
<button id="submit">Submit</button>
</form>
15 changes: 15 additions & 0 deletions packages/kit/test/apps/basics/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,18 @@ test.describe('Miscellaneous', () => {
expect(headers['cache-control'] || '').not.toContain('immutable');
});
});

test.describe('Proper redirect status code', () => {
test('Action redirect is correct when JS is disabled', async ({ page, baseURL, request }) => {
await page.goto('/actions/redirect/basic');
const respProm = page.waitForResponse((resp) =>
resp.url().includes('/actions/redirect/basic?redirected')
);
await page.locator('#submit').click();

const resp = await respProm;
const redirectedFrom = await resp.request().redirectedFrom().response();
expect(redirectedFrom.status()).toBe(302);
expect(redirectedFrom.headers()['location']).toEqual('/actions/redirect/basic?redirected');
});
});

0 comments on commit c720293

Please sign in to comment.