Skip to content

Commit

Permalink
Read mode/credentials from init object, not request (#7453)
Browse files Browse the repository at this point in the history
* [fix] fetch erroring on Cloudflare

Fixes #6739

* alternative to #7192

Co-authored-by: The Noah <thenoahbz@gmail.com>
  • Loading branch information
Rich-Harris and The-Noah authored Oct 31, 2022
1 parent 24561fb commit 9f57ff3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-tables-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] fetch erroring on Cloudflare
27 changes: 18 additions & 9 deletions packages/kit/src/runtime/server/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ import { respond } from './index.js';
*/
export function create_fetch({ event, options, state, get_cookie_header }) {
return async (info, init) => {
const request = normalize_fetch_input(info, init, event.url);
const original_request = normalize_fetch_input(info, init, event.url);

const request_body = init?.body;

// some runtimes (e.g. Cloudflare) error if you access `request.mode`,
// annoyingly, so we need to read the value from the `init` object instead
let mode = (info instanceof Request ? info.mode : init?.mode) ?? 'cors';
let credentials =
(info instanceof Request ? info.credentials : init?.credentials) ?? 'same-origin';

return await options.hooks.handleFetch({
event,
request,
request: original_request,
fetch: async (info, init) => {
const request = normalize_fetch_input(info, init, event.url);

Expand All @@ -28,10 +34,16 @@ export function create_fetch({ event, options, state, get_cookie_header }) {
request.headers.set('origin', event.url.origin);
}

if (info !== original_request) {
mode = (info instanceof Request ? info.mode : init?.mode) ?? 'cors';
credentials =
(info instanceof Request ? info.credentials : init?.credentials) ?? 'same-origin';
}

// Remove Origin, according to https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin#description
if (
(request.method === 'GET' || request.method === 'HEAD') &&
((request.mode === 'no-cors' && url.origin !== event.url.origin) ||
((mode === 'no-cors' && url.origin !== event.url.origin) ||
url.origin === event.url.origin)
) {
request.headers.delete('origin');
Expand All @@ -46,17 +58,14 @@ export function create_fetch({ event, options, state, get_cookie_header }) {
// - sub.my.domain.com WILL receive cookies
// ports do not affect the resolution
// leading dot prevents mydomain.com matching domain.com
if (
`.${url.hostname}`.endsWith(`.${event.url.hostname}`) &&
request.credentials !== 'omit'
) {
if (`.${url.hostname}`.endsWith(`.${event.url.hostname}`) && credentials !== 'omit') {
const cookie = get_cookie_header(url, request.headers.get('cookie'));
if (cookie) request.headers.set('cookie', cookie);
}

let response = await fetch(request);

if (request.mode === 'no-cors') {
if (mode === 'no-cors') {
response = new Response('', {
status: response.status,
statusText: response.statusText,
Expand Down Expand Up @@ -109,7 +118,7 @@ export function create_fetch({ event, options, state, get_cookie_header }) {
return await fetch(request);
}

if (request.credentials !== 'omit') {
if (credentials !== 'omit') {
const cookie = get_cookie_header(url, request.headers.get('cookie'));
if (cookie) {
request.headers.set('cookie', cookie);
Expand Down

0 comments on commit 9f57ff3

Please sign in to comment.