Skip to content

Commit

Permalink
fix: check if there are redirects before writing
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhincore committed Jul 5, 2024
1 parent b6abc2e commit e428e4b
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ export default function (options = {}) {

writeFileSync(`${dest}/_headers`, generate_headers(builder.getAppPath()), { flag: 'a' });

writeFileSync(`${dest}/_redirects`, generate_redirects(builder), { flag: 'a' });
if (builder.prerendered.redirects.size > 0) {
writeFileSync(`${dest}/_redirects`, generate_redirects(builder.prerendered.redirects), { flag: 'a' });
}

builder.copy(`${files}/worker.js`, `${tmp}/_worker.js`, {
replace: {
Expand Down Expand Up @@ -235,10 +237,10 @@ function generate_headers(app_dir) {
`.trimEnd();
}

/** @param {import('@sveltejs/kit').Builder} builder */
function generate_redirects(builder) {
/** @param {Map<string, { status: number; location: string }>} redirects */
function generate_redirects(redirects) {
const rules = Array.from(
builder.prerendered.redirects.entries(),
redirects.entries(),
([path, redirect]) => `${path} ${redirect.location} ${redirect.status}`
).join('\n');

Expand Down

0 comments on commit e428e4b

Please sign in to comment.