Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,30 @@ export default defineNuxtModule<ModuleOptions>({
})

// Resolve entry path for dev.
// Vite serves files relative to the project root, so we need to
// convert the absolute appDir to a path relative to rootDir.
// Append buildId as cache buster so browser refetches after restart.
//
// The Nuxt app entry lives in node_modules (nuxt/dist/app), which the
// Vite dev server does NOT serve from a root-relative URL. Vite addresses
// such files via its `@fs/<absolute-path>` mechanism, so we must build the
// entry URL from the entry's absolute filesystem path, not a synthesized
// root-relative path. (A prior implementation stripped appDir down to a
// `node_modules/...` path relative to rootDir; that 404s whenever appDir
// sits under rootDir — i.e. every normal single-project frontend.)
//
// The entry filename also differs from production: in dev Vite always uses
// the async entry (`@nuxt/vite-builder` forces `useAsyncEntry` whenever
// `nuxt.options.dev` is set), so the file is `entry.async.js`, not
// `entry.js`.
//
// Append buildId as cache buster so the browser refetches after a restart.
if (nuxt.options.dev) {
nuxt.hook('ready', () => {
const appDir = nuxt.options.appDir.replace(/\/+$/, '')
const rootDir = nuxt.options.rootDir.replace(/\/+$/, '')
const relativeAppDir = appDir.startsWith(rootDir)
? appDir.slice(rootDir.length + 1)
: appDir
const baseURL = (nuxt.options.app.baseURL || '/').replace(/\/+$/, '')
const assetsDir = (nuxt.options.app.buildAssetsDir || '/_nuxt/').replace(/^\/+|\/+$/g, '')
Comment on lines +146 to +147
const useAsyncEntry = nuxt.options.experimental?.asyncEntry || nuxt.options.dev
const entryName = useAsyncEntry ? 'entry.async' : 'entry'
Comment on lines +148 to +149
const buildId = nuxt.options.appConfig?.nuxt?.buildId
resolvedEntryPath = `/_nuxt/${relativeAppDir}/entry.js` + (buildId ? `?v=${buildId}` : '')
resolvedEntryPath = `${baseURL}/${assetsDir}/@fs${appDir}/${entryName}.js` + (buildId ? `?v=${buildId}` : '')
Comment on lines 145 to +151
})
}

Expand Down