Skip to content

dev: first request 503s with Vite environment "ssr" is unavailable on cold start #4295

@glekner

Description

@glekner

On a fresh vite dev, the very first page load occasionally fails with a 503 before the SSR environment is ready:

HTTPError: Vite environment "ssr" is unavailable
  at httpError (.../nitro/dist/runtime/internal/vite/dev-worker.mjs:208)
  at ViteEnvRunner.fetch (.../dev-worker.mjs:77)

Reloading the page always fixes it.

Cause

ViteEnvRunner.fetch waits for the SSR entry import to resolve, but only retries 5 times with exponential backoff:

for (let i = 0; i < 5 && !(this.entry || this.entryError); i++) {
  await new Promise((r) => setTimeout(r, 100 * Math.pow(2, i)));
}
if (!this.entry) {
  throw httpError(503, `Vite environment "${this.name}" is unavailable`);
}

That caps the wait at 100+200+400+800+1600 = ~3.1s. On a larger app the first cold import of the SSR entry takes longer than that, so the initial request gives up and 503s. A reload succeeds because the import has finished by then, which makes it look intermittent.

Reproduction

  • nitro 3.0.260522-beta, Vite 8, TanStack Start (preset vercel)
  • Start the dev server and hit / immediately, before the SSR module graph finishes transforming.

Possible fix

Poll on a short fixed interval with a higher ceiling instead of bailing after ~3s. It still exits the moment the entry resolves, so the common (warm) case stays fast, but a slow cold start no longer 503s:

for (let i = 0; i < 600 && !(this.entry || this.entryError); i++) {
  await new Promise((r) => setTimeout(r, 50));
}

I've been running this patch locally and it reliably removes the first-load 503. Happy to open a PR if the approach looks right.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions