Skip to content

Commit

Permalink
return 308s instead of 301s when normalising paths (#9351)
Browse files Browse the repository at this point in the history
* return 308s instead of 301s when normalising paths - fixes #9350

* remove only
  • Loading branch information
Rich-Harris authored Mar 7, 2023
1 parent 6a91e88 commit 8114c65
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/dirty-pianos-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: use 308 responses for trailing slash redirects, instead of 301s
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export async function respond(request, options, manifest, state) {

if (normalized !== url.pathname && !state.prerendering?.fallback) {
return new Response(undefined, {
status: 301,
status: 308,
headers: {
'x-sveltekit-normalize': '1',
location:
Expand Down
11 changes: 11 additions & 0 deletions packages/kit/test/apps/options/test/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as http from 'node:http';
import { expect } from '@playwright/test';
import { test } from '../../../utils.js';

Expand Down Expand Up @@ -173,6 +174,16 @@ test.describe('env', () => {

test.describe('trailingSlash', () => {
test('adds trailing slash', async ({ baseURL, page, clicknav }) => {
// we can't use Playwright's `request` here, because it resolves redirects
const status = await new Promise((fulfil, reject) => {
const request = http.get(`${baseURL}/path-base/slash`);
request.on('error', reject);
request.on('response', (response) => {
fulfil(response.statusCode);
});
});
expect(status).toBe(308);

await page.goto('/path-base/slash');

expect(page.url()).toBe(`${baseURL}/path-base/slash/`);
Expand Down

0 comments on commit 8114c65

Please sign in to comment.