Skip to content

Commit 3b83249

Browse files
authored
append function redirects after copying existing redirects - fixes #4722 (#4890)
* append function redirects after copying existing redirects - fixes #4722 * Update .changeset/ninety-books-jam.md * add explanatory comment
1 parent c8e9822 commit 3b83249

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

.changeset/ninety-books-jam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-netlify': patch
3+
---
4+
5+
Copy existing `_redirects` file before appending function redirects

packages/adapter-netlify/index.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
5454

5555
builder.log.minor(`Publishing to "${publish}"`);
5656

57+
builder.log.minor('Copying assets...');
58+
builder.writeStatic(publish);
59+
builder.writeClient(publish);
60+
builder.writePrerendered(publish);
61+
62+
builder.log.minor('Writing custom headers...');
63+
const headers_file = join(publish, '_headers');
64+
builder.copy('_headers', headers_file);
65+
appendFileSync(
66+
headers_file,
67+
`\n\n/${builder.config.kit.appDir}/*\n cache-control: public\n cache-control: immutable\n cache-control: max-age=31536000\n`
68+
);
69+
5770
// for esbuild, use ESM
5871
// for zip-it-and-ship-it, use CJS until https://github.com/netlify/zip-it-and-ship-it/issues/750
5972
const esm = netlify_config?.functions?.node_bundler === 'esbuild';
@@ -67,19 +80,6 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
6780
} else {
6881
await generate_lambda_functions({ builder, esm, split, publish });
6982
}
70-
71-
builder.log.minor('Copying assets...');
72-
builder.writeStatic(publish);
73-
builder.writeClient(publish);
74-
builder.writePrerendered(publish);
75-
76-
builder.log.minor('Writing custom headers...');
77-
const headers_file = join(publish, '_headers');
78-
builder.copy('_headers', headers_file);
79-
appendFileSync(
80-
headers_file,
81-
`\n\n/${builder.config.kit.appDir}/*\n cache-control: public\n cache-control: immutable\n cache-control: max-age=31536000\n`
82-
);
8383
}
8484
};
8585
}
@@ -230,6 +230,9 @@ function generate_lambda_functions({ builder, publish, split, esm }) {
230230
redirects.push('* /.netlify/functions/render 200');
231231
}
232232

233+
// this should happen at the end, after builder.writeStatic(...),
234+
// so that generated redirects are appended to custom redirects
235+
// rather than replaced by them
233236
builder.log.minor('Writing redirects...');
234237
const redirect_file = join(publish, '_redirects');
235238
if (existsSync('_redirects')) {

0 commit comments

Comments
 (0)