From 3b83249ba22353ccfe592d265d2c3b8b71c2c224 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 12 May 2022 17:22:59 -0500 Subject: [PATCH] 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 --- .changeset/ninety-books-jam.md | 5 +++++ packages/adapter-netlify/index.js | 29 ++++++++++++++++------------- 2 files changed, 21 insertions(+), 13 deletions(-) create mode 100644 .changeset/ninety-books-jam.md diff --git a/.changeset/ninety-books-jam.md b/.changeset/ninety-books-jam.md new file mode 100644 index 000000000000..45c0e1ed55cb --- /dev/null +++ b/.changeset/ninety-books-jam.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-netlify': patch +--- + +Copy existing `_redirects` file before appending function redirects diff --git a/packages/adapter-netlify/index.js b/packages/adapter-netlify/index.js index 917dabe00e65..89fa7305cc4e 100644 --- a/packages/adapter-netlify/index.js +++ b/packages/adapter-netlify/index.js @@ -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'; @@ -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` - ); } }; } @@ -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')) {