Skip to content

Commit

Permalink
fix: correctly check URL pathname with Cloudflare adapter (#8733)
Browse files Browse the repository at this point in the history
* add trailing slash check for prerendered files

* check URI decoded prerendered pathname

* correctly add placeholders.ts to tsconfig.json

* correct changeset

* cleanup

* rename generated files comment

Co-authored-by: Rich Harris <[email protected]>

* rewrite changeset

* add redirect to counterpart route

* add check for empty counterpart route string

* change redirect status to 308 to match adapter-vercel

---------

Co-authored-by: Simon H <[email protected]>
Co-authored-by: Rich Harris <[email protected]>
  • Loading branch information
3 people authored Jan 30, 2023
1 parent a5f9f41 commit 32cbe07
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
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"]
}

0 comments on commit 32cbe07

Please sign in to comment.