sveltejs / sveltejs/kit

Inconsistent image import behavior with adapter-vercel

Open
#14,088 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pkg:adapter-vercel
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the bug

When I import an unoptimized image in a SvelteKit page, say on the /image route, like this

import src from './mike-van-den-bos-fbQv3cx--bM-unsplash.jpg';

using adapter-vercel, I get inconsistent values for src:

  • When I go to /image directly (or do a hard reload), src resolves to a root-relative path, e.g. /_app/immutable/assets/mike-van-den-bos-fbQv3cx--bM-unsplash.a5Y7YFW3.jpg.

  • When I navigate from / to /image by clicking a link, src resolves to a fully qualified URL that includes the Vercel deployment URL, e.g. https://repro-sveltekit-vercel-image-optimization.vercel.app/_app/immutable/assets/mike-van-den-bos-fbQv3cx--bM-unsplash.a5Y7YFW3.jpg.

This behavior interferes with the Vercel Image Optimization API because a srcset URL like this (note the fully qualified URL in url)

/_vercel/image?url=https%3A%2F%2Frepro-sveltekit-vercel-image-optimization.vercel.app%2F_app%2Fimmutable%2Fassets%2Fmike-van-den-bos-fbQv3cx--bM-unsplash.a5Y7YFW3.jpg&w=1200&q=85

results in status 400 while a URL like this (note the root-relative URL in url)

/_vercel/image?url=%2F_app%2Fimmutable%2Fassets%2Fmike-van-den-bos-fbQv3cx--bM-unsplash.a5Y7YFW3.jpg&w=1200&q=85

results in a successful image generation.

In other words, the inconsistent src breaks the Vercel Image Optimization API whenever it is a fully qualified URL. The Vercel Image API documentation does not provide any details on why it would not work when an image using the fully qualified deployment URL is passed in. It might be to prevent recursive API invocations. But not sure.

But that aside, the question is whether src in above import not resolving consistenly is a bug. I also compared with the adapter-node behavior. For adapter-node src would always be a root-relative path.

Reproduction

Minimal repro repo: https://github.com/maiertech/repro-sveltekit-vercel-image-optimization.

The repro cannot be observed when running pnpm dev. You have to look at the Vercel production deployment.

  1. Go to https://repro-sveltekit-vercel-image-optimization.vercel.app/.
  2. Click "responsive image page".
  3. You should see a broken image.
  4. Check out the img tag in the DevTools. Note that src and the url params in srcset use the fully qaulified URL inlcuding the deployment URL origin.
  5. Now do a hard reload of the page.
  6. You should see the image.
  7. Note in the DevTools that the img tag now uses root-relative URLs (which are server-side rendered).
Logs

System Info
System:
    OS: macOS 15.6
    CPU: (8) arm64 Apple M3
    Memory: 609.17 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.18.0 - /opt/homebrew/opt/node@22/bin/node
    npm: 10.9.3 - /opt/homebrew/opt/node@22/bin/npm
    pnpm: 10.14.0 - /opt/homebrew/bin/pnpm
  Browsers:
    Chrome: 138.0.7204.184
    Safari: 18.6
  npmPackages:
    @sveltejs/adapter-vercel: ^5.8.1 => 5.8.1 
    @sveltejs/kit: ^2.22.0 => 2.27.0 
    @sveltejs/vite-plugin-svelte: ^6.0.0 => 6.1.0 
    svelte: ^5.0.0 => 5.37.2 
    vite: ^7.0.4 => 7.0.6
Severity

serious, but I can work around it

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked minimal repro and the /image route, comparing adapter-vercel behavior in a Vercel production deployment with adapter-node and with pnpm dev. Reproduce both hard reload and client navigation, then make imported image URLs consistent and verify that the Vercel image optimization request succeeds in both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.