Make Netlify adapter actually append redirects#3079
Merged
FredKSchott merged 1 commit intowithastro:mainfrom Apr 11, 2022
Merged
Make Netlify adapter actually append redirects#3079FredKSchott merged 1 commit intowithastro:mainfrom
FredKSchott merged 1 commit intowithastro:mainfrom
Conversation
🦋 Changeset detectedLatest commit: b4b99dd The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Member
|
LGTM! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:donehook calledfs.existsSync(_redirects)to check if the redirects target file already exists, before deciding to either call appendFile or writeFile. Unfortunately,_redirectsis 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_redirectsfile.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/_redirectsfile, 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/_redirectsfile.I also ran
pnpm run test --filter @astrojs/netlifywithout 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/_redirectsfile), which wasn't possible before due to the buggy check. Adding custom redirects could optionally be documented as an advanced usage scenario.