Skip to content

Commit

Permalink
include as many static assets as possible in exclude list - closes #7640
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jan 9, 2023
1 parent 5cdc3af commit fd8a924
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-penguins-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare': patch
---

Include as many static assets as possible in exclude list
39 changes: 22 additions & 17 deletions packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default function () {

writeFileSync(
`${dest}/_routes.json`,
JSON.stringify(get_routes_json(builder.config.kit.appDir, written_files))
JSON.stringify(get_routes_json(builder, written_files))
);

writeFileSync(`${dest}/_headers`, generate_headers(builder.config.kit.appDir));
Expand Down Expand Up @@ -62,29 +62,34 @@ export default function () {
}

/**
* @param {string} app_dir
* @param {import('@sveltejs/kit').Builder} builder
* @param {string[]} assets
* @returns {import('.').RoutesJSONSpec}
*/
function get_routes_json(app_dir, assets) {
function get_routes_json(builder, assets) {
/**
* The list of routes that will _not_ invoke functions (which cost money).
* This is done on a best-effort basis, as there is a limit of 100 rules
*/
const exclude = [`/${builder.config.kit.appDir}/*`];

for (const file of assets) {
if (!file.startsWith(`${builder.config.kit.appDir}/`)) {
exclude.push(`/${file}`);
}
}

for (const path of builder.prerendered.paths) {
if (!builder.prerendered.redirects.has(path)) {
exclude.push(path);
}
}

return {
version: 1,
description: 'Generated by @sveltejs/adapter-cloudflare',
include: ['/*'],
exclude: [
`/${app_dir}/immutable/*`,
...assets
// We're being conservative by not excluding all assets in
// /static just yet. If there are any upstream auth rules to
// protect certain things (e.g. a PDF that requires auth),
// then we wouldn't want to prevent those requests from going
// to the user functions worker.
// We do want to show an example of a _routes.json that
// excludes more than just /_app/immutable/*, and favicons
// are a reasonable choice
.filter((file) => file.startsWith('favicon'))
.map((file) => `/${file}`)
]
exclude: exclude.slice(0, 99)
};
}

Expand Down

0 comments on commit fd8a924

Please sign in to comment.