Skip to content

@astrojs/ solid-js

Astro 整合 为你的 SolidJS 组件启用服务器端渲染和客户端水合作用。

¥This Astro integration enables server-side rendering and client-side hydration for your SolidJS components.

¥Installation

Astro 包含一个 astro add 命令来自动设置官方集成。如果你愿意,你可以改用 安装集成手动

¥Astro includes an astro add command to automate the setup of official integrations. If you prefer, you can install integrations manually instead.

要安装 @astrojs/solid-js,请从项目目录运行以下命令并按照提示操作:

¥To install @astrojs/solid-js, run the following from your project directory and follow the prompts:

Terminal window
npx astro add solid

如果你遇到任何问题,请 请随时在 GitHub 上向我们报告 并尝试下面的手动安装步骤。

¥If you run into any issues, feel free to report them to us on GitHub and try the manual installation steps below.

¥Manual Install

首先,安装 @astrojs/solid-js 包:

¥First, install the @astrojs/solid-js package:

Terminal window
npm install @astrojs/solid-js

大多数包管理器也会安装相关的对等依赖。如果你在启动 Astro 时看到 Cannot find package 'solid-js'(或类似)警告,则需要安装 SolidJS:

¥Most package managers will install associated peer dependencies as well. If you see a Cannot find package 'solid-js' (or similar) warning when you start up Astro, you’ll need to install SolidJS:

Terminal window
npm install solid-js

然后,使用 integrations 属性将集成应用于你的 astro.config.* 文件:

¥Then, apply the integration to your astro.config.* file using the integrations property:

astro.config.mjs
import { defineConfig } from 'astro/config';
import solid from '@astrojs/solid-js';
export default defineConfig({
// ...
integrations: [solid()],
});

¥Getting started

要在 Astro 中使用你的第一个 SolidJS 组件,请前往我们的 UI 框架文档。你将探索:

¥To use your first SolidJS component in Astro, head to our UI framework documentation. You’ll explore:

  • 📦 如何加载框架组件,

  • 💧 客户端水合选项,以及

  • 🤝 将框架混合和嵌套在一起的机会

¥Configuration

Added in: @astrojs/solid-js@4.2.0

你可以通过将带有 devtools: true 的对象传递给你的 solid() 集成配置并将 solid-devtools 添加到项目依赖中来在开发中启用 Solid DevTools

¥You can enable Solid DevTools in development by passing an object with devtools: true to your solid() integration config and adding solid-devtools to your project dependencies:

Terminal window
npm install solid-devtools
astro.config.mjs
import { defineConfig } from 'astro/config';
import solid from '@astrojs/solid-js';
export default defineConfig({
// ...
integrations: [solid({ devtools: true })],
});

¥Options

¥Combining multiple JSX frameworks

当你在同一项目中使用多个 JSX 框架(React、Preact、Solid)时,Astro 需要确定每个组件应使用哪些特定于 JSX 框架的转换。如果你只在项目中添加了一个 JSX 框架集成,则不需要额外的配置。

¥When you are using multiple JSX frameworks (React, Preact, Solid) in the same project, Astro needs to determine which JSX framework-specific transformations should be used for each of your components. If you have only added one JSX framework integration to your project, no extra configuration is needed.

使用 include(必需)和 exclude(可选)配置选项来指定哪些文件属于哪个框架。为你正在使用的每个框架向 include 提供一系列文件和/或文件夹。通配符可用于包含多个文件路径。

¥Use the include (required) and exclude (optional) configuration options to specify which files belong to which framework. Provide an array of files and/or folders to include for each framework you are using. Wildcards may be used to include multiple file paths.

我们建议将通用框架组件放在同一文件夹中(例如 /components/react//components/solid/),以便更轻松地指定包含内容,但这不是必需的:

¥We recommend placing common framework components in the same folder (e.g. /components/react/ and /components/solid/) to make specifying your includes easier, but this is not required:

astro.config.mjs
import { defineConfig } from 'astro/config';
import preact from '@astrojs/preact';
import react from '@astrojs/react';
import svelte from '@astrojs/svelte';
import vue from '@astrojs/vue';
import solid from '@astrojs/solid-js';
export default defineConfig({
// Enable many frameworks to support all different kinds of components.
// No `include` is needed if you are only using a single JSX framework!
integrations: [
preact({
include: ['**/preact/*'],
}),
react({
include: ['**/react/*'],
}),
solid({
include: ['**/solid/*', '**/node_modules/@suid/material/**'],
}),
],
});

¥Usage

像使用任何 UI 框架组件 一样使用 SolidJS 组件。

¥Use a SolidJS component as you would any UI framework component.

¥Suspense Boundaries

为了支持 Solid Resources 和 Lazy Components 而无需过多配置,服务器专用和水合组件会自动封装在顶层 Suspense 边界中,并使用 renderToStringAsync 函数在服务器上渲染。因此,你不需要在异步组件周围添加顶层 Suspense 边界。

¥In order to support Solid Resources and Lazy Components without excessive configuration, server-only and hydrating components are automatically wrapped in top-level Suspense boundaries and rendered on the server using the renderToStringAsync function. Therefore, you do not need to add a top-level Suspense boundary around async components.

例如,你可以使用 Solid 的 createResource 在服务器上获取异步远程数据。远程数据将包含在 Astro 的初始服务器渲染 HTML 中:

¥For example, you can use Solid’s createResource to fetch async remote data on the server. The remote data will be included in the initial server-rendered HTML from Astro:

CharacterName.tsx
function CharacterName() {
const [name] = createResource(() =>
fetch('https://swapi.dev/api/people/1')
.then((result) => result.json())
.then((data) => data.name)
);
return (
<>
<h2>Name:</h2>
{/* Luke Skywalker */}
<div>{name()}</div>
</>
);
}

同样,Solid 的 Lazy Components 也将被解析,并且它们的 HTML 将包含在初始服务器渲染的页面中。

¥Similarly, Solid’s Lazy Components will also be resolved and their HTML will be included in the initial server-rendered page.

非水合 client:only 组件 不会自动封装在 Suspense 边界中。

¥Non-hydrating client:only components are not automatically wrapped in Suspense boundaries.

根据你的喜好随意添加额外的 Suspense 边界。

¥Feel free to add additional Suspense boundaries according to your preference.

More integrations

UI Frameworks

SSR Adapters

Other integrations

Astro 中文网 - 粤ICP备13048890号