---
title: "Share by link"
description: "Let a person share one of their rows by link: mark the table shareable, react when a link is turned on, rotate or revoke it with useShareLink, read the row on a public page whose images stay signed, give the link a preview card, and carry a made-with badge and the sharer's referral code."
canonical_url: "https://nuxt-backend.dev/docs/build/data/files/share-links"
---
# Share by link

> Let a person share one of their rows by link: mark the table shareable, react when a link is turned on, rotate or revoke it with useShareLink, read the row on a public page whose images stay signed, give the link a preview card, and carry a made-with badge and the sharer's referral code.

Version: nuxt-backend 0.6.x · Tab: Build · Kind: how-to · Status: stable

Every page: [llms.txt](https://nuxt-backend.dev/llms.txt) · Skill for coding agents: [nuxt-backend](https://nuxt-backend.dev/.well-known/skills/nuxt-backend/SKILL.md)

Commands use npm; use your project's package manager.

A Panels author sends a finished strip to a friend who has no account. The author turns a link on, and anyone holding it can read that one strip on a public page, with a preview card when the link is pasted into a chat. Convex has no sharing of its own. The package adds share links whose token is stored only as a hash, rotation and revocation, a preview card from the row's image, and the share loop: a "made with" badge that carries the sharer's referral code.

## Mark the table shareable

`storage.shareable(table, options)` says who may share a row of the table, where its public page lives and which image its preview card shows:

```ts [backend/storage.ts]
import { setupStorage } from 'nuxt-backend/storage'
import { components } from './_generated/api'
import type { DataModel } from './_generated/dataModel'

// DataModel types the row each table's callbacks receive.
export const storage = setupStorage<DataModel>(components, { stripMetadata: true })

storage.shareable('strips', {
  path: '/s',
  canShare: (ctx, strip) => strip.authorId === ctx.user.id && strip.status === 'ready',
  ogImage: strip => strip.coverId,
  onEnable: async (ctx, strip) => {
    await ctx.db.patch('strips', strip._id, { sharedAt: Date.now() })
  },
})

export const { generateUploadUrl, getFileUrl, getShareLink, enableShareLink, revokeShareLink } = storage.api
```

<table>
<thead>
  <tr>
    <th>
      Option
    </th>
    
    <th>
      Effect
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        path
      </code>
    </td>
    
    <td>
      Where the public page lives: a link reads <code>
        SITE_URL
      </code>
      
       + <code>
        path
      </code>
      
       + <code>
        /
      </code>
      
       + the token
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        canShare
      </code>
    </td>
    
    <td>
      Whether the signed-in caller may turn the row's link on or rotate it. It receives the row
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        ogImage
      </code>
    </td>
    
    <td>
      The storage id of the image the preview card shows, or <code>
        null
      </code>
      
       for none. It receives the row
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        onEnable
      </code>
    </td>
    
    <td>
      Runs in <code>
        enableShareLink
      </code>
      
      's mutation once the link is on: record an event, tick off a checklist step, stamp the row. It receives the mutation's context with <code>
        ctx.user
      </code>
      
      , the row, and <code>
        { url, rotated }
      </code>
      
      , <code>
        rotated
      </code>
      
       when the link replaced a live one. A throw rolls the link back with it. <code>
        url
      </code>
      
       is the live link: hand it on (an email to the author), never store or log it
    </td>
  </tr>
</tbody>
</table>

<warning>

`link.url` in `onEnable` is a bearer secret: anyone holding it reads the row. The package stores only its hash. Never write it to a table, a log or an analytics event, where it would outlive a rotation and reach people the author never sent it to. Record that a link was turned on, and by whom, instead.

</warning>

`getShareLink`, `enableShareLink` and `revokeShareLink` take `{ table, id }` and answer signed-in accounts only. Turning a link on asks `canShare`. Revoking asks `canShare`, or that the caller turned the link on, so a strip that changed since can still be taken offline.

## Turn a link on from a page

`useShareLink(table, id)` reads whether the row is shared and turns its link on and off:

```vue [app/components/ShareStrip.vue]
<script setup lang="ts">
const props = defineProps<{ stripId: string }>()
const { shared, sharedAt, url, enable, revoke, isPending, error } = useShareLink('strips', () => props.stripId)
</script>

<template>
  <section aria-labelledby="share-heading">
    <h2 id="share-heading">Share this strip</h2>
    <p v-if="url">Anyone with this link can read the strip: <code>{{ url }}</code></p>
    <p v-else-if="shared">Shared since {{ new Date(sharedAt!).toLocaleDateString() }}.</p>
    <button type="button" :aria-disabled="isPending || undefined" @click="isPending || enable()">
      {{ shared ? 'Make a new link' : 'Turn on a link' }}
    </button>
    <button v-if="shared" type="button" :aria-disabled="isPending || undefined" @click="isPending || revoke()">
      Turn the link off
    </button>
    <p v-if="error" role="alert">{{ error.message }}</p>
  </section>
</template>
```

<table>
<thead>
  <tr>
    <th>
      It returns
    </th>
    
    <th>
      
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        shared
      </code>
      
      , <code>
        sharedAt
      </code>
    </td>
    
    <td>
      Whether the row has a live link, and since when: live, in every open tab
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        url
      </code>
    </td>
    
    <td>
      The link, right after <code>
        enable()
      </code>
      
       in this tab, then <code>
        null
      </code>
      
       after a reload. The token is shown once
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        enable()
      </code>
    </td>
    
    <td>
      Mints a link, replacing any earlier one, and resolves with <code>
        { token, url }
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        revoke()
      </code>
    </td>
    
    <td>
      Turns the link off. A page that holds it reads <code>
        null
      </code>
      
       from then on
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        isPending
      </code>
      
      , <code>
        error
      </code>
    </td>
    
    <td>
      An action on its way, and why the last one failed, as <code>
        { code, data, message }
      </code>
    </td>
  </tr>
</tbody>
</table>

## Read the shared row on a public page

A public query turns the token back into the row with `storage.resolveShareLink(ctx, table, token)`. It returns `{ id, sharedBy, ogImageUrl }`, or `null` for an unknown or revoked token. Return only what a stranger may see. Serve the row's images as [expiring URLs](https://nuxt-backend.dev/docs/build/data/files/images#serve-a-private-file-for-a-while), so turning the link off cuts them too: the query runs again on each half-hourly tick of the URL clock while the page is open, so they never expire under a reader.

```ts [backend/strips.ts]
import { HOUR } from 'nuxt-backend/rate-limit'
import { v } from 'convex/values'
import { query } from './_generated/server'
import { referrals } from './referrals'
import { storage } from './storage'

export const shared = query({
  args: { token: v.string() },
  handler: async (ctx, { token }) => {
    const link = await storage.resolveShareLink(ctx, 'strips', token)
    const strip = link ? await ctx.db.get('strips', link.id) : null
    if (!link || !strip || strip.status !== 'ready') return null
    return {
      title: strip.title,
      coverUrl: strip.coverId ? await storage.signedUrl(ctx, strip.coverId, { ttl: 2 * HOUR }) : null,
      ogImageUrl: link.ogImageUrl,
      referralCode: await referrals.codeFor(ctx, link.sharedBy),
    }
  },
})
```

```vue [app/pages/s/[token].vue]
<script setup lang="ts">
import { api } from '#backend/api'

const route = useRoute()
const { data: strip } = await useAsyncQuery(api.strips.shared, { token: route.params.token as string })
// The token is case-sensitive: name the link as it is, which nothing lowercases.
const canonical = `${useRequestURL().origin}${route.path}`

useHead({ link: [{ rel: 'canonical', href: canonical }] })
useSeoMeta({
  robots: 'noindex',
  referrer: 'no-referrer',
  ogUrl: canonical,
  ogTitle: () => strip.value?.title ?? 'A strip made with Panels',
  ogImage: () => strip.value?.ogImageUrl ?? undefined,
  twitterCard: 'summary_large_image',
})
</script>

<template>
  <main>
    <article v-if="strip">
      <h1>{{ strip.title }}</h1>
      <img v-if="strip.coverUrl" :src="strip.coverUrl" alt="" width="960">
      <a
        :href="strip.referralCode ? `/r/${strip.referralCode}` : '/'"
        :title="strip.referralCode ? 'Made with Panels. The author earns credits when you join.' : undefined"
      >
        Made with Panels: make your own
      </a>
    </article>
    <p v-else>This link no longer works. Ask the author for a new one.</p>
  </main>
</template>
```

The page renders on the server for a visitor with no session, so a chat app's crawler reads the card. Keep `noindex` unless the shared rows are meant for search engines: a link is unlisted, not secret, once someone posts it. `no-referrer` keeps the token out of the requests the page makes to other sites.

## Give the link a preview card

`ogImageUrl` is an [expiring private URL](https://nuxt-backend.dev/docs/build/data/files/images#serve-a-private-file-for-a-while) for the `ogImage` file, valid for seven days and minted again on each read. A crawler can fetch it without a session; it is served by the route `registerBackendRoutes(http, { storage })` mounts ([Show images](https://nuxt-backend.dev/docs/build/data/files/images#serve-a-private-file-for-a-while)). Use a 1200 × 630 image for a large card; the provider can crop one from the cover.

The token is case-sensitive, so the page names its own canonical URL and `og:url`, the path as it came. [nuxt-seo-utils](https://nuxtseo.com/seo-utils) (alone or through `@nuxtjs/seo`) lowercases the canonical URLs it adds by default, which would turn the link into a dead one; a page's own canonical link and `og:url` replace its defaults, so the rest of the app keeps lowercase canonicals. A card rendered by nuxt-og-image under `/_og/**` is served with `Cross-Origin-Resource-Policy: cross-origin`, so chat apps can fetch it ([Resources other sites load](https://nuxt-backend.dev/docs/ship/security#resources-other-sites-load)).

## Close the share loop

The badge links to `/r/<code>`, the sharer's [referral link](https://nuxt-backend.dev/docs/build/growth/offers/referrals), so a reader who signs up is credited to the author. `referrals.codeFor(ctx, userId)` returns the person's code, or `null` when they have none yet or turned it off for their share pages. A mutation or an action makes the code on its first call; a query reads the one that exists, as the page's query above does, so call it from the mutation that turns the link on:

```ts [backend/strips.ts]
export const publish = mutation({
  args: { id: v.id('strips') },
  handler: async (ctx, { id }) => {
    // …check the strip is the caller's and ready, then:
    await referrals.codeFor(ctx, (await ctx.auth.getUserIdentity())!.subject)
  },
})
```

The referral program decides the rewards and caps; the share page only carries the code. Say on the page that the link is a referral, as the program's disclosure asks: the badge's `title` above does.

## What is stored

<table>
<thead>
  <tr>
    <th>
      Stored
    </th>
    
    <th>
      Not stored
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      The table, the row's id, the SHA-256 of the token, when it was turned on and by whom
    </td>
    
    <td>
      The token, and the link built from it
    </td>
  </tr>
</tbody>
</table>

A row's link is deleted with its owner's account erasure. When your code deletes a row, call `storage.revokeShareLink(ctx, table, id)` in the same mutation.

## What happens when…

<table>
<thead>
  <tr>
    <th>
      Situation
    </th>
    
    <th>
      Result
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      The table is not marked shareable
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/reference/backend/errors#invalid_input">
        <code>
          invalid_input
        </code>
      </a>
      
       on <code>
        table
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        canShare
      </code>
      
       says no
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/reference/backend/errors#forbidden">
        <code>
          forbidden
        </code>
      </a>
      
      , reason <code>
        cannot_share
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      A signed-out caller turns a link on
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/reference/backend/errors#unauthenticated">
        <code>
          unauthenticated
        </code>
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      The token is unknown, rotated or revoked
    </td>
    
    <td>
      <code>
        resolveShareLink
      </code>
      
       returns <code>
        null
      </code>
    </td>
  </tr>
</tbody>
</table>

## Limits

- **Revoking stops the page, and expiring URLs within their ttl.** A plain file URL (`storage.fileUrl`) works until the file is deleted, and a visitor who opened the page has it: serve signed URLs, as above, or store a new file and `release` the old one.
- **One link per row.** Turning a link on again rotates it.
- **No view counts or expiry dates.** Count views with a [counter](https://nuxt-backend.dev/docs/build/data/read-write/aggregates#counts-with-many-writers), and expire links with a [retention rule](https://nuxt-backend.dev/docs/build/data/retention).

## Test it

```ts [backend/strips.test.ts]
import { asUser, defineBackendTestApp } from 'nuxt-backend/test'
import { expect, test } from 'vitest'
import { api } from './_generated/api'
import schema from './schema'

const testApp = defineBackendTestApp(schema, import.meta.glob('./**/*.ts'))

test('a revoked link stops working', async () => {
  const t = testApp()
  const author = await asUser(t)
  const id = await author.mutation(api.strips.create, { title: 'Monday' })
  await author.mutation(api.strips.publish, { id })
  const { token } = await author.mutation(api.storage.enableShareLink, { table: 'strips', id })
  expect(await t.query(api.strips.shared, { token })).toMatchObject({ title: 'Monday' })
  await author.mutation(api.storage.revokeShareLink, { table: 'strips', id })
  expect(await t.query(api.strips.shared, { token })).toBeNull()
})
```

## Related

- [Show images](https://nuxt-backend.dev/docs/build/data/files/images): the provider and expiring URLs the public page uses.
- [Run a referral program](https://nuxt-backend.dev/docs/build/growth/offers/referrals): the codes the badge carries.
- [Render data on the server](https://nuxt-backend.dev/docs/build/data/read-write/server-rendering): `useAsyncQuery` on a public page.
- [Delete old data on a schedule](https://nuxt-backend.dev/docs/build/data/retention#store-a-tokens-hash-not-the-token): token helpers for links of your own.


## Sitemap

See the full [sitemap](https://nuxt-backend.dev/sitemap.md) for all pages.
