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

feat: generate .assetsignore file for Cloudflare deployment #13109

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
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');
},
async emulate() {
const proxy = await getPlatformProxy(options.platformProxy);
Expand Down
Loading