Skip to content

Commit c9d2711

Browse files
Rich-Harrisdummdidummbenmccann
authored
include as many static assets as possible in exclude list (#8422)
* 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 <[email protected]> * fix Co-authored-by: Simon H <[email protected]> Co-authored-by: Ben McCann <[email protected]>
1 parent 06a56ae commit c9d2711

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

.changeset/giant-penguins-act.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-cloudflare': minor
3+
---
4+
5+
feat: include as many static assets as possible in exclude list

packages/adapter-cloudflare/index.js

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default function () {
3434

3535
writeFileSync(
3636
`${dest}/_routes.json`,
37-
JSON.stringify(get_routes_json(builder.config.kit.appDir, written_files))
37+
JSON.stringify(get_routes_json(builder, written_files))
3838
);
3939

4040
writeFileSync(`${dest}/_headers`, generate_headers(builder.config.kit.appDir));
@@ -62,29 +62,50 @@ export default function () {
6262
}
6363

6464
/**
65-
* @param {string} app_dir
65+
* @param {import('@sveltejs/kit').Builder} builder
6666
* @param {string[]} assets
6767
* @returns {import('.').RoutesJSONSpec}
6868
*/
69-
function get_routes_json(app_dir, assets) {
69+
function get_routes_json(builder, assets) {
70+
/**
71+
* The list of routes that will _not_ invoke functions (which cost money).
72+
* This is done on a best-effort basis, as there is a limit of 100 rules
73+
*/
74+
const exclude = [
75+
`/${builder.config.kit.appDir}/*`,
76+
...assets.filter((file) => !file.startsWith(`${builder.config.kit.appDir}/`))
77+
];
78+
79+
const MAX_EXCLUSIONS = 99; // 100 minus existing `include` rules
80+
let excess;
81+
82+
if (exclude.length > MAX_EXCLUSIONS) {
83+
excess = 'static assets';
84+
85+
if (builder.prerendered.paths.length > 0) {
86+
excess += ' or prerendered routes';
87+
}
88+
} else if (exclude.length + builder.prerendered.paths.length > MAX_EXCLUSIONS) {
89+
excess = 'prerendered routes';
90+
}
91+
92+
for (const path of builder.prerendered.paths) {
93+
if (!builder.prerendered.redirects.has(path)) {
94+
exclude.push(path);
95+
}
96+
}
97+
98+
if (excess) {
99+
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.`;
100+
builder.log.warn(message);
101+
exclude.length = 99;
102+
}
103+
70104
return {
71105
version: 1,
72106
description: 'Generated by @sveltejs/adapter-cloudflare',
73107
include: ['/*'],
74-
exclude: [
75-
`/${app_dir}/immutable/*`,
76-
...assets
77-
// We're being conservative by not excluding all assets in
78-
// /static just yet. If there are any upstream auth rules to
79-
// protect certain things (e.g. a PDF that requires auth),
80-
// then we wouldn't want to prevent those requests from going
81-
// to the user functions worker.
82-
// We do want to show an example of a _routes.json that
83-
// excludes more than just /_app/immutable/*, and favicons
84-
// are a reasonable choice
85-
.filter((file) => file.startsWith('favicon'))
86-
.map((file) => `/${file}`)
87-
]
108+
exclude
88109
};
89110
}
90111

0 commit comments

Comments
 (0)