Skip to content

Commit

Permalink
Merge branch 'sveltejs:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
isaac-mcfadyen authored Feb 13, 2022
2 parents ac35102 + dfae153 commit e37cbed
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-geckos-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Set cookies when redirecting from shadow endpoint
1 change: 1 addition & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"clever-dolls-poke",
"clever-donuts-smile",
"clever-eagles-live",
"clever-geckos-confess",
"clever-lizards-grab",
"clever-pillows-sing",
"clever-readers-turn",
Expand Down
6 changes: 6 additions & 0 deletions packages/kit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @sveltejs/kit

## 1.0.0-next.267

### Patch Changes

- Set cookies when redirecting from shadow endpoint ([#3874](https://github.com/sveltejs/kit/pull/3874))

## 1.0.0-next.266

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sveltejs/kit",
"version": "1.0.0-next.266",
"version": "1.0.0-next.267",
"repository": {
"type": "git",
"url": "https://github.com/sveltejs/kit",
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ export async function respond(request, options, state = {}) {
const location = response.headers.get('location');

if (location) {
const headers = new Headers(response.headers);
headers.set('x-sveltekit-location', location);
response = new Response(undefined, {
status: 204,
headers: {
'x-sveltekit-location': location
}
headers
});
}
}
Expand Down
30 changes: 13 additions & 17 deletions packages/kit/src/runtime/server/page/load_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,22 +396,21 @@ async function load_shadow_data(route, event, prerender) {
if (result.fallthrough) return result;

const { status, headers, body } = validate_shadow_output(result);
data.status = status;

add_cookies(/** @type {string[]} */ (data.cookies), headers);

// Redirects are respected...
if (status >= 300 && status < 400) {
return {
status,
redirect: /** @type {string} */ (
headers instanceof Headers ? headers.get('location') : headers.location
)
};
data.redirect = /** @type {string} */ (
headers instanceof Headers ? headers.get('location') : headers.location
);
return data;
}

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

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

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

if (status >= 400) {
return {
status,
error: new Error('Failed to load data')
};
data.error = new Error('Failed to load data');
return data;
}

if (status >= 300) {
return {
status,
redirect: /** @type {string} */ (
headers instanceof Headers ? headers.get('location') : headers.location
)
};
data.redirect = /** @type {string} */ (
headers instanceof Headers ? headers.get('location') : headers.location
);
return data;
}

data.body = { ...body, ...data.body };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<a href="/shadowed/simple">simple</a>
<a href="/shadowed/redirect-get">redirect-get</a>
<a href="/shadowed/redirect-get-with-cookie">redirect-get-with-cookie</a>
<a href="/shadowed/error-get">error-get</a>

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

<form action="/shadowed/redirect-post-with-cookie" method="post">
<button type="submit" id="redirect-post-with-cookie">redirect</button>
</form>

<form action="/shadowed/error-post" method="post">
<button type="submit" id="error-post">error</button>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function get() {
return {
status: 302,
headers: {
location: '/shadowed/redirected',
'set-cookie': 'shadow-redirect=happy'
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>This should not be visible</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function post() {
return {
status: 302,
headers: {
location: '/shadowed/redirected',
'set-cookie': 'shadow-redirect=happy'
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>This should not be visible</h1>
22 changes: 22 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,34 @@ test.describe.parallel('Shadowed pages', () => {
expect(await page.textContent('h1')).toBe('Redirection was successful');
});

test('Handles GET redirects with cookies', async ({ page, context, clicknav }) => {
await page.goto('/shadowed');
await clicknav('[href="/shadowed/redirect-get-with-cookie"]');
expect(await page.textContent('h1')).toBe('Redirection was successful');

const cookies = await context.cookies();
expect(cookies).toEqual(
expect.arrayContaining([expect.objectContaining({ name: 'shadow-redirect', value: 'happy' })])
);
});

test('Handles POST redirects', async ({ page }) => {
await page.goto('/shadowed');
await Promise.all([page.waitForNavigation(), page.click('#redirect-post')]);
expect(await page.textContent('h1')).toBe('Redirection was successful');
});

test('Handles POST redirects with cookies', async ({ page, context }) => {
await page.goto('/shadowed');
await Promise.all([page.waitForNavigation(), page.click('#redirect-post-with-cookie')]);
expect(await page.textContent('h1')).toBe('Redirection was successful');

const cookies = await context.cookies();
expect(cookies).toEqual(
expect.arrayContaining([expect.objectContaining({ name: 'shadow-redirect', value: 'happy' })])
);
});

test('Renders error page for 4xx and 5xx responses from GET', async ({ page, clicknav }) => {
await page.goto('/shadowed');
await clicknav('[href="/shadowed/error-get"]');
Expand Down
3 changes: 0 additions & 3 deletions sites/kit.svelte.dev/src/lib/docs/Contents.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
// wait for fonts to load...
const timeouts = [setTimeout(onresize, 1000), setTimeout(onscroll, 5000)];
update();
highlight();
return () => {
window.removeEventListener('scroll', onscroll, true);
window.removeEventListener('resize', onresize, true);
Expand Down
2 changes: 1 addition & 1 deletion sites/kit.svelte.dev/src/lib/search/SearchBox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@

<svelte:window
on:keydown={(e) => {
if (e.code === 'KeyK' && (navigator.platform === 'MacIntel' ? e.metaKey : e.ctrlKey)) {
if (e.key === 'k' && (navigator.platform === 'MacIntel' ? e.metaKey : e.ctrlKey)) {
e.preventDefault();
$query = '';
$searching = !$searching;
Expand Down

0 comments on commit e37cbed

Please sign in to comment.