-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(cloudflare): optimize Cache usage; strict TS source (#4453)
* chore(cloudflare): optimize Cache usage; strict TS source * chore: add changeset * skip lib check for conflicts * bundle worktop (#4473) * bundle worktop * build on prepublishOnly * Update packages/adapter-cloudflare/tsconfig.json * update builder file paths Co-authored-by: Luke Edwards <luke.edwards05@gmail.com> * convert to JS (#4474) * Update .changeset/empty-falcons-run.md Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
- Loading branch information
1 parent
a11bae8
commit 316943f
Showing
7 changed files
with
96 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/adapter-cloudflare': patch | ||
--- | ||
|
||
Check for Cache match sooner; use `worktop` for types & Cache operations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
.DS_Store | ||
node_modules | ||
target | ||
/files |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { Server } from 'SERVER'; | ||
import { manifest, prerendered } from 'MANIFEST'; | ||
import * as Cache from 'worktop/cfw.cache'; | ||
|
||
const server = new Server(manifest); | ||
|
||
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 { | ||
let res = await Cache.lookup(req); | ||
if (res) return res; | ||
|
||
let { pathname } = new URL(req.url); | ||
|
||
// 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 | ||
} | ||
|
||
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 | ||
return Cache.save(req, res, context); | ||
} catch (e) { | ||
return new Response('Error rendering route: ' + (e.message || e.toString()), { status: 500 }); | ||
} | ||
} | ||
}; | ||
|
||
export default worker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.