Astro 4.3

作者
Erika
Emanuele Stoppa
Matthew Phillips
Nate Moore
Bjorn Lu

Astro 4.3 现已推出!此版本包含一项新的实验性 i18n 功能供您试用,以及在构建输出、组件 prop 类型、Markdown 图像等方面进行的改进。

亮点包括

如何升级

要利用最新功能,请确保您正在运行最新版本的 Astro。您可以通过运行 @astrojs/upgrade 命令升级到 Astro 4.3

npx @astrojs/upgrade

或者通过运行您的包管理器的升级命令

npm install astro@latest
pnpm upgrade astro --latest
yarn upgrade astro --latest

实验性:为 i18n 添加域支持

Astro 4.3 添加了一个实验性的 domains i18n 配置。这允许您为不同的支持区域设置指定不同的主域或子域。

例如,您现在可以使用 example.com 作为您的英文网站,fr.example.com 作为您的法文网站,以及 example.es 作为您的西班牙文网站。在 astro.config.mjs 文件中,启用实验性标志 i18nDomains 并使用 i18n.domains 将您所有或部分区域设置映射到域。

astro.config.mjs
import {defineConfig} from "astro/config"
export default defineConfig({
site: "https://example.com",
output: "server", // required, with no prerendered pages
adapter: node({
mode: 'standalone',
}),
i18n: {
defaultLocaLe: 'en',
locales: ['en', 'es', 'pt_BR', 'pt', 'fr'],
domains: {
fr: "https://fr.example.com",
es: "https://example.es"
},
routing: {
prefixDefaultLocale: true,
}
},
experimental: {
i18nDomains: true
},
})

请注意,此功能要求整个网站都通过服务器渲染,不包含预渲染页面。目前,支持 @astrojs/node@astrojs/vercel 适配器,未来将支持更多适配器!

有关此实验性路由功能的更多详细信息和限制,请参阅我们的国际化文档

更好地控制 HTML 文件输出

此版本添加了一个名为 preserve 的新 build.format 选项,让您可以更好地控制生产构建中生成的 HTML 文件。

当前的配置选项(filedirectory)分别将所有 HTML 页面构建为与路由名称匹配的文件(例如 /about.html),或者将所有文件构建为嵌套目录结构中的 index.html(例如 /about/index.html)。在使用 file 配置选项时,无法创建单独的索引页面(例如 /about/index.html)。

为了避免对 file 引入破坏性更改,我们添加了新的 preserve 格式,它将保留文件系统的结构,并确保其在生产环境中得到镜像。

  • about.astro 变为 about.html
  • about/index.astro 变为 about/index.html

所见即所得!此功能可提供更好的兼容性,特别是对于对文件结构有严格要求的某些 Web 服务器而言。

有关更多详细信息,请参阅build.format 配置选项参考

添加 ComponentProps 类型实用程序

Astro 现在从 astro/types 包含一个新的 ComponentProps 类型导出,用于获取 Astro 组件的 props 类型。这类似于 React.ComponentProps 或 Svelte 的 ComponentProps

此类型导出允许您引用其他组件接受的 Props,即使该组件不直接导出该 Props 类型。

---
import type { ComponentProps } from 'astro/types';
import Button from "./Button.astro";
type MyButtonProps = ComponentProps<typeof Button>;
---

修复 Markdown 中不使用相对路径指示符的图像

此前,在 Markdown 中使用图像时,如果不使用相对路径指示符(例如 ./../),Astro 会抛出错误。

现在,您可以在 Markdown 文件中使用标准 ![](img.png) 语法来引用同一文件夹中的图像:无需相对路径指示符!

无需更新您的项目;您现有的图像将继续正常工作。但是,您可以安全地从这些 Markdown 图像中删除任何相对路径指示符,因为它们不再是必需的……这正是 Markdown 规范所期望的!

![A cute dog](./dog.jpg)
![A cute dog](dog.jpg)
<!-- This dog lives in the same folder as my article! -->

感谢 Oliver Speir 贡献此修复!

错误修复

一如既往,此版本还包括其他错误修复。查看发行说明了解更多信息。