How to Use Adapter for Deployment in Astro
In Astro, use
adapter packages to prepare your site for deployment on different platforms. Install the adapter for your target platform, then configure it in astro.config.mjs under the adapter property to enable platform-specific builds.Syntax
To use an adapter in Astro, you import the adapter package and add it to your astro.config.mjs file inside the adapter property. This tells Astro how to build and deploy your site for a specific platform.
Key parts:
import adapter from '@astrojs/adapter-name': imports the adapter package.export default { adapter: adapter() }: sets the adapter in the config.
javascript
import adapter from '@astrojs/adapter-netlify'; export default { adapter: adapter(), };
Example
This example shows how to set up the Netlify adapter for deployment. It installs the adapter, imports it, and configures it in astro.config.mjs. When you run astro build, Astro prepares your site for Netlify hosting.
javascript
import adapter from '@astrojs/adapter-netlify'; export default { adapter: adapter(), // other config options can go here };
Output
Build completed. Site ready for Netlify deployment.
Common Pitfalls
Common mistakes when using adapters include:
- Not installing the adapter package with
npm installoryarn add. - Forgetting to import the adapter in
astro.config.mjs. - Not calling the adapter as a function (
adapter()), which is required. - Using an adapter that does not match your deployment platform.
Always check the adapter documentation for platform-specific options.
javascript
/* Wrong: adapter not called as function */ import adapter from '@astrojs/adapter-netlify'; export default { adapter: adapter, // โ should be adapter() }; /* Correct: */ import adapter from '@astrojs/adapter-netlify'; export default { adapter: adapter(), // โ };
Quick Reference
| Step | Description |
|---|---|
| Install adapter | Run npm install @astrojs/adapter-name |
| Import adapter | Add import statement in astro.config.mjs |
| Configure adapter | Set adapter: adapter() in export default |
| Build site | Run astro build to prepare for deployment |
| Deploy | Upload build output to your platform |
Key Takeaways
Install and import the correct adapter for your deployment platform.
Configure the adapter in astro.config.mjs by calling it as a function.
Adapters customize the build output for specific hosting environments.
Always run astro build after setting the adapter to prepare your site.
Check adapter docs for platform-specific options and updates.