@astrojs/ netlify
Ce contenu n’est pas encore disponible dans votre langue.
This adapter allows Astro to deploy your hybrid
or server
rendered site to Netlify.
If you’re using Astro as a static site builder, you don’t need an adapter.
Learn how to deploy your Astro site in our Netlify deployment guide.
Why Astro Netlify
Section titled Why Astro NetlifyNetlify is a deployment platform that allows you to host your site by connecting directly to your GitHub repository. This adapter enhances the Astro build process to prepare your project for deployment through Netlify.
Installation
Section titled InstallationAstro includes an astro add
command to automate the setup of official integrations. If you prefer, you can install integrations manually instead.
Add the Netlify adapter to enable SSR in your Astro project with the astro add
command.
This will install @astrojs/netlify
and make the appropriate changes to your astro.config.mjs
file in one step.
Manual Install
Section titled Manual InstallFirst, install the Netlify adapter to your project’s dependencies using your preferred package manager:
Then, add the adapter and your desired on-demand rendering mode to your astro.config.*
file:
Usage
Section titled UsageRead the full deployment guide here.
Follow the instructions to build your site locally. After building, you will have a .netlify/
folder containing both Netlify Functions in the .netlify/functions-internal/
folder and Netlify Edge Functions in the.netlify/edge-functions/
folder.
To deploy your site, install the Netlify CLI and run:
The Netlify Blog post on Astro and the Netlify Docs provide more information on how to use this integration to deploy to Netlify.
Accessing edge context from your site
Section titled Accessing edge context from your siteNetlify Edge Functions provide a context object that includes metadata about the request such as a user’s IP, geolocation data, and cookies.
This can be accessed through the Astro.locals.netlify.context
object:
If you’re using TypeScript, you can get proper typings by updating src/env.d.ts
to use NetlifyLocals
:
This is not available on prerendered pages.
Running Astro middleware in Edge Functions
Section titled Running Astro middleware in Edge FunctionsAny Astro middleware is applied to pre-rendered pages at build-time, and to on-demand-rendered pages at runtime.
To implement redirects, access control or custom response headers for pre-rendered pages, run your middleware on Netlify Edge Functions by enabling the edgeMiddleware
option:
Configuring edgeMiddleware: true
will deploy your middleware as an Edge Function, and run it on all routes - including pre-rendered pages. However, locals specified in the middleware won’t be available to any pre-rendered pages, because they’ve already been fully-rendered at build-time.
Netlify Image CDN support
Section titled Netlify Image CDN supportThis adapter by default uses the Netlify Image CDN to transform images on-the-fly without impacting build times. It’s implemented using an Astro Image Service under the hood.
To opt out of Netlify’s Image CDN remote image optimization, use the imageCDN
option:
This adapter does not support the image.domains
and image.remotePatterns
config properties in your Astro config. To specify remote paths for Netlify Image CDN, use the remote_images
field in netlify.toml
.
Static sites & Redirects
Section titled Static sites & RedirectsFor static sites you usually don’t need an adapter. However, if you use redirects
configuration in your Astro config, the Netlify adapter can be used to translate this to the proper _redirects
format.
Once you run astro build
there will be a dist/_redirects
file. Netlify will use that to properly route pages in production.
You can still include a public/_redirects
file for manual redirects. Any redirects you specify in the redirects config are appended to the end of your own.
Caching Pages
Section titled Caching PagesOn-demand rendered pages without any dynamic content can be cached to improve performance and lower resource usage.
Enabling the cacheOnDemandPages
option in the adapter will cache all server-rendered pages for up to one year:
This can be changed on a per-page basis by adding caching headers to your response:
With fine-grained cache control, Netlify supports
standard caching headers like CDN-Cache-Control
or Vary
.
Refer to the docs to learn about implementing e.g. time to live (TTL) or stale while revalidate (SWR) caching: https://docs.netlify.com/platform/caching
Examples
Section titled Examples-
The Astro Netlify Edge Starter provides an example and a guide in the README.
-
Browse Astro Netlify projects on GitHub for more examples!