-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
chore(cloudflare): optimize Cache usage; strict TS source #4453
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4ae2d71
chore(cloudflare): optimize Cache usage; strict TS source
lukeed 5c15fee
chore: add changeset
lukeed 2ce5407
skip lib check for conflicts
lukeed 77702f9
bundle worktop (#4473)
Rich-Harris 0185508
convert to JS (#4474)
Rich-Harris c0724b2
Update .changeset/empty-falcons-run.md
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 | ||
| --- | ||
|
|
||
| Convert entry to TypeScript; check for Cache match sooner; use `worktop` for types & Cache operations | ||
This file contains hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
| import { Server } from 'SERVER'; | ||
| import { manifest, prerendered } from 'MANIFEST'; | ||
| import * as Cache from 'worktop/cfw.cache'; | ||
|
|
||
| import type { Module, Bindings } from 'worktop/cfw'; | ||
| import type { Durable } from 'worktop/cfw.durable'; | ||
|
|
||
| interface Environment extends Bindings { | ||
| ASSETS: Durable.Object; | ||
| } | ||
|
|
||
| const server = new Server(manifest); | ||
|
|
||
| const prefix = `/${manifest.appDir}/`; | ||
|
|
||
| const worker: Module.Worker<Environment> = { | ||
| 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.