@astrojs/ solid-js
本頁內容尚未翻譯。
This Astro integration enables rendering and client-side hydration for your SolidJS components.
Installation
Section titled “Installation”Astro includes an astro add command to automate the setup of official integrations. If you prefer, you can install integrations manually instead.
To install @astrojs/solid-js, run the following from your project directory and follow the prompts:
npx astro add solidpnpm astro add solidyarn astro add solidIf you run into any issues, feel free to report them to us on GitHub and try the manual installation steps below.
Manual Install
Section titled “Manual Install”First, install the @astrojs/solid-js package:
npm install @astrojs/solid-jspnpm add @astrojs/solid-jsyarn add @astrojs/solid-jsMost package managers will install associated peer dependencies as well. If you see a Cannot find package 'solid-js' or Cannot find package '@solidjs/web' warning when you start up Astro, you’ll need to install SolidJS and its web renderer:
npm install solid-js @solidjs/webpnpm add solid-js @solidjs/webyarn add solid-js @solidjs/webThen, apply the integration to your astro.config.* file using the integrations property:
import { defineConfig } from 'astro/config';import solidJs from '@astrojs/solid-js';
export default defineConfig({ // ... integrations: [solidJs()],});And add the following code to the tsconfig.json file.
{ "extends": "astro/tsconfigs/strict", "include": [".astro/types.d.ts", "**/*"], "exclude": ["dist"], "compilerOptions": { "jsx": "preserve", "jsxImportSource": "@solidjs/web" }}Getting started
Section titled “Getting started”To use your first SolidJS component in Astro, head to our UI framework documentation. You’ll explore:
- 📦 how framework components are loaded,
- 💧 client-side hydration options, and
- 🤝 opportunities to mix and nest frameworks together
Configuration
Section titled “Configuration”compiler
Section titled “compiler”Type: "babel" | "native"
Default: "native"
@astrojs/solid-js@8.0.0
新
Selects the compiler that transforms your Solid JSX. See Solid’s compiler documentation for each compiler and its requirements.
import { defineConfig } from 'astro/config';import solid from '@astrojs/solid-js';
export default defineConfig({ integrations: [solid({ compiler: 'babel' })],});serverFunctions
Section titled “serverFunctions”Type: boolean | object
@astrojs/solid-js@8.0.0
新
Enables Solid’s experimental server functions using the "use server" directive. Set this to true to use the defaults, or pass an options object to customize their behavior. Server functions require an adapter to handle requests at runtime. See Solid’s serverFunctions documentation for the available options.
The following example enables server functions in a project using the Node adapter:
import { defineConfig } from 'astro/config';import solid from '@astrojs/solid-js';import node from '@astrojs/node';
export default defineConfig({ adapter: node({ mode: 'standalone' }), integrations: [solid({ serverFunctions: true })],});Options
Section titled “Options”Combining multiple JSX frameworks
Section titled “Combining multiple JSX frameworks”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.
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.
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:
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/**'], }), ],});Use a SolidJS component as you would any UI framework component.
Loading boundaries
Section titled “Loading boundaries”Server-only and hydrating Solid components are rendered on the server using renderToStream. Astro waits for their async work to settle before including the resulting HTML in the page. The integration does not automatically wrap components in a boundary.
For example, you can use Solid’s createMemo to fetch async remote data on the server and read it inside a Loading boundary. The resolved data will be included in the server-rendered HTML from Astro:
import { createMemo, Loading } from 'solid-js';
function CharacterName() { const name = createMemo(() => fetch('https://swapi.dev/api/people/1') .then((result) => result.json()) .then((data) => data.name) );
return ( <Loading fallback={<p>Loading character...</p>}> <h2>Name:</h2> {/* Luke Skywalker */} <div>{name()}</div> </Loading> );}Similarly, Solid’s Lazy Components will also be resolved and their HTML will be included in the initial server-rendered page.
client:only components skip server rendering. Use a Loading boundary to display a fallback while their async data loads in the browser.