Nuxt (SSR)

Since Nuxt renders pages on the server first, the tiun SDK must only initialize on the client. The recommended approach is to set up everything in your app.vue using onMounted.

For plain Vue 3 (no SSR), see the Vue example.


Initialize

Initialize tiun inside onMounted in your app.vue. The !tiun.isInitialized guard prevents double initialization, which can happen in Nuxt during development.

<!-- app.vue -->
<script setup>
import { tiun } from '@tiun/sdk';
import { onMounted, onUnmounted } from 'vue';

onMounted(() => {
  if (!tiun.isInitialized) {
    tiun.init({
      snippetId: 'YOUR_SNIPPET_ID',
      language: 'en', // set to your site's language
    });
  }
});

onUnmounted(() => {
  tiun.destroy();
});
</script>

<template>
  <NuxtPage />
</template>

Checkout

Trigger checkout from any component. The SDK is already initialized in app.vue, so you can call methods directly.


Shared state with useState

Use Nuxt's useState to create reactive state that any component can read. This is SSR-safe and works across the app without prop drilling.

Any component can then read the shared state:


Gate content

Use useState values in any page or component to show or hide content based on productAccess.


Paywall events (time-based)

For time-based billing, use paywallShow and paywallHide to control access. Wire them to shared state in app.vue.

Then in any component:


Content management (time-based)

Track route changes to keep billing accurate. Use router.afterEach with an import.meta.client guard so it only runs in the browser. Send the initial content state when the SDK is ready.


Alternative: plugin approach

Instead of app.vue, you can initialize tiun in a Nuxt plugin. The .client.ts suffix ensures it only runs in the browser.

This runs before any component mounts, but you lose the onUnmounted cleanup. The app.vue approach gives you more control over the full lifecycle.

Last updated

Was this helpful?