Skip to content

🦥 Lazy Outposts ​

Load heavy outpost handlers on-demand for better code splitting.

⚡ Basic Usage ​

typescript
// Eager — loaded immediately (default)
import { authCheck } from './outposts/auth';

citadel.deployOutpost({ name: 'auth', handler: authCheck });

// Lazy — loaded on first navigation
citadel.deployOutpost({
  name: 'heavy-validation',
  lazy: true,
  handler: () => import('./outposts/heavy-validation'),
});

🔧 How It Works ​

🔑 Key Behavior ​

  • Module loading has no timeout — network latency is unpredictable
  • timeout applies only to handler execution after loading
  • If load fails, error is passed to onError and retry is allowed on next navigation
  • After first successful load, handler is cached — subsequent calls are instant

💡 Example: Lazy Outpost with Heavy Dependencies ​

typescript
// src/outposts/premium.ts — loaded only when needed
import { z } from 'zod'; // Heavy dependency
import type { NavigationOutpostHandler } from 'vue-router-citadel';

const handler: NavigationOutpostHandler = ({ verdicts, to }) => {
  const schema = z.object({ tier: z.enum(['free', 'premium']) });
  const result = schema.safeParse(to.meta);

  if (!result.success || result.data.tier !== 'premium') {
    return { name: 'upgrade' };
  }

  return verdicts.ALLOW;
};

export default handler;
typescript
// main.ts — premium outpost is lazy-loaded
citadel.deployOutpost({
  name: 'premium-check',
  lazy: true,
  timeout: 500, // 500ms for handler execution (loading not counted)
  handler: () => import('./outposts/premium'),
});

📊 Timeline Example ​

EventTimeNotes
Navigation starts0ms
Module load starts0msNo timeout
Module loaded800msSlow network, but OK
Handler starts800msTimeout starts (500ms)
Handler completes900ms100ms execution < 500ms
Navigation completes900msSuccess

If handler took 600ms (> 500ms timeout), it would timeout — but loading time is never counted.

Diagram Legend
ColorMeaning
🟢Success, ALLOW, continue
🟡Warning, redirect, deduplicate
🔴Error, BLOCK, cancel
🔵Logging (when logger is enabled)
🟣Named debug breakpoint (debug: true)

Released under the MIT License.