{"record":{"id":"8c99a0b188c1cf5c","repo":"docmirror/dev-sidecar","slug":"devsidecar-script-filename-not-found","errorCode":null,"errorMessage":"DevSidecar: script '${filename}' not found","messagePattern":"DevSidecar: script '(.+?)' not found","errorType":"http","errorClass":null,"httpStatus":404,"severity":"info","filePath":"packages/mitmproxy/src/lib/proxy/middleware/InsertScriptMiddleware.js","lineNumber":157,"sourceCode":"  requestIntercept (context, req, res, ssl, next) {\n    const { rOptions, log, setting } = context\n    if (rOptions.path.indexOf(contextPath) !== 0) {\n      return\n    }\n    const urlPath = rOptions.path\n    let filename = urlPath.replace(contextPath, '')\n\n    // 重命名过，向下兼容\n    if (filename === 'global') {\n      filename = 'tampermonkey'\n    }\n\n    const script = monkey.get(setting.script.defaultDir)[filename]\n    // log.info(`urlPath: ${urlPath}, fileName: ${filename}, script: ${script}`)\n\n    log.info('ds_script, filename:', filename, ', `script != null` =', script != null)\n    if (script == null) {\n      res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' })\n      res.end(`DevSidecar: script '${filename}' not found`)\n      return true\n    }\n    const now = new Date()\n    res.writeHead(200, {\n      'DS-Middleware': 'ds_script',\n      'Content-Type': 'application/javascript; charset=utf-8',\n      'Cache-Control': 'public, max-age=86401, immutable', // 缓存1天\n      'Last-Modified': now.toUTCString(),\n      'Expires': new Date(now.getTime() + 86400000).toUTCString(), // 缓存1天\n      'Date': now.toUTCString(),\n    })\n    res.write(script.script)\n    res.end()\n    return true\n  },\n  responseInterceptor (req, res, proxyReq, proxyRes, ssl, next, append) {\n    if (append == null || (!append.head && !append.body)) {","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/docmirror/dev-sidecar/blob/7710cd56cce760c708f30b01d2d4056eb8c402d5/packages/mitmproxy/src/lib/proxy/middleware/InsertScriptMiddleware.js#L139-L175","documentation":"InsertScriptMiddleware serves locally installed userscripts over the proxy at a ds_script URL path. When the requested filename is not present in the script directory (monkey.get(setting.script.defaultDir)), the middleware responds 404 with this plain-text body instead of throwing. It is an intentional HTTP-level 'resource not found'.","triggerScenarios":"A page or injected <script src> references a userscript filename served through the proxy that does not exist in the script default directory — e.g. the script was deleted/renamed on disk while a cached page still references it, or the URL filename is misspelled/encoded oddly.","commonSituations":"Stale HTML referencing removed scripts; scripts directory (~/.dev-sidecar/scripts or configured defaultDir) reset or moved; case-sensitivity mismatch on Linux (file 'Foo.js' requested as 'foo.js').","solutions":["Check setting.script.defaultDir (default under ~/.dev-sidecar) and confirm the requested filename exists there","Re-create or re-download the missing userscript into the scripts directory","Clear browser/app cache so the stale reference to the deleted script is re-fetched","On Linux/macOS verify exact filename casing matches the requested name"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"const fs = require('fs'); const path = require('path')\nfunction scriptExists(defaultDir, filename) {\n  return Boolean(filename) && fs.existsSync(path.join(defaultDir, filename))\n}","typeGuard":"function isScriptNotFoundResponse(res) {\n  return res.statusCode === 404 && typeof res.body === 'string' && res.body.startsWith(\"DevSidecar: script '\")\n}","tryCatchPattern":"const res = await fetch(scriptUrl, { agent: proxyAgent })\nif (res.status === 404 && (await res.text()).includes('not found')) {\n  console.warn('Userscript missing on proxy host; reinstalling...')\n  await reinstallUserscript(filename)\n}","preventionTips":["Never delete userscripts that injected pages may still reference without reloading the app","Keep scripts in setting.script.defaultDir with exact, stable filenames (mind case on Linux)","Hard-reload pages after renaming scripts to flush stale <script src> references","Back up the scripts directory when upgrading dev-sidecar"],"tags":["http-404","userscript","middleware"],"backgroundTag":"script-not-found","analyzedSha":"7710cd56cce760c708f30b01d2d4056eb8c402d5","analyzedAt":"2026-08-31T22:07:07.234Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}