---
title: "Show images"
description: "Show a stored image with <StorageImage>, at the size and format a page needs with <NuxtImg> and the backend image provider, or behind an expiring private URL that stops working after a set time."
canonical_url: "https://nuxt-backend.dev/docs/build/data/files/images"
---
# Show images

> Show a stored image with <StorageImage>, at the size and format a page needs with <NuxtImg> and the backend image provider, or behind an expiring private URL that stops working after a set time.

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.

Show a strip's cover at 320 pixels wide in a list, at full size on its page, and as a private download that stops working after an hour. Convex serves a file's original bytes at a URL that works for whoever holds it, with no transforms. The package adds the access check before a URL leaves the backend, an image provider for `@nuxt/image` so your host's optimizer does the resizing, and expiring private URLs.

## Show one file

`<StorageImage>` comes from the base module. It reads the URL through a query, renders its default slot while the URL loads, and shows the `missing` slot once the file is gone. Pass it `getFileUrl` from `backend/storage.ts`, which answers only the file's owner and their workspace:

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

defineProps<{ coverId: string | null }>()
</script>

<template>
  <StorageImage :get-url="api.storage.getFileUrl" :storage-id="coverId" alt="Strip cover" width="320">
    <div class="cover-skeleton" />
    <template #missing>
      <div class="cover-placeholder" />
    </template>
  </StorageImage>
</template>
```

`get-url` is read once, at setup; `storage-id` is reactive. Every other attribute goes onto the `<img>`. Give it an `alt` that says what the image shows, or `alt=""` when it is decoration.

## Resize with the image provider

With `@nuxt/image` in your modules, the module registers a provider named `backend` and adds your deployment's file host to `image.domains`. The provider takes a file URL your query returned and hands the size and format to the optimizer your host runs.

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['@nuxt/image', 'nuxt-backend'],
})
```

Return the URL from the query that lists the rows, with `storage.fileUrl(ctx, id)` once your query has checked who may see them:

```ts [backend/strips.ts]
import { workspace } from './functions'
import { storage } from './storage'

export const list = workspace.query({
  args: {},
  handler: async (ctx) => {
    const strips = await ctx.db.query('strips')
      .withIndex('by_workspaceId', q => q.eq('workspaceId', ctx.workspace.id))
      .order('desc')
      .take(50)
    return Promise.all(strips.map(async strip => ({
      _id: strip._id,
      title: strip.title,
      coverUrl: strip.coverId ? await storage.fileUrl(ctx, strip.coverId) : null,
    })))
  },
})
```

```vue [app/components/StripCard.vue]
<script setup lang="ts">
defineProps<{ strip: { title: string, coverUrl: string | null } }>()
</script>

<template>
  <NuxtImg
    v-if="strip.coverUrl"
    provider="backend"
    :src="strip.coverUrl"
    :alt="strip.title"
    width="320"
    format="webp"
    loading="lazy"
  />
</template>
```

The provider honours the modifiers `@nuxt/image` defines, `width`, `height`, `fit`, `format` and `quality` among them, and the `sizes` and `densities` that build a `srcset`.

<table>
<thead>
  <tr>
    <th>
      Host
    </th>
    
    <th>
      Optimizer
    </th>
    
    <th>
      What you set
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      Vercel
    </td>
    
    <td>
      Vercel's image optimization, counted in your Vercel plan's usage (<a href="https://nuxt-backend.dev/docs/services/vercel">
        Vercel
      </a>
      
      )
    </td>
    
    <td>
      Nothing: the module adds the file host to the allowed domains
    </td>
  </tr>
  
  <tr>
    <td>
      Any other host
    </td>
    
    <td>
      IPX, <code>
        @nuxt/image
      </code>
      
      's own, in your Nitro server
    </td>
    
    <td>
      Nothing: the same domain list applies
    </td>
  </tr>
  
  <tr>
    <td>
      A static build
    </td>
    
    <td>
      None at runtime
    </td>
    
    <td>
      Use <code>
        <StorageImage>
      </code>
      
       or the original URL
    </td>
  </tr>
</tbody>
</table>

## Serve a private file for a while

A file URL from Convex works for anyone who holds it, until the file is deleted. For a download that must stop working, such as a paid export or a photo shared in a support thread, hand out an expiring URL:

```ts [backend/strips.ts]
import { backendError } from 'nuxt-backend/functions'
import { HOUR } from 'nuxt-backend/rate-limit'
import { v } from 'convex/values'

export const downloadUrl = workspace.query({
  args: { id: v.id('strips') },
  handler: async (ctx, { id }) => {
    const strip = await ctx.db.get('strips', id)
    if (strip?.workspaceId !== ctx.workspace.id) throw backendError('forbidden', { reason: 'other_workspace' })
    return strip.exportId ? await storage.signedUrl(ctx, strip.exportId, { ttl: HOUR }) : null
  },
})
```

`signedUrl(ctx, storageId, { ttl })` returns a URL on your deployment's HTTP routes, signed with the app's secret (`AUTH_SECRET`), that serves the file until `ttl` milliseconds have passed. The route is `GET /storage/<id>`; pass `storage` to `registerBackendRoutes` once, in `backend/http.ts`, and it is mounted:

```ts [backend/http.ts]
import { storage } from './storage'

registerBackendRoutes(http, { auth: { authComponent, createAuth }, billing, email, ai, storage })
```

After that, and once the file is released, it answers 404. It sends `X-Content-Type-Options: nosniff` and a sandboxing `Content-Security-Policy`, and serves anything that is not a raster image as a download. `ttl` defaults to an hour and is capped at seven days.

A live query that signs a URL keeps it fresh on its own. The `backend` component's URL clock ticks every half hour, and every query that called `signedUrl` with a `ttl` under a day runs again. The URL counts from the last tick, not from the moment the query ran, and changes only once a step of half its `ttl`: the same URL between steps, so the browser keeps the image it already has. It works for `ttl` at least and half as long again at most. A page left open, such as a shared row's public page, never holds an expired URL as long as `ttl` is an hour or more, with no clock argument or timer of yours. A query read once on the server, with no live subscription, signs once.

Each step is one new URL for every signed image on every open page, and one more request to the file route for each. With the default hour that is a step every half hour. An image-heavy page held open for long gets fewer with a longer `ttl`: six hours steps every three. A `ttl` of a day or more reads no clock: it counts from the current hour and never changes under an open page, so keep it for a page held for less than its life, such as a share page's preview card.

Use signed URLs with `<NuxtImg>` only for short-lived pages: the optimizer caches each resized copy by its source URL, so a copy can outlive the signature.

## What happens when…

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

<tbody>
  <tr>
    <td>
      The caller is not the owner and not in the file's workspace
    </td>
    
    <td>
      <code>
        getFileUrl
      </code>
      
       returns <code>
        null
      </code>
      
      , and <code>
        <StorageImage>
      </code>
      
       shows <code>
        missing
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      The file was released, erased, or failed its type check
    </td>
    
    <td>
      <code>
        null
      </code>
      
      , and a signed URL answers 404
    </td>
  </tr>
  
  <tr>
    <td>
      A signed URL expired or was altered
    </td>
    
    <td>
      404
    </td>
  </tr>
</tbody>
</table>

## Limits

- **No transforms in storage.** Convex serves the original bytes. Resizing and format changes come from your host's optimizer.
- **Cached copies.** The optimizer caches each resized copy by its URL and size, so a copy can outlive a file you released or erased until that cache expires. Show images that must vanish at once with `<StorageImage>`.
- **Plain file URLs never expire.** Return them only to people who may see the file anyway; the query's check is the one that holds.

## Test it

Open a page with `<NuxtImg provider="backend" width="320">` and look at the image's request in the browser's network panel: it goes to the optimizer with your width, and returns a smaller file than the original. In a backend test, call `downloadUrl` as a member of another workspace and expect `forbidden` ([Test backend functions](https://nuxt-backend.dev/docs/develop/testing/functions)).

## Related

- [Accept checked uploads](https://nuxt-backend.dev/docs/build/data/files/checked-uploads): `getFileUrl`, and the files it answers for.
- [Share by link](https://nuxt-backend.dev/docs/build/data/files/share-links): public pages that show a shared row's images.
- [Convex](https://nuxt-backend.dev/docs/services/convex): file storage and its bandwidth costs.
- [`@nuxt/image` providers](https://image.nuxt.com/advanced/custom-provider): how a provider maps a source to a URL.


## Sitemap

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