Make Netlify adapter actually append redirects #3079
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes
The previous code in the
astro:build:done
hook calledfs.existsSync(_redirects)
to check if the redirects target file already exists, before deciding to either call appendFile or writeFile. Unfortunately,_redirects
is not the file name/URL, but the contents, so this check always failed. The code therefore always used writeFile, effectively overwriting any existing contents of the_redirects
file.My code eliminates this check and always uses
appendFile
, which will automatically create the file if it doesn't exist yet (as per the Node.js docs).If you put your own custom redirects in a
/public/_redirects
file, they will get copied to the build output directory and Astro will now add its own redirects to the end instead of always overwriting the file.Testing
The new code was tested locally on Windows 10 by building an example site using the fixed Netlify adapter, both with and without an existing
/public/_redirects
file.I also ran
pnpm run test --filter @astrojs/netlify
without any errors.Docs
This is just a fix, so no documentation is needed per se. It however allows the use case outlined above (putting custom redirects in a
/public/_redirects
file), which wasn't possible before due to the buggy check. Adding custom redirects could optionally be documented as an advanced usage scenario.