{"record":{"id":"1b617e425fbfee5b","repo":"microsoft/playwright","slug":"unable-to-restore-opfs-e-message","errorCode":null,"errorMessage":"Unable to restore OPFS: ${e.message}","messagePattern":"Unable to restore OPFS: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/injected/src/storageScript.ts","lineNumber":307,"sourceCode":"      for (const db of await this._global.indexedDB.databases?.() || []) {\n        // Do not wait for the callback - it is called on timer in Chromium (slow).\n        if (db.name)\n          this._global.indexedDB.deleteDatabase(db.name!);\n      }\n      await Promise.all((originState?.indexedDB ?? []).map(dbInfo => this._restoreDB(dbInfo)));\n    } catch (e) {\n      throw new Error('Unable to restore IndexedDB: ' + e.message);\n    }\n\n    this._global.sessionStorage.clear();\n    this._global.localStorage.clear();\n    for (const { name, value } of (originState?.localStorage || []))\n      this._global.localStorage.setItem(name, value);\n\n    try {\n      await this._restoreOPFS(originState);\n    } catch (e) {\n      throw new Error('Unable to restore OPFS: ' + e.message);\n    }\n  }\n}\n","sourceCodeStart":289,"sourceCodeEnd":311,"githubUrl":"https://github.com/microsoft/playwright/blob/312030cdcee20c006343e5f8420fe451b9aa6390/packages/injected/src/storageScript.ts#L289-L311","documentation":"When restoring a saved storageState into a new context, Playwright writes back cookies, localStorage, IndexedDB and OPFS files. If `_restoreOPFS` throws — recreating the captured OPFS files fails — `restore` rethrows it as 'Unable to restore OPFS: <reason>'. The suffix contains the underlying error so developers can diagnose why the OPFS replay failed.","triggerScenarios":"Calling an API that applies storageState (e.g. creating a context with `storageState`, or `context.storageState(...)` restore flows) where the saved state contains OPFS entries and writing those files back into `navigator.storage.getDirectory()` rejects — quota exceeded, insecure origin, unsupported browser build, or corrupted/malformed saved state.","commonSituations":"Reusing a storageState JSON captured in one browser on another browser/engine that lacks full OPFS support; quota exhaustion in CI containers with small tmpfs; manually edited or version-mismatched storageState files.","solutions":["Inspect the message suffix for the underlying OPFS write failure (quota, security, unsupported).","Re-capture the storageState with the same browser engine/version that will restore it.","Free storage quota or increase the context/browser storage quota in constrained CI environments.","Restore on a secure origin (https/localhost) where the File System Access API is available.","Strip the `opfs` entries from the storageState JSON if OPFS replay is not required."],"exampleFix":"// before\nconst context = await browser.newContext({ storageState: 'state.json' }); // throws restoring opfs\n// after\nconst state = JSON.parse(fs.readFileSync('state.json', 'utf8'));\ndelete state.opfs; // skip OPFS replay if not needed\nconst context = await browser.newContext({ storageState: state });","handlingStrategy":"fallback","validationCode":"const state = JSON.parse(fs.readFileSync(stateFile, 'utf8'));\nif (state.opfs?.length && !isSecureContextTarget) {\n  delete state.opfs; // target cannot restore OPFS\n}","typeGuard":"function isRestorableState(state: unknown): state is { origins?: unknown[]; cookies?: unknown[]; opfs?: unknown[] } {\n  return typeof state === 'object' && state !== null;\n}","tryCatchPattern":"try {\n  const context = await browser.newContext({ storageState: stateFile });\n} catch (e) {\n  if (String(e.message).includes('Unable to restore OPFS:')) {\n    console.error('OPFS restore failed:', String(e.message).replace('Unable to restore OPFS: ', ''));\n    const state = JSON.parse(fs.readFileSync(stateFile, 'utf8'));\n    delete state.opfs;\n    return browser.newContext({ storageState: state });\n  }\n  throw e;\n}","preventionTips":["Capture and restore storageState with the same browser engine and version.","Strip `opfs` entries when the target environment cannot replay them.","Ensure CI containers have enough storage quota before restoring OPFS state.","Only restore on secure-context pages."],"tags":["storage-state","opfs","restore"],"backgroundTag":"file-write-failed","analyzedSha":"312030cdcee20c006343e5f8420fe451b9aa6390","analyzedAt":"2026-09-07T14:42:43.271Z","contentChangedAt":"2026-09-07T14:42:43.271Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}