Working with Integrations
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
Astro integrations add new functionality and behaviors for your project with only a few lines of code. You can use an official integration, integrations built by the community or even build a custom integration yourself.
Integrations can…
- Unlock React, Vue, Svelte, Solid, and other popular UI frameworks with a renderer.
- Enable on-demand rendering with an SSR adapter.
- Integrate tools like MDX, and Partytown with a few lines of code.
- Add new features to your project, like automatic sitemap generation.
- Write custom code that hooks into the build process, dev server, and more.
Browse or search the complete set of hundreds of official and community integrations in our integrations directory. Find packages to add to your Astro project for authentication, analytics, performance, SEO, accessibility, UI, developer tools, and more.
Official Integrations
Section titled “Official Integrations”The following integrations are maintained by Astro.
UI-Frameworks
SSR-Adapter
Sonstiges
Automatic Integration Setup
Section titled “Automatic Integration Setup”Astro includes an astro add command to automate the setup of official integrations. Several community plugins can also be added using this command. Please check each integration’s own documentation to see whether astro add is supported, or whether you must install manually.
Run the astro add command using the package manager of your choice and our automatic integration wizard will update your configuration file and install any necessary dependencies.
npx astro add reactpnpm astro add reactyarn astro add reactIt’s even possible to add multiple integrations at the same time!
npx astro add react sitemap partytownpnpm astro add react sitemap partytownyarn astro add react sitemap partytownIf you see any warnings like Cannot find package '[package-name]' after adding an integration, your package manager may not have installed peer dependencies for you. To install these missing packages, run the following command:
npm install [package-name]pnpm add [package-name]yarn add [package-name]Manual Installation
Section titled “Manual Installation”Astro integrations are always added through the integrations property in your astro.config.mjs file.
There are three common ways to import an integration into your Astro project:
-
Import your own integration from a local file inside your project.
-
Write your integration inline, directly in your config file.
astro.config.mjs import { defineConfig } from 'astro/config';import installedIntegration from '@astrojs/vue';import localIntegration from './my-integration.js';export default defineConfig({integrations: [// 1. Imported from an installed npm packageinstalledIntegration(),// 2. Imported from a local JS filelocalIntegration(),// 3. An inline object{name: 'namespace:id', hooks: { /* ... */ }},]});
Check out the Integration API reference to learn all of the different ways that you can write an integration.
Installing an NPM package
Section titled “Installing an NPM package”Install an NPM package integration using a package manager, and then update astro.config.mjs manually.
For example, to install the @astrojs/sitemap integration:
-
Install the integration to your project dependencies using your preferred package manager:
Terminal window npm install @astrojs/sitemapTerminal window pnpm add @astrojs/sitemapTerminal window yarn add @astrojs/sitemap -
Import the integration to your
astro.config.mjsfile, and add it to yourintegrations[]array, along with any configuration options:astro.config.mjs import { defineConfig } from 'astro/config';import sitemap from '@astrojs/sitemap';export default defineConfig({// ...integrations: [sitemap()],// ...});Note that different integrations may have different configuration settings. Read each integration’s documentation, and apply any necessary config options to your chosen integration in
astro.config.mjs.
Custom Options
Section titled “Custom Options”Integrations are almost always authored as factory functions that return the actual integration object. This lets you pass arguments and options to the factory function that customize the integration for your project.
integrations: [ // Example: Customize your integration with function arguments sitemap({filter: true})]Toggle an Integration
Section titled “Toggle an Integration”Falsy integrations are ignored, so you can toggle integrations on & off without worrying about left-behind undefined and boolean values.
integrations: [ // Example: Skip building a sitemap on Windows process.platform !== 'win32' && sitemap()]Upgrading Integrations
Section titled “Upgrading Integrations”To upgrade all official integrations at once, run the @astrojs/upgrade command. This will upgrade both Astro and all official integrations to their latest versions.
Automatic Upgrading
Section titled “Automatic Upgrading”# Upgrade Astro and official integrations together to latestnpx @astrojs/upgrade# Upgrade Astro and official integrations together to latestpnpm dlx @astrojs/upgrade# Upgrade Astro and official integrations together to latestyarn dlx @astrojs/upgradeManual Upgrading
Section titled “Manual Upgrading”To upgrade one or more integrations manually, use the appropriate command for your package manager.
# Example: upgrade React and Partytown integrationsnpm install @astrojs/react@latest @astrojs/partytown@latest# Example: upgrade React and Partytown integrationspnpm add @astrojs/react@latest @astrojs/partytown@latest# Example: upgrade React and Partytown integrationsyarn add @astrojs/react@latest @astrojs/partytown@latestRemoving an Integration
Section titled “Removing an Integration”-
To remove an integration, first uninstall the integration from your project.
Terminal window npm uninstall @astrojs/reactTerminal window pnpm remove @astrojs/reactTerminal window yarn remove @astrojs/react -
Next, remove the integration from your
astro.config.*file:astro.config.mjs import { defineConfig } from 'astro/config';import react from '@astrojs/react';export default defineConfig({integrations: [react()]});
Finding More Integrations
Section titled “Finding More Integrations”You can find many integrations developed by the community in the Astro Integrations Directory. Follow links there for detailed usage and configuration instructions.
Building Your Own Integration
Section titled “Building Your Own Integration”Astro’s Integration API is inspired by Rollup and Vite, and designed to feel familiar to anyone who has ever written a Rollup or Vite plugin before.
Check out the Integration API reference to learn what integrations can do and how to write one yourself.
Publishing Your Integration to npm
Section titled “Publishing Your Integration to npm”Publishing an Astro component is a great way to reuse your existing work across your projects, and to share with the wider Astro community at large. Astro components can be published directly to and installed from npm, just like any other JavaScript package.
Looking for inspiration? Check out some of our favorite themes and components from the Astro community. You can also search npm to see the entire public catalog.
Check out Astro Community’s component template for a community-supported, out-of-the-box template!
Quick Start
Section titled “Quick Start”To get started developing your component quickly, you can use a template already set up for you.
# Initialize the Astro Component template in a new directorynpm create astro@latest my-new-component-directory -- --template component# yarnyarn create astro my-new-component-directory --template component# pnpmpnpm create astro@latest my-new-component-directory -- --template componentCreating a package
Section titled “Creating a package”Before diving in, it will help to have a basic understanding of:
To create a new package, configure your development environment to use workspaces within your project. This will allow you to develop your component alongside a working copy of Astro.
Ordnermy-new-component-directory/
Ordnerdemo/
- … for testing and demonstration
- package.json
Ordnerpackages/
Ordnermy-component/
- index.js
- package.json
- … additional files used by the package
This example, named my-project, creates a project with a single package, named my-component, and a demo/ directory for testing and demonstrating the component.
This is configured in the project root’s package.json file:
{ "name": "my-project", "workspaces": ["demo", "packages/*"]}In this example, multiple packages can be developed together from the packages directory. These packages can also be referenced from demo, where you can install a working copy of Astro.
npm create astro@latest demo -- --template minimal# yarnyarn create astro demo --template minimal# pnpmpnpm create astro@latest demo -- --template minimalThere are two initial files that will make up your individual package: package.json and index.js.
package.json
Section titled “package.json”The package.json in the package directory includes all of the information related to your package, including its description, dependencies, and any other package metadata.
{ "name": "my-component", "description": "Component description", "version": "1.0.0", "homepage": "https://github.com/owner/project#readme", "type": "module", "exports": { ".": "./index.js", "./astro": "./MyAstroComponent.astro", "./react": "./MyReactComponent.jsx" }, "files": ["index.js", "MyAstroComponent.astro", "MyReactComponent.jsx"], "keywords": ["astro", "withastro", "astro-component", "...", "..."]}description
Section titled “description”A short description of your component used to help others know what it does.
{ "description": "An Astro Element Generator"}The module format used by Node.js and Astro to interpret your index.js files.
{ "type": "module"}Use "type": "module" so that your index.js can be used as an entrypoint with import and export .
homepage
Section titled “homepage”The url to the project homepage.
{ "homepage": "https://github.com/owner/project#readme"}This is a great way to direct users to an online demo, documentation, or homepage for your project.
exports
Section titled “exports”The entry points of a package when imported by name.
{ "exports": { ".": "./index.js", "./astro": "./MyAstroComponent.astro", "./react": "./MyReactComponent.jsx" }}In this example, importing my-component would use index.js, while importing my-component/astro or my-component/react would use MyAstroComponent.astro or MyReactComponent.jsx respectively.
An optional optimization to exclude unnecessary files from the bundle shipped to users via npm. Note that only files listed here will be included in your package, so if you add or change files necessary for your package to work, you must update this list accordingly.
{ "files": ["index.js", "MyAstroComponent.astro", "MyReactComponent.jsx"]}keywords
Section titled “keywords”An array of keywords relevant to your component, used to help others find your component on npm and in any other search catalogs.
Add astro-component, astro-integration, or withastro as a special keyword to maximize its discoverability in the Astro ecosystem.
{ "keywords": ["astro-component", "withastro", "... etc", "... etc"]}Keywords are also used by our integrations library! See below for a full list of keywords we look for in npm.
index.js
Section titled “index.js”The main package entrypoint used whenever your package is imported.
export { default as MyAstroComponent } from './MyAstroComponent.astro';
export { default as MyReactComponent } from './MyReactComponent.jsx';This allows you to package multiple components together into a single interface.
Example: Using Named Imports
Section titled “Example: Using Named Imports”---import { MyAstroComponent } from 'my-component';import { MyReactComponent } from 'my-component';---<MyAstroComponent /><MyReactComponent />Example: Using Namespace Imports
Section titled “Example: Using Namespace Imports”---import * as Example from 'example-astro-component';---<Example.MyAstroComponent /><Example.MyReactComponent />Example: Using Individual Imports
Section titled “Example: Using Individual Imports”---import MyAstroComponent from 'example-astro-component/astro';import MyReactComponent from 'example-astro-component/react';---<MyAstroComponent /><MyReactComponent />Developing your package
Section titled “Developing your package”Astro does not have a dedicated “package mode” for development. Instead, you should use a demo project to develop and test your package inside of your project. This can be a private website only used for development, or a public demo/documentation website for your package.
If you are extracting components from an existing project, you can even continue to use that project to develop your now-extracted components.
Testing your component
Section titled “Testing your component”Astro does not currently ship a test runner. (If you are interested in helping out with this, join us on Discord!)
In the meantime, our current recommendation for testing is:
-
Add a test
fixturesdirectory to yourdemo/src/pagesdirectory. -
Add a new page for every test that you’d like to run.
-
Each page should include some different component usage that you’d like to test.
-
Run
astro buildto build your fixtures, then compare the output of thedist/__fixtures__/directory to what you expected.Ordnermy-project/demo/src/pages/__fixtures__/
- test-name-01.astro
- test-name-02.astro
- test-name-03.astro
Publishing your component
Section titled “Publishing your component”Once you have your package ready, you can publish it to npm using the npm publish command. If that fails, make sure that you have logged in via npm login and that your package.json is correct. If it succeeds, you’re done!
Notice that there was no build step for Astro packages. Any file type that Astro supports natively, such as .astro, .ts, .jsx, and .css, can be published directly without a build step.
If you need another file type that isn’t natively supported by Astro, add a build step to your package. This advanced exercise is left up to you.
Integrations Library
Section titled “Integrations Library”Share your hard work by adding your integration to our integrations library!
Do you need some help building your integration, or just want to meet other integrations builders? We have a dedicated #integrations channel on our Discord server. Come say hi!
package.json data
Section titled “package.json data”The library is automatically updated weekly, pulling in every package published to npm with the astro-component, astro-integration, or withastro keyword.
The integrations library reads the name, description, repository, and homepage data from your package.json.
Avatars are a great way to highlight your brand in the library! Once your package is published you can file a GitHub issue with your avatar attached and we will add it to your listing.
Need to override the information our library reads from npm? No problem! File an issue with the updated information and we’ll make sure the custom name, description, or homepage is used instead.
Categories
Section titled “Categories”In addition to the required astro-component, astro-integration, or withastro keyword, special keywords are also used to automatically organize packages. Including any of the keywords below will add your integration to the matching category in our integrations library.
| category | keywords |
|---|---|
| Accessibility | a11y, accessibility |
| Adapters | astro-adapter |
| Analytics | analytics |
| CSS + UI | css, ui, icon, icons, renderer |
| Frameworks | renderer |
| Content Loaders | astro-loader |
| Images + Media | media, image, images, video, audio |
| Performance + SEO | performance, perf, seo, optimization |
| Dev Toolbar | devtools, dev-overlay, dev-toolbar |
| Utilities | tooling, utils, utility |
Packages that don’t include any keyword matching a category will be shown as Uncategorized.
We encourage you to share your work, and we really do love seeing what our talented Astronauts create. Come and share what you create with us in our Discord or mention @astrodotbuild in a Tweet!
Learn