@sveltejs/adapter-cloudflare-workers
Advanced tools
Comparing version 1.0.0-next.28 to 1.0.0-next.29
import { App } from 'APP'; | ||
import { manifest } from './manifest.js'; | ||
import { getAssetFromKV, NotFoundError } from '@cloudflare/kv-asset-handler'; | ||
import { manifest, prerendered } from './manifest.js'; | ||
import { getAssetFromKV } from '@cloudflare/kv-asset-handler'; | ||
const app = new App(manifest); | ||
addEventListener('fetch', (event) => { | ||
const prefix = `/${manifest.appDir}/`; | ||
addEventListener('fetch', (/** @type {FetchEvent} */ event) => { | ||
event.respondWith(handle(event)); | ||
}); | ||
/** | ||
* @param {FetchEvent} event | ||
* @returns {Promise<Response>} | ||
*/ | ||
async function handle(event) { | ||
// try static files first | ||
if (event.request.method == 'GET') { | ||
try { | ||
// TODO rather than attempting to get an asset, | ||
// use the asset manifest to see if it exists | ||
return await getAssetFromKV(event); | ||
} catch (e) { | ||
if (!(e instanceof NotFoundError)) { | ||
return new Response('Error loading static asset:' + (e.message || e.toString()), { | ||
status: 500 | ||
}); | ||
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') | ||
} | ||
} | ||
}); | ||
} | ||
// fall back to an app route | ||
const request = event.request; | ||
// prerendered pages and index.html files | ||
const pathname = url.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 || '/') | ||
) { | ||
return await getAssetFromKV(event); | ||
} | ||
// dynamically-generated pages | ||
try { | ||
const rendered = await app.render({ | ||
@@ -41,3 +63,3 @@ url: request.url, | ||
status: rendered.status, | ||
headers: makeHeaders(rendered.headers) | ||
headers: make_headers(rendered.headers) | ||
}); | ||
@@ -49,3 +71,3 @@ } | ||
return new Response({ | ||
return new Response('Not Found', { | ||
status: 404, | ||
@@ -61,7 +83,4 @@ statusText: 'Not Found' | ||
/** | ||
* @param {Record<string, string | string[]>} headers | ||
* @returns {Request} | ||
*/ | ||
function makeHeaders(headers) { | ||
/** @param {Record<string, string | string[]>} headers */ | ||
function make_headers(headers) { | ||
const result = new Headers(); | ||
@@ -68,0 +87,0 @@ for (const header in headers) { |
@@ -26,3 +26,3 @@ import { existsSync, readFileSync, writeFileSync } from 'fs'; | ||
builder.log.info('Prerendering static pages...'); | ||
await builder.prerender({ | ||
const { paths } = await builder.prerender({ | ||
dest: bucket | ||
@@ -51,3 +51,3 @@ }); | ||
relativePath | ||
})};\n` | ||
})};\n\nexport const prerendered = new Set(${JSON.stringify(paths)});\n` | ||
); | ||
@@ -72,2 +72,3 @@ | ||
/** @param {import('@sveltejs/kit').Builder} builder */ | ||
function validate_config(builder) { | ||
@@ -74,0 +75,0 @@ if (existsSync('wrangler.toml')) { |
{ | ||
"name": "@sveltejs/adapter-cloudflare-workers", | ||
"version": "1.0.0-next.28", | ||
"version": "1.0.0-next.29", | ||
"repository": { | ||
@@ -29,3 +29,5 @@ "type": "git", | ||
"devDependencies": { | ||
"@sveltejs/kit": "1.0.0-next.216" | ||
"@cloudflare/kv-asset-handler": "^0.2.0", | ||
"@cloudflare/workers-types": "^3.3.0", | ||
"@sveltejs/kit": "1.0.0-next.218" | ||
}, | ||
@@ -32,0 +34,0 @@ "scripts": { |
@@ -50,3 +50,3 @@ # adapter-cloudflare-workers | ||
Get them by visiting your Cloudflare-Dashboard and click on any domain. There, you can scroll down and on the left, you can see your details under **API**. | ||
Get them by visiting your [Cloudflare dashboard](https://dash.cloudflare.com) and click on any domain. There, you can scroll down and on the left, you can see your details under **API**. | ||
@@ -53,0 +53,0 @@ Then configure your sites build directory and your account-details in the config file: |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10738
175
3