Configuration Astro Globale
Type de ressource : Configuration Framework Astro
Fichier source :astro.config.mjs
Statut : [Actif / Déprécié / En développement]
1. Vue dâensemble
Section titled â1. Vue dâensembleâ1.1. Description
Section titled â1.1. Descriptionâ[Description concise du rĂŽle de cette configuration dans lâarchitecture du site]
1.2. Objectif
Section titled â1.2. Objectifâ- [Objectif principal : ex. configurer le mode hybride SSG/SSR]
- [BĂ©nĂ©fice pour lâarchitecture : ex. alignement avec Cloudflare Pages Functions]
1.3. PérimÚtre concerné
Section titled â1.3. PĂ©rimĂštre concernĂ©â- Mode de rendu : [Hybrid / Static / Server]
- Adaptateur : [Cloudflare / Vercel / Netlify / Node]
- Intégrations actives : [Liste : Tailwind, Sanity, Starlight, Pagefind, etc.]
- Environnements : [Development / Staging / Production]
2. Configuration principale
Section titled â2. Configuration principaleâ2.1. Mode de sortie
Section titled â2.1. Mode de sortieâexport default defineConfig({ output: 'hybrid', // SSG par dĂ©faut, SSR explicite});Rationale : [Expliquer pourquoi ce mode est choisi]
2.2. Adaptateur Cloudflare
Section titled â2.2. Adaptateur Cloudflareâimport cloudflare from '@astrojs/cloudflare';
export default defineConfig({ adapter: cloudflare({ platformProxy: { enabled: true, // Facilite le développement local avec Wrangler }, }),});Rationale : [Expliquer pourquoi cet adaptateur est nécessaire]
2.3. Variables dâenvironnement
Section titled â2.3. Variables dâenvironnementâ| Variable | Description | Obligatoire | Valeur par dĂ©faut |
|---|---|---|---|
PUBLIC_SANITY_PROJECT_ID |
ID du projet Sanity | â | - |
PUBLIC_SANITY_DATASET |
Nom du dataset | â | production |
SANITY_API_READ_TOKEN |
Token de lecture (preview) | â (en preview) | - |
| [Autre variable] | [Description] | [â /â] | [Valeur] |
3. Intégrations
Section titled â3. IntĂ©grationsâ3.1. Tailwind CSS
Section titled â3.1. Tailwind CSSâimport tailwind from '@astrojs/tailwind';
export default defineConfig({ integrations: [ tailwind(), ],});Configuration :
- Fichier :
tailwind.config.mjs - ThÚme personnalisé : [Oui/Non]
- Design tokens : [Lien vers tokens.css]
3.2. Sanity CMS
Section titled â3.2. Sanity CMSâimport sanity from '@sanity/astro';
export default defineConfig({ integrations: [ sanity({ projectId: 'votre_project_id', dataset: 'production', useCdn: true, // CDN Sanity pour le build SSG // Le client sera reconfiguré dynamiquement en mode preview }), ],});Rationale : [Expliquer la configuration du CDN et le mode preview]
3.3. Starlight (Documentation)
Section titled â3.3. Starlight (Documentation)âimport starlight from '@astrojs/starlight';
export default defineConfig({ integrations: [ starlight({ title: 'Documentation Technique', sidebar: [ { label: 'Guide de Démarrage', autogenerate: { directory: 'guide' } }, { label: 'Références API', autogenerate: { directory: 'api' } }, ], }), ],});Configuration :
- Route interceptée :
/docs - Source de contenu :
src/content/docs/ - ThÚme personnalisé : [Lien vers starlight-overrides.css]
3.4. Pagefind (Recherche)
Section titled â3.4. Pagefind (Recherche)âimport pagefind from 'astro-pagefind';
export default defineConfig({ integrations: [ pagefind({ ranking: { pageLength: 0.1, termFrequency: 0.5, termSaturation: 0.5, termSimilarity: 0.9, }, }), ],});Rationale : [Expliquer pourquoi Pagefind est utilisĂ© et comment il sâintĂšgre]
3.5. Sitemap
Section titled â3.5. Sitemapâimport sitemap from '@astrojs/sitemap';
export default defineConfig({ integrations: [ sitemap({ // Configuration optionnelle }), ],});3.6. Autres intégrations
Section titled â3.6. Autres intĂ©grationsâ| IntĂ©gration | RĂŽle | Configuration |
|---|---|---|
| [Nom] | [RĂŽle] | [Lien ou extrait] |
4. Build et déploiement
Section titled â4. Build et dĂ©ploiementâ4.1. Commandes npm
Section titled â4.1. Commandes npmâ{ "scripts": { "dev": "astro dev", "start": "astro dev", "build": "astro build", "preview": "astro preview", "astro": "astro", "typegen": "sanity typegen generate" }}4.2. Pipeline CI/CD
Section titled â4.2. Pipeline CI/CDâ# Exemple de workflow GitHub Actionsname: Build and Deployon: push: branches: [main] repository_dispatch: types: [sanity-webhook]
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: '20' - run: npm ci - run: npm run typegen - run: npm run build - run: npm run deploy4.3. Variables dâenvironnement CI/CD
Section titled â4.3. Variables dâenvironnement CI/CDâ| Variable | Description | Obligatoire |
|---|---|---|
PUBLIC_SANITY_PROJECT_ID |
ID du projet Sanity | â |
PUBLIC_SANITY_DATASET |
Nom du dataset | â |
SANITY_API_READ_TOKEN |
Token de lecture | â |
| [Autre variable] | [Description] | [â /â] |
5. Configuration du mode Preview
Section titled â5. Configuration du mode Previewâ5.1. DĂ©tection du mode Preview
Section titled â5.1. DĂ©tection du mode Previewâimport { createClient } from '@sanity/client';import type { AstroGlobal } from 'astro';
export function getSanityClient(astro: AstroGlobal) { const isPreview = astro.cookies.get('sanity-preview')?.value === 'true' || astro.request.headers.get('x-sanity-preview') === 'true';
return createClient({ projectId: import.meta.env.PUBLIC_SANITY_PROJECT_ID, dataset: import.meta.env.PUBLIC_SANITY_DATASET, apiVersion: '2025-01-01', useCdn: !isPreview, perspective: isPreview ? 'previewDrafts' : 'published', token: isPreview ? import.meta.env.SANITY_API_READ_TOKEN : undefined, });}5.2. RĂšgles importantes
Section titled â5.2. RĂšgles importantesâ- â Aucune page en mode Preview nâest gĂ©nĂ©rĂ©e en SSG
- â
Le token
SANITY_API_READ_TOKENest uniquement disponible en runtime - â La documentation Starlight nâest pas concernĂ©e par ce mĂ©canisme
- â Le Middleware Cloudflare gĂšre la validation du secret et la pose du cookie
6. Stratégie de cache
Section titled â6. StratĂ©gie de cacheâ6.1. Politique de cache par type de page
Section titled â6.1. Politique de cache par type de pageâ| Type de page | Cache-Control (navigateur) | CDN-Cache-Control (Cloudflare) |
|---|---|---|
| Pages SSG | public, max-age=31536000, immutable |
public, max-age=31536000, immutable |
| Pages SSR semi-dynamiques | public, max-age=60 |
max-age=300, stale-while-revalidate=86400 |
| Pages SSR hautement dynamiques | private, no-store |
no-store |
| Pages de formulaire (GET) | public, max-age=300 |
max-age=600, stale-while-revalidate=3600 |
| RĂ©ponses dâactions (POST) | no-store |
no-store |
| Mode Preview | no-store, no-cache, must-revalidate, private |
no-store |
6.2. Configuration dans les pages SSR
Section titled â6.2. Configuration dans les pages SSRâ// Exemple dans une page SSRAstro.response.headers.set('Cache-Control', 'public, max-age=60');Astro.response.headers.set('CDN-Cache-Control', 'max-age=300, stale-while-revalidate=86400');7. Typage et gĂ©nĂ©ration de types
Section titled â7. Typage et gĂ©nĂ©ration de typesâ7.1. Sanity TypeGen
Section titled â7.1. Sanity TypeGenâ{ "path": "./src/**/*.{ts,tsx,astro}", "schema": "./schema.json", "generates": "./src/types/sanity.d.ts"}7.2. Commandes de gĂ©nĂ©ration
Section titled â7.2. Commandes de gĂ©nĂ©rationâ# GĂ©nĂ©rer le schĂ©ma depuis Sanitynpx sanity schema extract --enforce-required-fields
# Générer les types TypeScriptnpx sanity typegen generate7.3. Intégration CI/CD
Section titled â7.3. IntĂ©gration CI/CDâ# Dans le pipeline CI- run: npm run typegen- run: npm run buildRĂšgle : Le pipeline CI Ă©choue si les types gĂ©nĂ©rĂ©s ne sont pas Ă jour par rapport aux schĂ©mas.
8. Performance et optimisation
Section titled â8. Performance et optimisationâ8.1. Architecture en Ăźlots
Section titled â8.1. Architecture en Ăźlotsâ| Composant | StratĂ©gie dâhydratation | Rationale |
|---|---|---|
| Navigation mobile | client:load |
Critique pour lâUX |
| Carrousel témoignages | client:visible |
Chargement différé |
| Formulaire de contact | client:load |
Interaction immédiate |
| Contenu statique | Aucune | HTML pur |
8.2. Optimisation des images
Section titled â8.2. Optimisation des imagesâ- â
Toujours fournir
width,heightetalt - â
Utiliser
loading="lazy"sauf pour les images LCP - â
Générer des
srcset/sizespour le responsive - â Formats modernes : WebP/AVIF (via Cloudflare)
8.3. Core Web Vitals
Section titled â8.3. Core Web Vitalsâ| MĂ©trique | Objectif | StratĂ©gie |
|---|---|---|
| LCP | < 2.5s | Images optimisées, fonts préchargées |
| INP | < 200ms | JavaScript minimal, ßlots ciblés |
| CLS | < 0.1 | Dimensions explicites, fonts stables |
9. Sécurité
Section titled â9. SĂ©curitĂ©â9.1. En-tĂȘtes de sĂ©curitĂ©
Section titled â9.1. En-tĂȘtes de sĂ©curitĂ©âLes en-tĂȘtes de sĂ©curitĂ© sont injectĂ©s par le Middleware Cloudflare (voir DAT L3 §3.2) :
| En-tĂȘte | Valeur | Rationale |
|---|---|---|
X-Content-Type-Options |
nosniff |
Ăvite le MIME sniffing |
X-Frame-Options |
DENY |
Protection clickjacking |
Referrer-Policy |
strict-origin-when-cross-origin |
ContrĂŽle du referrer |
Strict-Transport-Security |
max-age=31536000; includeSubDomains; preload |
Force HTTPS |
Content-Security-Policy |
[Voir DAT L3 §3.2] | Protection XSS |
9.2. Tokens et secrets
Section titled â9.2. Tokens et secretsâ- â
Les tokens publics (
PUBLIC_*) peuvent ĂȘtre exposĂ©s cĂŽtĂ© client - â
Les tokens privés (
SANITY_API_*) ne doivent jamais ĂȘtre exposĂ©s cĂŽtĂ© client - â
Les secrets sont injectés via
wrangler secret put
10. Débogage et troubleshooting
Section titled â10. DĂ©bogage et troubleshootingâ10.1. ProblĂšmes courants
Section titled â10.1. ProblĂšmes courantsâ| SymptĂŽme | Cause probable | Solution |
|---|---|---|
| Build échoue | Types Sanity obsolÚtes | Exécuter npm run typegen |
| Preview ne fonctionne pas | Cookie non détecté | Vérifier le Middleware Cloudflare |
| Pages SSR lentes | Cache mal configuré | Vérifier les headers Cache-Control |
| Images non optimisées | Cloudflare Polish désactivé | Activer Polish dans le Dashboard |
| Erreur 500 en production | Token Sanity manquant | VĂ©rifier les variables dâenvironnement |
10.2. Logs et monitoring
Section titled â10.2. Logs et monitoringâ# Logs de buildnpm run build -- --verbose
# Logs de développementnpm run dev
# Logs Cloudflare (production)# Dashboard Cloudflare â Analytics & Logs10.3. Tests manuels
Section titled â10.3. Tests manuelsâ# Test localnpm run dev
# Test de buildnpm run buildnpm run preview
# Vérification des typesnpx astro check11. Bonnes pratiques
Section titled â11. Bonnes pratiquesâ11.1. Configuration
Section titled â11.1. Configurationâ- â
Utiliser le mode
hybridpour bĂ©nĂ©ficier du SSG par dĂ©faut - â Toujours spĂ©cifier lâadaptateur Cloudflare pour lâalignement L3
- â
Centraliser la configuration du client Sanity dans
src/lib/sanity.ts - â Documenter les changements de configuration dans ce fichier
11.2. Développement
Section titled â11.2. DĂ©veloppementâ- â
Tester en local avec
platformProxy.enabled: true - â Utiliser les variables dâenvironnement pour tous les secrets
- â Valider les types TypeScript avant chaque commit
- â VĂ©rifier les Core Web Vitals avec Lighthouse
11.3. Déploiement
Section titled â11.3. DĂ©ploiementâ- â
Exécuter
npm run typegenavant le build - â VĂ©rifier les variables dâenvironnement dans lâhĂ©bergeur
- â Tester le mode Preview en staging avant production
- â Surveiller les erreurs 5xx aprĂšs dĂ©ploiement
12. Ăvolutions et roadmap
Section titled â12. Ăvolutions et roadmapâ12.1. AmĂ©liorations planifiĂ©es
Section titled â12.1. AmĂ©liorations planifiĂ©esâ- [Ăvolution 1 : ex. Ajout de lâintĂ©gration i18n]
- [Ăvolution 2 : ex. Migration vers Astro 5.x]
- [Ăvolution 3 : ex. ImplĂ©mentation de View Transitions]
12.2. Limitations connues
Section titled â12.2. Limitations connuesâ- â Le mode Preview ne fonctionne pas avec le SSG (SSR uniquement)
- â Les Pages Functions ont des limites de mĂ©moire (128 MB)
- â Le build peut ĂȘtre lent avec beaucoup de pages (> 1000)
13. Ressources liées
Section titled â13. Ressources liĂ©esâ- Documentation Astro : Configuration Reference
- Documentation @astrojs/cloudflare : Cloudflare Adapter
- DAT L2 : §2 - Architecture de Rendu & Configuration du Framework
- DAT L3 : §3 - Middleware Global (En-tĂȘtes de sĂ©curitĂ©)
- Fichiers concernés :
astro.config.mjspackage.jsonsrc/lib/sanity.tstailwind.config.mjssanity-typegen.config.json
14. Historique des modifications
Section titled â14. Historique des modificationsâ| Date | Version | Description | Auteur |
|---|---|---|---|
| YYYY-MM-DD | 1.0 | Création initiale | [Nom] |
| YYYY-MM-DD | 1.1 | [Modification] | [Nom] |
---
**Points clés de ce template :**- Documente la configuration globale d'Astro (mode hybride, adaptateur, intégrations)- Détaille la stratégie de cache par type de page- Inclut la configuration du mode Preview et la génération de types- Fournit les bonnes pratiques de performance et de sécurité- Sépare clairement la configuration, le build, et le déploiement- Inclut un guide de débogage complet