@@ -4,7 +4,9 @@ description: Simple integration of Vyuh with Sanity.io, the default CMS
4
4
---
5
5
6
6
import { PubBadge } from ' @/components/PubBadge'
7
- import { Aside } from ' @astrojs/starlight/components'
7
+ import { Image } from ' astro:assets'
8
+ import { Aside , Badge } from ' @astrojs/starlight/components'
9
+ import refreshButtonImage from ' ./images/refresh-button.png'
8
10
9
11
Now that we know how to setup a feature in Vyuh, let's see how we can integrate
10
12
it with a CMS. We will be using [ Sanity.io] ( https://sanity.io ) for this example,
@@ -282,6 +284,134 @@ launches on the correct port.
282
284
283
285
</Aside >
284
286
287
+ ## 6. Enable Live Content Editing <sup ><Badge text = " New" /></sup >
288
+
289
+ One of the most powerful features of Vyuh is the ability to see content changes
290
+ in real-time without having to restart your app or manually refresh. This
291
+ feature, called Live Content Editing, significantly improves your development
292
+ workflow.
293
+
294
+ ### How to Enable Live Routes
295
+
296
+ Enabling Live Content Editing is as simple as setting the ` useLiveRoute ` flag to
297
+ ` true ` on the ` DefaultContentPlugin ` . Here's how you can modify your main.dart
298
+ file:
299
+
300
+ ``` dart {13} title="lib/main.dart"
301
+ void main() async {
302
+ WidgetsFlutterBinding.ensureInitialized();
303
+
304
+ vc.runApp(
305
+ initialLocation: '/blog',
306
+ features: () => [
307
+ system.feature,
308
+ developer.feature,
309
+ blog.feature,
310
+ ],
311
+ plugins: PluginDescriptor(
312
+ content: DefaultContentPlugin(
313
+ useLiveRoute: true, // Enable live routes
314
+ provider: SanityContentProvider(
315
+ SanityClient(
316
+ SanityConfig(
317
+ dataset: 'your_dataset',
318
+ projectId: 'your_project_id',
319
+ token:'your_token',
320
+ perspective: Perspective.previewDrafts, // To see draft content
321
+ useCdn: false, // Required for live updates
322
+ ),
323
+ ),
324
+ ),
325
+ )
326
+ ),
327
+ );
328
+ }
329
+ ```
330
+
331
+ ### Important Configuration Notes
332
+
333
+ For Live Content Editing to work properly, make sure to:
334
+
335
+ 1 . Set ` useLiveRoute: true ` on the ` DefaultContentPlugin `
336
+ 2 . Set ` perspective: Perspective.previewDrafts ` to see draft content before
337
+ publishing
338
+ 3 . Set ` useCdn: false ` as we want to work with draft content
339
+ 4 . Provide a valid Sanity token with appropriate permissions. Without a valid
340
+ token this will fail.
341
+
342
+ ### How It Works
343
+
344
+ Behind the scenes, Vyuh leverages Sanity's Live Content API to establish a
345
+ real-time connection between your app and the CMS. When content changes are made
346
+ in the Sanity Studio, the API sends those changes to your app, which then
347
+ updates the UI accordingly.
348
+
349
+ This works in both development and production environments. In production, you
350
+ would typically need to publish your content before it appears on customer
351
+ devices, unless you specifically configure your app to show draft content.
352
+
353
+ <Aside type = " tip" title = " Development vs. Production" >
354
+
355
+ In development, it's useful to see draft content immediately using
356
+ ` Perspective.previewDrafts ` .
357
+
358
+ For production apps, you might want to switch to ` Perspective.published ` to
359
+ ensure only published content is visible to users.
360
+
361
+ </Aside >
362
+
363
+ ### Manual Route Refreshing
364
+
365
+ In addition to live updates, Vyuh also provides a way to manually refresh routes
366
+ through the ` allowRouteRefresh ` parameter. This is useful when you want to give
367
+ users the ability to pull-to-refresh content or when you need a refresh button
368
+ in your UI.
369
+
370
+ ``` dart {14} title="lib/main.dart"
371
+ void main() async {
372
+ WidgetsFlutterBinding.ensureInitialized();
373
+
374
+ vc.runApp(
375
+ initialLocation: '/blog',
376
+ features: () => [
377
+ system.feature,
378
+ developer.feature,
379
+ blog.feature,
380
+ ],
381
+ plugins: PluginDescriptor(
382
+ content: DefaultContentPlugin(
383
+ useLiveRoute: true,
384
+ allowRouteRefresh: true, // Enable manual refreshing
385
+ provider: SanityContentProvider(
386
+ SanityClient(
387
+ SanityConfig(
388
+ dataset: 'your_dataset',
389
+ projectId: 'your_project_id',
390
+ token:'your_token',
391
+ perspective: Perspective.previewDrafts,
392
+ useCdn: false,
393
+ ),
394
+ ),
395
+ ),
396
+ )
397
+ ),
398
+ );
399
+ }
400
+ ```
401
+
402
+ <Image src = { refreshButtonImage } alt = { ' Manual Refresh Button' } />
403
+
404
+ This parameter works with both live routes and static routes, giving you
405
+ flexibility in how content updates are handled in your application.
406
+
407
+ <Aside type = " note" >
408
+
409
+ Even when ` useLiveRoute ` is enabled, manual refreshing can still be useful in
410
+ certain scenarios, such as when you want to force an immediate refresh rather
411
+ than waiting for the next live update.
412
+
413
+ </Aside >
414
+
285
415
## Summary
286
416
287
417
This guide gave you a quick way of creating content on Sanity and seeing it live
0 commit comments