|
2 | 2 | title: Integrating Weaverse into an Existing Hydrogen Project |
3 | 3 | description: Enhance your existing Hydrogen project by integrating the Weaverse SDK for visual page building and theme customization. |
4 | 4 | publishedAt: November 20, 2023 |
5 | | -updatedAt: April 24, 2025 |
| 5 | +updatedAt: May 07, 2025 |
6 | 6 | order: 0 |
7 | 7 | published: true |
8 | 8 | --- |
@@ -258,7 +258,94 @@ export function getWeaverseCsp(request: Request, context: AppLoadContext) { |
258 | 258 | } |
259 | 259 | ``` |
260 | 260 |
|
261 | | -## Step 4: Integrate Weaverse into Your Application |
| 261 | +## Step 4: Add Weaverse Client to Hydrogen App Context |
| 262 | + |
| 263 | +A crucial step is to initialize the Weaverse client and add it to your Hydrogen app load context. This allows your application to access Weaverse functionality throughout your routes. |
| 264 | + |
| 265 | +### 1. Update Your Context File |
| 266 | + |
| 267 | +Modify your app context file (usually `app/lib/context.ts` or similar) to initialize and include the WeaverseClient: |
| 268 | + |
| 269 | +```typescript |
| 270 | +// app/lib/context.ts |
| 271 | +import { WeaverseClient } from "@weaverse/hydrogen"; |
| 272 | +import { themeSchema } from "~/weaverse/schema.server"; |
| 273 | +import { components } from "~/weaverse/components"; |
| 274 | +// ... other imports |
| 275 | + |
| 276 | +export async function createAppLoadContext( |
| 277 | + request: Request, |
| 278 | + env: Env, |
| 279 | + executionContext: ExecutionContext, |
| 280 | +) { |
| 281 | + // Your existing context setup code... |
| 282 | + // Create the Hydrogen context |
| 283 | + const hydrogenContext = createHydrogenContext({ |
| 284 | + env, |
| 285 | + request, |
| 286 | + cache, |
| 287 | + waitUntil, |
| 288 | + session, |
| 289 | + i18n: getLocaleFromRequest(request), |
| 290 | + // ... other Hydrogen context options |
| 291 | + }); |
| 292 | + |
| 293 | + // Return the context with Weaverse client added |
| 294 | + return { |
| 295 | + ...hydrogenContext, |
| 296 | + weaverse: new WeaverseClient({ |
| 297 | + ...hydrogenContext, |
| 298 | + request, |
| 299 | + cache, |
| 300 | + themeSchema, |
| 301 | + components, |
| 302 | + }), |
| 303 | + }; |
| 304 | +} |
| 305 | +``` |
| 306 | + |
| 307 | +This modification: |
| 308 | +1. Imports the `WeaverseClient` from the Weaverse Hydrogen SDK |
| 309 | +2. Imports your theme schema and components from the files created in the previous step |
| 310 | +3. Initializes a new `WeaverseClient` instance with necessary context |
| 311 | +4. Adds the Weaverse client to your app context, making it available via `context.weaverse` in loaders and actions |
| 312 | + |
| 313 | +### 2. Ensure Server Entry Point Uses Updated Context |
| 314 | + |
| 315 | +Your server entry point (typically `server.ts`) should already use your context function: |
| 316 | + |
| 317 | +```typescript |
| 318 | +// server.ts |
| 319 | +import { createAppLoadContext } from '~/lib/context'; |
| 320 | + |
| 321 | +export default { |
| 322 | + async fetch( |
| 323 | + request: Request, |
| 324 | + env: Env, |
| 325 | + executionContext: ExecutionContext, |
| 326 | + ): Promise<Response> { |
| 327 | + // Create app context with Weaverse client |
| 328 | + const appLoadContext = await createAppLoadContext( |
| 329 | + request, |
| 330 | + env, |
| 331 | + executionContext, |
| 332 | + ); |
| 333 | + |
| 334 | + // Use context in request handler |
| 335 | + const handleRequest = createRequestHandler({ |
| 336 | + build: remixBuild, |
| 337 | + mode: process.env.NODE_ENV, |
| 338 | + getLoadContext: () => appLoadContext, |
| 339 | + }); |
| 340 | + |
| 341 | + // ... rest of your server code |
| 342 | + } |
| 343 | +} |
| 344 | +``` |
| 345 | + |
| 346 | +--- |
| 347 | + |
| 348 | +## Step 5: Integrate Weaverse into Your Application |
262 | 349 |
|
263 | 350 | Now, let's modify your existing Hydrogen files to enable Weaverse. |
264 | 351 |
|
@@ -433,7 +520,7 @@ export default function Product() { |
433 | 520 |
|
434 | 521 | The `WeaverseContent` component will automatically render the content based on the page type and handle, using the components registered in your `components.ts` file. |
435 | 522 |
|
436 | | -## Step 5: Run and Connect |
| 523 | +## Step 6: Run and Connect |
437 | 524 |
|
438 | 525 | 1. **Start your dev server:** `npm run dev` |
439 | 526 | 2. **Open Weaverse Studio:** Go to your project in Weaverse. |
|
0 commit comments