{"record":{"id":"442b9f72acee55a7","repo":"withastro/astro","slug":"script-src-src-references-an-asset-in-the","errorCode":null,"errorMessage":"\n\n<script src=\"${src}\"> references an asset in the \"${publicDir}\" directory. Please add the \"is:inline\" directive to keep this asset from being bundled.\n\nFile: ${id}","messagePattern":"\n\n<script src=\"(.+?)\"> references an asset in the \"(.+?)\" directory\\. Please add the \"is:inline\" directive to keep this asset from being bundled\\.\n\nFile: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/astro/src/vite-plugin-astro/index.ts","lineNumber":213,"sourceCode":"\t\t\t\t\t\t\t// SSR script only exists to make them appear in the module graph.\n\t\t\t\t\t\t\tif (isAstroServerEnvironment(this.environment)) {\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tcode: `/* client script, empty in SSR: ${id} */`,\n\t\t\t\t\t\t\t\t\tmoduleType: 'ts',\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst script = compileMetadata.scripts[query.index];\n\t\t\t\t\t\t\tif (!script) {\n\t\t\t\t\t\t\t\tthrow new Error(`No script at index ${query.index}`);\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif (script.type === 'external') {\n\t\t\t\t\t\t\t\tconst src = script.src!;\n\t\t\t\t\t\t\t\tif (src.startsWith('/') && !isBrowserPath(src)) {\n\t\t\t\t\t\t\t\t\tconst publicDir =\n\t\t\t\t\t\t\t\t\t\tconfig.publicDir.pathname.replace(/\\/$/, '').split('/').pop() + '/';\n\t\t\t\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t\t\t\t`\\n\\n<script src=\"${src}\"> references an asset in the \"${publicDir}\" directory. Please add the \"is:inline\" directive to keep this asset from being bundled.\\n\\nFile: ${id}`,\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst result: vite.Rolldown.SourceDescription = {\n\t\t\t\t\t\t\t\tcode: '',\n\t\t\t\t\t\t\t\tmoduleType: 'ts',\n\t\t\t\t\t\t\t\tmeta: {\n\t\t\t\t\t\t\t\t\tvite: {\n\t\t\t\t\t\t\t\t\t\tlang: 'ts',\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\n\t\t\t\t\t\t\tswitch (script.type) {\n\t\t\t\t\t\t\t\tcase 'inline': {\n\t\t\t\t\t\t\t\t\tresult.code = script.code ?? '';","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/withastro/astro/blob/52e6c34790cc8ac4e69e6135ace06049867e5c4a/packages/astro/src/vite-plugin-astro/index.ts#L195-L231","documentation":"Astro bundles processed <script> tags (those without is:inline). If such a script's src is an absolute path (/...) that does not point inside the source root (src/ by default), Astro concludes it references a file in the public directory. public/ assets are copied verbatim and are outside the module graph, so Vite cannot bundle them — the build stops and asks you to keep the script as-is with the is:inline directive.","triggerScenarios":"<script src=\"/js/analytics.js\"></script> where the file lives at public/js/analytics.js; moving a bundled script into public/ without adding is:inline; root-relative paths to vendor files dropped into public/.","commonSituations":"Pasting <script src=\"...\"> snippets from vendor docs into an .astro layout; migrating a static site whose scripts all lived in the public folder.","solutions":["Add the is:inline directive: <script is:inline src=\"/js/analytics.js\"></script>","Or move the file out of public/ into the source tree (e.g. src/scripts/analytics.js) and reference it relatively so Vite bundles it","Keep third-party CDN scripts (full https:// URLs) as-is; only root-relative /... paths that resolve into public/ hit this error"],"exampleFix":"<!-- before -->\n<script src=\"/js/analytics.js\"></script>\n\n<!-- after -->\n<script is:inline src=\"/js/analytics.js\"></script>","handlingStrategy":"validation","validationCode":"// check whether a root-relative src points into public/ before writing the tag\nimport fs from 'node:fs';\nimport path from 'node:path';\nconst isPublicAsset = (src: string) =>\n\tsrc.startsWith('/') && fs.existsSync(path.join('public', src));\n// isPublicAsset('/js/analytics.js') === true -> needs is:inline","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always add is:inline to scripts whose src points at files in public/","Keep bundled scripts inside the source tree (src/) and reference them relatively","Reserve public/ for assets copied verbatim — anything Vite should bundle belongs in src/"],"tags":["scripts","public-dir","bundling","vite","build"],"backgroundTag":"public-dir-asset-reference","analyzedSha":"52e6c34790cc8ac4e69e6135ace06049867e5c4a","analyzedAt":"2026-08-18T18:48:03.901Z","contentChangedAt":"2026-08-18T18:48:03.901Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}