Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/proud-taxis-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare': minor
---

feat: add `.assetsignore` file to avoid serving known server-only files to browser
3 changes: 2 additions & 1 deletion packages/adapter-cloudflare/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/files
/files/*
!/files/.assetsignore
4 changes: 4 additions & 0 deletions packages/adapter-cloudflare/files/.assetsignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
_worker.js
_routes.json
_headers
_redirects
16 changes: 15 additions & 1 deletion packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, writeFileSync } from 'node:fs';
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
import * as path from 'node:path';
import { fileURLToPath } from 'node:url';
import * as esbuild from 'esbuild';
Expand Down Expand Up @@ -143,6 +143,20 @@ export default function (options = {}) {
}`
);
}

const assetsignore = [`${dest}/.assetsignore`, `${files}/.assetsignore`].reduce(
(acc, file) => {
if (existsSync(file)) {
readFileSync(file, 'utf8')
.split('\n')
.filter((line) => line.trim())
.forEach((line) => acc.add(line));
}
return acc;
},
new Set()
);
writeFileSync(`${dest}/.assetsignore`, Array.from(assetsignore).sort().join('\n') + '\n');
Copy link
Member

@teemingc teemingc Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend appending to the existing file instead, similar to the generate_headers function. It should be fine if we have duplicate entries. This way we don't need the files/.assetsignore file and .gitignore change either.

Suggested change
const assetsignore = [`${dest}/.assetsignore`, `${files}/.assetsignore`].reduce(
(acc, file) => {
if (existsSync(file)) {
readFileSync(file, 'utf8')
.split('\n')
.filter((line) => line.trim())
.forEach((line) => acc.add(line));
}
return acc;
},
new Set()
);
writeFileSync(`${dest}/.assetsignore`, Array.from(assetsignore).sort().join('\n') + '\n');
writeFileSync(`${dest}/.assetsignore`, generate_assetsignore(), { flag: 'a' });
 function generate_assetsignore() { 
 	return ` 
_worker.js
_routes.json
_headers
_redirects
 `.trimEnd(); 
 } 

Copy link
Contributor Author

@methanoya methanoya Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have simplified the code and added generate_assetsignore() function to handle the .assetsignore separately. It is not clear why keeping the source code intact (.gitignore and files) would take priority over avoiding the hardcoding. The .assetsignore comes from another project cloudflare/workers-sdk and it makes sense to keep it separated for readability and maintainability in case of further updates.
For now, the generate_assetsignore() simply merges the user's .assetsignore file and the original one into the final .assetsignore. The proposed snipped above doesn't provide the merging, so cannot be used as is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposed snipped above doesn't provide the merging, so cannot be used as is.

It does merge it because if the user has the file /static/.assetsignore it would have been copied over to the destination and those entries would be appended to the end of the existing file or write to a new file if it doesn't already exist.

Copy link
Contributor Author

@methanoya methanoya Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I missed the first part of the update besides the snippet. It looks like my concerns are ignored, so I've updated the PR as suggested but added comments about the source of the .assetsignore.

},
async emulate() {
const proxy = await getPlatformProxy(options.platformProxy);
Expand Down
Loading