Remix
Remix 是一個預設進行伺服器端渲染的全端框架。使用 useEffect 在根元件中僅於客戶端載入 Wink CDN。
@wink/elements npm 套件 安裝並載入帶有 TypeScript 型別的 CDN 套件。
npm install @wink/elements在 app/root.tsx 載入
Section titled “在 app/root.tsx 載入”useEffect 僅在瀏覽器執行,伺服器端渲染時不會執行:
import { useEffect } from 'react';import { Links, Meta, Outlet, Scripts, ScrollRestoration,} from '@remix-run/react';import { load } from '@wink/elements';
export default function App() { useEffect(() => { load({ clientId: import.meta.env.VITE_WINK_CLIENT_ID }); }, []);
return ( <html lang="en"> <head> <Meta /> <Links /> </head> <body> <Outlet /> <ScrollRestoration /> <Scripts /> </body> </html> );}Remix(基於 Vite,v2.8+)支援 import.meta.env 用於瀏覽器可見的變數。加上 VITE_ 前綴即可暴露給瀏覽器套件:
VITE_WINK_CLIENT_ID=your-client-id對於使用 Node.js adapter 的舊版 Remix 設定,請從 root loader 回傳 client ID,並在元件中讀取:
import { json } from '@remix-run/node';import { useLoaderData } from '@remix-run/react';
export async function loader() { return json({ winkClientId: process.env.WINK_CLIENT_ID });}
export default function App() { const { winkClientId } = useLoaderData<typeof loader>();
useEffect(() => { if (winkClientId) { load({ clientId: winkClientId }); } }, [winkClientId]);
// ...}在路由中使用
Section titled “在路由中使用”export default function HotelsRoute() { return ( <main> <wink-content-loader layout="HOTEL" id="YOUR_LAYOUT_ID" /> </main> );}TypeScript 型別
Section titled “TypeScript 型別”從 React 整合頁面 加入 JSX intrinsic 宣告檔 — Remix 底層使用 React。