Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove try-catch around server.respond #4738

Merged
merged 1 commit into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/brave-rabbits-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@sveltejs/adapter-cloudflare': patch
'@sveltejs/adapter-cloudflare-workers': patch
'@sveltejs/adapter-netlify': patch
---

[breaking] Remove try-catch around server.respond
45 changes: 18 additions & 27 deletions packages/adapter-cloudflare-workers/files/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,12 @@ export default {
}

// dynamically-generated pages
try {
return await server.respond(req, {
platform: { env, context },
getClientAddress() {
return req.headers.get('cf-connecting-ip');
}
});
} catch (e) {
return new Response('Error rendering route: ' + (e.message || e.toString()), { status: 500 });
}
return await server.respond(req, {
platform: { env, context },
getClientAddress() {
return req.headers.get('cf-connecting-ip');
}
});
}
};

Expand All @@ -79,24 +75,19 @@ export default {
* @param {any} context
*/
async function get_asset_from_kv(req, env, context, map = mapRequestToAsset) {
try {
return await getAssetFromKV(
{
request: req,
waitUntil(promise) {
return context.waitUntil(promise);
}
},
{
ASSET_NAMESPACE: env.__STATIC_CONTENT,
ASSET_MANIFEST: static_asset_manifest,
mapRequestToAsset: map
return await getAssetFromKV(
{
request: req,
waitUntil(promise) {
return context.waitUntil(promise);
}
);
} catch (e) {
const status = is_error(e.status) ? e.status : 500;
return new Response(e.message || e.toString(), { status });
}
},
{
ASSET_NAMESPACE: env.__STATIC_CONTENT,
ASSET_MANIFEST: static_asset_manifest,
mapRequestToAsset: map
}
);
}

/**
Expand Down
92 changes: 44 additions & 48 deletions packages/adapter-cloudflare/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,58 @@ const prefix = `/${manifest.appDir}/`;
/** @type {import('worktop/cfw').Module.Worker<{ ASSETS: import('worktop/cfw.durable').Durable.Object }>} */
const worker = {
async fetch(req, env, context) {
try {
// skip cache if "cache-control: no-cache" in request
let pragma = req.headers.get('cache-control') || '';
let res = !pragma.includes('no-cache') && (await Cache.lookup(req));
if (res) return res;
// skip cache if "cache-control: no-cache" in request
let pragma = req.headers.get('cache-control') || '';
let res = !pragma.includes('no-cache') && (await Cache.lookup(req));
if (res) return res;

let { pathname } = new URL(req.url);
let { pathname } = new URL(req.url);

// static assets
if (pathname.startsWith(prefix)) {
res = await env.ASSETS.fetch(req);
// static assets
if (pathname.startsWith(prefix)) {
res = await env.ASSETS.fetch(req);

res = new Response(res.body, {
headers: {
// include original cache headers, minus cache-control which
// is overridden, and etag which is no longer useful
'cache-control': 'public, immutable, max-age=31536000',
'content-type': res.headers.get('content-type'),
'x-robots-tag': 'noindex'
}
});
} else {
// prerendered pages and index.html files
pathname = pathname.replace(/\/$/, '') || '/';

let file = pathname.substring(1);

try {
file = decodeURIComponent(file);
} catch (err) {
// ignore
res = new Response(res.body, {
headers: {
// include original cache headers, minus cache-control which
// is overridden, and etag which is no longer useful
'cache-control': 'public, immutable, max-age=31536000',
'content-type': res.headers.get('content-type'),
'x-robots-tag': 'noindex'
}
});
} else {
// prerendered pages and index.html files
pathname = pathname.replace(/\/$/, '') || '/';

if (
manifest.assets.has(file) ||
manifest.assets.has(file + '/index.html') ||
prerendered.has(pathname)
) {
res = await env.ASSETS.fetch(req);
} else {
// dynamically-generated pages
res = await server.respond(req, {
platform: { env, context },
getClientAddress() {
return req.headers.get('cf-connecting-ip');
}
});
}
let file = pathname.substring(1);

try {
file = decodeURIComponent(file);
} catch (err) {
// ignore
}

// Writes to Cache only if allowed & specified
pragma = res.headers.get('cache-control');
return pragma ? Cache.save(req, res, context) : res;
} catch (e) {
return new Response('Error rendering route: ' + (e.message || e.toString()), { status: 500 });
if (
manifest.assets.has(file) ||
manifest.assets.has(file + '/index.html') ||
prerendered.has(pathname)
) {
res = await env.ASSETS.fetch(req);
} else {
// dynamically-generated pages
res = await server.respond(req, {
platform: { env, context },
getClientAddress() {
return req.headers.get('cf-connecting-ip');
}
});
}
}

// Writes to Cache only if allowed & specified
pragma = res.headers.get('cache-control');
return pragma ? Cache.save(req, res, context) : res;
}
};

Expand Down
22 changes: 8 additions & 14 deletions packages/adapter-netlify/src/edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,18 @@ const prefix = `/${manifest.appDir}/`;
* @param { any } context
* @returns { Promise<Response> }
*/
export default async function handler(request, context) {
export default function handler(request, context) {
if (is_static_file(request)) {
// Static files can skip the handler
return;
}
try {
const response = await server.respond(request, {
platform: { context },
getClientAddress() {
return request.headers.get('x-nf-client-connection-ip');
}
});
return response;
} catch (error) {
return new Response('Error rendering route:' + (error.message || error.toString()), {
status: 500
});
}

return server.respond(request, {
platform: { context },
getClientAddress() {
return request.headers.get('x-nf-client-connection-ip');
}
});
}

/**
Expand Down