Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Netlify adapter actually append redirects #3079

Merged

Conversation

hippotastic
Copy link
Contributor

Changes

The previous code in the astro:build:done hook called fs.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.

@changeset-bot
Copy link

changeset-bot bot commented Apr 11, 2022

🦋 Changeset detected

Latest commit: b4b99dd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@astrojs/netlify Patch

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

@github-actions github-actions bot added the pkg: integration Related to any renderer integration (scope) label Apr 11, 2022
@FredKSchott
Copy link
Member

LGTM!

@FredKSchott FredKSchott merged commit 9f248b0 into withastro:main Apr 11, 2022
@github-actions github-actions bot mentioned this pull request Apr 11, 2022
@hippotastic hippotastic deleted the fix-netlify-redirects-output branch June 22, 2022 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: integration Related to any renderer integration (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants