Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include as many static assets as possible in exclude list #8422

Merged
merged 8 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
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)
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
};
}

Expand Down