{"record":{"id":"a0a79e60c806d938","repo":"vitejs/vite","slug":"module-url-was-mistakenly-invalidated-during","errorCode":null,"errorMessage":"Module \"${url}\" was mistakenly invalidated during fetch phase.","messagePattern":"Module \"(.+?)\" was mistakenly invalidated during fetch phase\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vite/src/module-runner/runner.ts","lineNumber":298,"sourceCode":"    const isCached = !!(typeof cachedModule === 'object' && cachedModule.meta)\n\n    const fetchedModule = // fast return for established externalized pattern\n      (\n        url.startsWith('data:') || this.isBuiltin?.(url)\n          ? { externalize: url, type: 'builtin' }\n          : await this.transport.invoke('fetchModule', [\n              url,\n              importer,\n              {\n                cached: isCached,\n                startOffset: this.evaluator.startOffset,\n              },\n            ])\n      ) as ResolvedResult\n\n    if ('cache' in fetchedModule) {\n      if (!cachedModule || !cachedModule.meta) {\n        throw new Error(\n          `Module \"${url}\" was mistakenly invalidated during fetch phase.`,\n        )\n      }\n      return cachedModule\n    }\n\n    const moduleId =\n      'externalize' in fetchedModule\n        ? fetchedModule.externalize\n        : fetchedModule.id\n    const moduleUrl = 'url' in fetchedModule ? fetchedModule.url : url\n    const module = this.evaluatedModules.ensureModule(moduleId, moduleUrl)\n\n    if ('invalidate' in fetchedModule && fetchedModule.invalidate) {\n      this.evaluatedModules.invalidateModule(module)\n    }\n\n    fetchedModule.url = moduleUrl","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/vitejs/vite/blob/b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab/packages/vite/src/module-runner/runner.ts#L280-L316","documentation":"When the transport returns a result with a `cache` flag (telling the runner to reuse its cached module), the runner validates that a cached module actually exists (`cachedModule?.meta`). If the server says 'use cache' but the runner has no cached entry, that indicates a state inconsistency - the server invalidated/cleared something it shouldn't have during the fetch phase - so the runner throws rather than return stale or missing data.","triggerScenarios":"The module-runner server/transport responds with `{ cache: true }` for a module the runner never fetched (or whose `meta` was cleared). Usually a transport bug or incorrect invalidation during the fetch phase of the same request.","commonSituations":"Custom transports that eagerly return `cache` without tracking runner state. Race conditions where modules are invalidated mid-fetch. Bugs in dev server invalidation logic interacting with the runner.","solutions":["If you implemented a custom transport, only return `{ cache: true }` when the runner previously reported having the module (track via the `cached` option passed to `fetchModule`).","Restart the dev server / runner to reset inconsistent module state.","Update vite to the latest patch - this is often triggered by a fixed invalidation bug."],"exampleFix":"// before (custom transport)\ninvoke('fetchModule', [url, importer, opts]) {\n  return { cache: true } // claimed even on first fetch\n}\n// after\ninvoke('fetchModule', [url, importer, opts]) {\n  if (opts.cached && hasInStore(url)) return { cache: true }\n  return fetchFresh(url)\n}","handlingStrategy":"validation","validationCode":"// For custom transports: only return { cache: true } when the runner previously had the module\nfunction buildFetchResult(url: string, opts: { cached: boolean }, store: Map<string, unknown>) {\n  if (opts.cached && store.has(url)) return { cache: true }\n  const code = store.get(url) as string // or fetch fresh\n  return { code, url, id: url }\n}","typeGuard":null,"tryCatchPattern":"try {\n  return await runner.import(url)\n} catch (e) {\n  if (e instanceof Error && /mistakenly invalidated during fetch phase/.test(e.message)) {\n    // restart the runner / reset dev server module graph, then retry once\n  } else throw e\n}","preventionTips":["Custom transports must honor the `cached` flag passed to fetchModule and only return { cache: true } when warranted.","Keep vite updated - invalidation bugs are patched frequently."],"tags":["module-runner","transport","cache","invalidation","ssr"],"backgroundTag":null,"analyzedSha":"b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab","analyzedAt":"2026-08-11T11:49:19.515Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}