Skip to content

Commit

Permalink
include as many static assets as possible in exclude list (#8422)
Browse files Browse the repository at this point in the history
* include as many static assets as possible in exclude list - closes #7640

* temporarily add trailing slash

* Revert "temporarily add trailing slash"

This reverts commit f3ff35f.

* add a warning

* fix typo

* fix string

* Update .changeset/giant-penguins-act.md

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>

* fix

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 19, 2023
1 parent 06a56ae commit c9d2711
Show file tree
Hide file tree
Showing 2 changed files with 43 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': minor
---

feat: include as many static assets as possible in exclude list
55 changes: 38 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,50 @@ 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}/*`,
...assets.filter((file) => !file.startsWith(`${builder.config.kit.appDir}/`))
];

const MAX_EXCLUSIONS = 99; // 100 minus existing `include` rules
let excess;

if (exclude.length > MAX_EXCLUSIONS) {
excess = 'static assets';

if (builder.prerendered.paths.length > 0) {
excess += ' or prerendered routes';
}
} else if (exclude.length + builder.prerendered.paths.length > MAX_EXCLUSIONS) {
excess = 'prerendered routes';
}

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

if (excess) {
const message = `Static file count exceeds _routes.json limits (see https://developers.cloudflare.com/pages/platform/functions/routing/#limits). Accessing some ${excess} will cause function invocations.`;
builder.log.warn(message);
exclude.length = 99;
}

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
};
}

Expand Down

0 comments on commit c9d2711

Please sign in to comment.