-
-
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.
Merge branch 'master' into js-typechecking
- Loading branch information
Showing
20 changed files
with
211 additions
and
425 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/kit': patch | ||
--- | ||
|
||
Custom `load` `dependencies` in `LoadOutput` |
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,12 @@ | ||
--- | ||
'@sveltejs/adapter-cloudflare-workers': patch | ||
--- | ||
|
||
[Breaking] refactor implementation from "Service Worker" pattern to "Module Worker" used in adapter-cloudflare | ||
|
||
#### add the following to your wrangler.toml | ||
```toml | ||
[build.upload] | ||
format = "modules" | ||
main = "./worker.mjs" | ||
``` |
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
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
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,61 +1,101 @@ | ||
import { Server } from 'SERVER'; | ||
import { manifest, prerendered } from 'MANIFEST'; | ||
import { getAssetFromKV } from '@cloudflare/kv-asset-handler'; | ||
import static_asset_manifest_json from '__STATIC_CONTENT_MANIFEST'; | ||
const static_asset_manifest = JSON.parse(static_asset_manifest_json); | ||
|
||
const server = new Server(manifest); | ||
|
||
const prefix = `/${manifest.appDir}/`; | ||
|
||
addEventListener('fetch', (/** @type {FetchEvent} */ event) => { | ||
event.respondWith(handle(event)); | ||
}); | ||
export default { | ||
/** | ||
* @param {Request} req | ||
* @param {any} env | ||
* @param {any} context | ||
*/ | ||
async fetch(req, env, context) { | ||
const url = new URL(req.url); | ||
|
||
/** | ||
* @param {FetchEvent} event | ||
* @returns {Promise<Response>} | ||
*/ | ||
async function handle(event) { | ||
const { request } = event; | ||
|
||
const url = new URL(request.url); | ||
|
||
// generated assets | ||
if (url.pathname.startsWith(prefix)) { | ||
const res = await getAssetFromKV(event); | ||
return new Response(res.body, { | ||
headers: { | ||
'cache-control': 'public, immutable, max-age=31536000', | ||
'content-type': res.headers.get('content-type') | ||
// static assets | ||
if (url.pathname.startsWith(prefix)) { | ||
/** @type {Response} */ | ||
const res = await get_asset_from_kv(req, env, context); | ||
if (is_error(res.status)) { | ||
return res; | ||
} | ||
}); | ||
} | ||
return 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' | ||
} | ||
}); | ||
} | ||
|
||
// prerendered pages and index.html files | ||
const pathname = url.pathname.replace(/\/$/, ''); | ||
let file = pathname.substring(1); | ||
// prerendered pages and index.html files | ||
const pathname = url.pathname.replace(/\/$/, ''); | ||
let file = pathname.substring(1); | ||
|
||
try { | ||
file = decodeURIComponent(file); | ||
} catch (err) { | ||
// ignore | ||
} | ||
try { | ||
file = decodeURIComponent(file); | ||
} catch (err) { | ||
// ignore | ||
} | ||
|
||
if ( | ||
manifest.assets.has(file) || | ||
manifest.assets.has(file + '/index.html') || | ||
prerendered.has(pathname || '/') | ||
) { | ||
return get_asset_from_kv(req, env, context); | ||
} | ||
|
||
if ( | ||
manifest.assets.has(file) || | ||
manifest.assets.has(file + '/index.html') || | ||
prerendered.has(pathname || '/') | ||
) { | ||
return await getAssetFromKV(event); | ||
// dynamically-generated pages | ||
try { | ||
return await server.respond(req, { | ||
platform: { env, context }, | ||
getClientAddress() { | ||
return req.headers.get('cf-connecting-ip'); | ||
} | ||
}); | ||
} catch (e) { | ||
return new Response('Error rendering route: ' + (e.message || e.toString()), { status: 500 }); | ||
} | ||
} | ||
}; | ||
|
||
// dynamically-generated pages | ||
/** | ||
* @param {Request} req | ||
* @param {any} env | ||
* @param {any} context | ||
*/ | ||
async function get_asset_from_kv(req, env, context) { | ||
try { | ||
return await server.respond(request, { | ||
getClientAddress() { | ||
return request.headers.get('cf-connecting-ip'); | ||
return await getAssetFromKV( | ||
{ | ||
request: req, | ||
waitUntil(promise) { | ||
return context.waitUntil(promise); | ||
} | ||
}, | ||
{ | ||
ASSET_NAMESPACE: env.__STATIC_CONTENT, | ||
ASSET_MANIFEST: static_asset_manifest | ||
} | ||
}); | ||
); | ||
} catch (e) { | ||
return new Response('Error rendering route:' + (e.message || e.toString()), { status: 500 }); | ||
const status = is_error(e.status) ? e.status : 500; | ||
return new Response(e.message || e.toString(), { status }); | ||
} | ||
} | ||
|
||
/** | ||
* @param {number} status | ||
* @returns {boolean} | ||
*/ | ||
function is_error(status) { | ||
return status > 399; | ||
} |
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
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
Oops, something went wrong.