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

fix: correctly check URL pathname with Cloudflare adapter #8733

Merged
merged 11 commits into from
Jan 30, 2023
5 changes: 5 additions & 0 deletions .changeset/chilly-bears-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare': patch
---

fix: correctly check URL pathname with Cloudflare adapter
39 changes: 26 additions & 13 deletions packages/adapter-cloudflare/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const app_path = `/${manifest.appPath}/`;
/** @type {import('worktop/cfw').Module.Worker<{ ASSETS: import('worktop/cfw.durable').Durable.Object }>} */
const worker = {
async fetch(req, env, context) {
// @ts-ignore
await server.init({ env });
// skip cache if "cache-control: no-cache" in request
let pragma = req.headers.get('cache-control') || '';
Expand All @@ -17,7 +18,7 @@ const worker = {

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

// static assets
// generated files
if (pathname.startsWith(app_path)) {
res = await env.ASSETS.fetch(req);
if (!res.ok) return res;
Expand All @@ -36,26 +37,38 @@ const worker = {
}
});
} else {
// prerendered pages and index.html files
pathname = pathname.replace(/\/$/, '') || '/';

let file = pathname.substring(1);
// prerendered pages and /static files

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

const stripped_pathname = pathname.replace(/\/$/, '');

let is_static_asset = false;
const filename = stripped_pathname.substring(1);
if (filename) {
is_static_asset =
manifest.assets.has(filename) || manifest.assets.has(filename + '/index.html');
}

if (
manifest.assets.has(file) ||
manifest.assets.has(file + '/index.html') ||
prerendered.has(pathname)
) {
const counterpart_route = pathname.at(-1) === '/' ? stripped_pathname : pathname + '/';

if (is_static_asset || prerendered.has(pathname)) {
res = await env.ASSETS.fetch(req);
} else if (counterpart_route && prerendered.has(counterpart_route)) {
res = new Response('', {
status: 308,
headers: {
location: counterpart_route
}
});
} else {
// dynamically-generated pages
res = await server.respond(req, {
// @ts-ignore
platform: { env, context, caches },
getClientAddress() {
return req.headers.get('cf-connecting-ip');
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-cloudflare/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"@sveltejs/kit": ["../kit/types/index"]
}
},
"include": ["index.js", "placeholders.ts", "src/worker.ts"]
"include": ["index.js", "placeholders.ts", "src/worker.js"]
}