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 prerendered page pathname for Cloudflare adapter
31 changes: 18 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
// immutable app assets
eltigerchino marked this conversation as resolved.
Show resolved Hide resolved
if (pathname.startsWith(app_path)) {
res = await env.ASSETS.fetch(req);
if (!res.ok) return res;
Expand All @@ -36,26 +37,30 @@ 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
}

if (
manifest.assets.has(file) ||
manifest.assets.has(file + '/index.html') ||
prerendered.has(pathname)
) {
const stripped_pathname = pathname.replace(/\/$/, '') || '';
eltigerchino marked this conversation as resolved.
Show resolved Hide resolved

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could there theoretically be an asset named index.html? What was the reason for changing this?

Copy link
Member Author

@eltigerchino eltigerchino Jan 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could there theoretically be an asset named index.html?

If a dev decides to include one in /static I suppose. It helps if someone navigates to that route.
This check was already there previously, but I suppose it's good to have it since the worker has to handle all file routing.

What was the reason for changing this?

I thought it would be a small optimisation, although I'm unsure if it has an impact.

If the url is https://test.com/, then the resulting filename would be ''. This addition helps skip the manifest check for an empty string.


const is_prerendered = prerendered.has(pathname) || prerendered.has(stripped_pathname);
eltigerchino marked this conversation as resolved.
Show resolved Hide resolved

if (is_asset || is_prerendered) {
res = await env.ASSETS.fetch(req);
} 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"]
}