Skip to content

Commit e37cbed

Browse files
Merge branch 'sveltejs:master' into master
2 parents ac35102 + dfae153 commit e37cbed

File tree

14 files changed

+77
-25
lines changed

14 files changed

+77
-25
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Set cookies when redirecting from shadow endpoint

.changeset/pre.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"clever-dolls-poke",
8888
"clever-donuts-smile",
8989
"clever-eagles-live",
90+
"clever-geckos-confess",
9091
"clever-lizards-grab",
9192
"clever-pillows-sing",
9293
"clever-readers-turn",

packages/kit/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @sveltejs/kit
22

3+
## 1.0.0-next.267
4+
5+
### Patch Changes
6+
7+
- Set cookies when redirecting from shadow endpoint ([#3874](https://github.com/sveltejs/kit/pull/3874))
8+
39
## 1.0.0-next.266
410

511
### Patch Changes

packages/kit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sveltejs/kit",
3-
"version": "1.0.0-next.266",
3+
"version": "1.0.0-next.267",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/sveltejs/kit",

packages/kit/src/runtime/server/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ export async function respond(request, options, state = {}) {
149149
const location = response.headers.get('location');
150150

151151
if (location) {
152+
const headers = new Headers(response.headers);
153+
headers.set('x-sveltekit-location', location);
152154
response = new Response(undefined, {
153155
status: 204,
154-
headers: {
155-
'x-sveltekit-location': location
156-
}
156+
headers
157157
});
158158
}
159159
}

packages/kit/src/runtime/server/page/load_node.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -396,22 +396,21 @@ async function load_shadow_data(route, event, prerender) {
396396
if (result.fallthrough) return result;
397397

398398
const { status, headers, body } = validate_shadow_output(result);
399+
data.status = status;
400+
399401
add_cookies(/** @type {string[]} */ (data.cookies), headers);
400402

401403
// Redirects are respected...
402404
if (status >= 300 && status < 400) {
403-
return {
404-
status,
405-
redirect: /** @type {string} */ (
406-
headers instanceof Headers ? headers.get('location') : headers.location
407-
)
408-
};
405+
data.redirect = /** @type {string} */ (
406+
headers instanceof Headers ? headers.get('location') : headers.location
407+
);
408+
return data;
409409
}
410410

411411
// ...but 4xx and 5xx status codes _don't_ result in the error page
412412
// rendering for non-GET requests — instead, we allow the page
413413
// to render with any validation errors etc that were returned
414-
data.status = status;
415414
data.body = body;
416415
}
417416

@@ -422,21 +421,18 @@ async function load_shadow_data(route, event, prerender) {
422421

423422
const { status, headers, body } = validate_shadow_output(result);
424423
add_cookies(/** @type {string[]} */ (data.cookies), headers);
424+
data.status = status;
425425

426426
if (status >= 400) {
427-
return {
428-
status,
429-
error: new Error('Failed to load data')
430-
};
427+
data.error = new Error('Failed to load data');
428+
return data;
431429
}
432430

433431
if (status >= 300) {
434-
return {
435-
status,
436-
redirect: /** @type {string} */ (
437-
headers instanceof Headers ? headers.get('location') : headers.location
438-
)
439-
};
432+
data.redirect = /** @type {string} */ (
433+
headers instanceof Headers ? headers.get('location') : headers.location
434+
);
435+
return data;
440436
}
441437

442438
data.body = { ...body, ...data.body };
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
<a href="/shadowed/simple">simple</a>
22
<a href="/shadowed/redirect-get">redirect-get</a>
3+
<a href="/shadowed/redirect-get-with-cookie">redirect-get-with-cookie</a>
34
<a href="/shadowed/error-get">error-get</a>
45

56
<form action="/shadowed/redirect-post" method="post">
67
<button type="submit" id="redirect-post">redirect</button>
78
</form>
89

10+
<form action="/shadowed/redirect-post-with-cookie" method="post">
11+
<button type="submit" id="redirect-post-with-cookie">redirect</button>
12+
</form>
13+
914
<form action="/shadowed/error-post" method="post">
1015
<button type="submit" id="error-post">error</button>
1116
</form>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function get() {
2+
return {
3+
status: 302,
4+
headers: {
5+
location: '/shadowed/redirected',
6+
'set-cookie': 'shadow-redirect=happy'
7+
}
8+
};
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>This should not be visible</h1>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function post() {
2+
return {
3+
status: 302,
4+
headers: {
5+
location: '/shadowed/redirected',
6+
'set-cookie': 'shadow-redirect=happy'
7+
}
8+
};
9+
}

0 commit comments

Comments
 (0)