diff --git a/.changeset/strip-toolbar-sourcemap.md b/.changeset/strip-toolbar-sourcemap.md new file mode 100644 index 000000000000..9f52d74ec677 --- /dev/null +++ b/.changeset/strip-toolbar-sourcemap.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a spurious 404 request for a dev toolbar sourcemap during `astro dev` caused by the browser mis-resolving a relative `sourceMappingURL` from the `/@id/` URL prefix diff --git a/packages/astro/src/toolbar/vite-plugin-dev-toolbar.ts b/packages/astro/src/toolbar/vite-plugin-dev-toolbar.ts index aaac1d5fcce7..2da7c98cd0df 100644 --- a/packages/astro/src/toolbar/vite-plugin-dev-toolbar.ts +++ b/packages/astro/src/toolbar/vite-plugin-dev-toolbar.ts @@ -1,3 +1,4 @@ +import { readFileSync, writeFileSync } from 'node:fs'; import type * as vite from 'vite'; import { telemetry } from '../events/index.js'; import { eventAppToggled } from '../events/toolbar.js'; @@ -21,6 +22,33 @@ export default function astroDevToolbar({ settings, logger }: AstroPluginOptions 'astro > axobject-query', ...(settings.devToolbarApps.length > 0 ? ['astro/toolbar'] : []), ], + esbuildOptions: { + plugins: [ + { + name: 'astro:strip-toolbar-sourcemap', + setup(build) { + // The dev toolbar entrypoint is served via /@id/ which causes + // the browser to mis-resolve the relative sourceMappingURL that + // esbuild adds, producing a bogus 404 request. Strip it after + // esbuild writes the optimized deps to disk. + build.onEnd((result) => { + if (!result.metafile) return; + for (const outputPath of Object.keys(result.metafile.outputs)) { + if (!outputPath.includes('entrypoint') || !outputPath.endsWith('.js')) continue; + const code = readFileSync(outputPath, 'utf-8'); + const stripped = code.replace( + /\/\/# sourceMappingURL=.*$/m, + '', + ); + if (stripped !== code) { + writeFileSync(outputPath, stripped); + } + } + }); + }, + }, + ], + }, }, }; },