Skip to content

Commit

Permalink
append function redirects after copying existing redirects - fixes #4722
Browse files Browse the repository at this point in the history
 (#4890)

* append function redirects after copying existing redirects - fixes #4722

* Update .changeset/ninety-books-jam.md

* add explanatory comment
  • Loading branch information
Rich-Harris authored May 12, 2022
1 parent c8e9822 commit 3b83249
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-books-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-netlify': patch
---

Copy existing `_redirects` file before appending function redirects
29 changes: 16 additions & 13 deletions packages/adapter-netlify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) {

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

builder.log.minor('Copying assets...');
builder.writeStatic(publish);
builder.writeClient(publish);
builder.writePrerendered(publish);

builder.log.minor('Writing custom headers...');
const headers_file = join(publish, '_headers');
builder.copy('_headers', headers_file);
appendFileSync(
headers_file,
`\n\n/${builder.config.kit.appDir}/*\n cache-control: public\n cache-control: immutable\n cache-control: max-age=31536000\n`
);

// for esbuild, use ESM
// for zip-it-and-ship-it, use CJS until https://github.com/netlify/zip-it-and-ship-it/issues/750
const esm = netlify_config?.functions?.node_bundler === 'esbuild';
Expand All @@ -67,19 +80,6 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
} else {
await generate_lambda_functions({ builder, esm, split, publish });
}

builder.log.minor('Copying assets...');
builder.writeStatic(publish);
builder.writeClient(publish);
builder.writePrerendered(publish);

builder.log.minor('Writing custom headers...');
const headers_file = join(publish, '_headers');
builder.copy('_headers', headers_file);
appendFileSync(
headers_file,
`\n\n/${builder.config.kit.appDir}/*\n cache-control: public\n cache-control: immutable\n cache-control: max-age=31536000\n`
);
}
};
}
Expand Down Expand Up @@ -230,6 +230,9 @@ function generate_lambda_functions({ builder, publish, split, esm }) {
redirects.push('* /.netlify/functions/render 200');
}

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

0 comments on commit 3b83249

Please sign in to comment.