Skip to content

Commit ac2d954

Browse files
authored
perf(dev): use exsolve to resolve + import legacy loading template (#936)
1 parent 3cacce8 commit ac2d954

File tree

3 files changed

+22
-28
lines changed

3 files changed

+22
-28
lines changed

packages/nuxi/src/commands/dev.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { isBun, isTest } from 'std-env'
2020
import { initialize } from '../dev'
2121
import { renderError } from '../dev/error'
2222
import { isSocketURL, parseSocketURL } from '../dev/socket'
23+
import { resolveLoadingTemplate } from '../dev/utils'
2324
import { showVersions } from '../utils/banner'
2425
import { overrideEnv } from '../utils/env'
2526
import { loadKit } from '../utils/kit'
@@ -128,7 +129,7 @@ const command = defineCommand({
128129
}
129130

130131
// Start proxy Listener
131-
const devProxy = await createDevProxy(nuxtOptions, listenOptions)
132+
const devProxy = await createDevProxy(cwd, nuxtOptions, listenOptions)
132133

133134
const useSocket = nuxtOptions._majorVersion === 4 || !!process.env.NUXT_SOCKET
134135

@@ -182,7 +183,7 @@ type ArgsT = Exclude<
182183

183184
type DevProxy = Awaited<ReturnType<typeof createDevProxy>>
184185

185-
async function createDevProxy(nuxtOptions: NuxtOptions, listenOptions: Partial<ListenOptions>) {
186+
async function createDevProxy(cwd: string, nuxtOptions: NuxtOptions, listenOptions: Partial<ListenOptions>) {
186187
let loadingMessage = 'Nuxt dev server is starting...'
187188
let error: Error | undefined
188189
let address: string | undefined
@@ -218,26 +219,16 @@ async function createDevProxy(nuxtOptions: NuxtOptions, listenOptions: Partial<L
218219
if (!address) {
219220
res.statusCode = 503
220221
res.setHeader('Content-Type', 'text/html')
222+
res.setHeader('Cache-Control', 'no-store')
221223
if (loadingTemplate) {
222224
res.end(loadingTemplate({ loading: loadingMessage }))
223225
return
224226
}
225-
// older versions of Nuxt did not have the loading template defined in the schema
226227

228+
// Nuxt <3.6 did not have the loading template defined in the schema
227229
async function resolveLoadingMessage() {
228-
const { createJiti } = await import('jiti')
229-
const jiti = createJiti(nuxtOptions.rootDir)
230-
for (const url of nuxtOptions.modulesDir) {
231-
const r = await jiti.import<{ loading: (opts?: { loading?: string }) => string }>('@nuxt/ui-templates', {
232-
parentURL: url,
233-
try: true,
234-
})
235-
if (r) {
236-
loadingTemplate = r.loading
237-
res.end(r.loading({ loading: loadingMessage }))
238-
break
239-
}
240-
}
230+
loadingTemplate = await resolveLoadingTemplate(cwd)
231+
res.end(loadingTemplate({ loading: loadingMessage }))
241232
}
242233
return resolveLoadingMessage()
243234
}

packages/nuxi/src/commands/info.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import process from 'node:process'
66

77
import { defineCommand } from 'citty'
88
import clipboardy from 'clipboardy'
9-
import { createJiti } from 'jiti'
109
import { detectPackageManager } from 'nypm'
1110
import { resolve } from 'pathe'
1211
import { readPackageJSON } from 'pkg-types'
@@ -168,6 +167,7 @@ function normalizeConfigModule(
168167

169168
async function getNuxtConfig(rootDir: string) {
170169
try {
170+
const { createJiti } = await import('jiti')
171171
const jiti = createJiti(rootDir, {
172172
interopDefault: true,
173173
// allow using `~` and `@` in `nuxt.config`

packages/nuxi/src/dev/utils.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ import type { AddressInfo } from 'node:net'
99
import EventEmitter from 'node:events'
1010
import { existsSync, watch } from 'node:fs'
1111
import { mkdir } from 'node:fs/promises'
12-
1312
import process from 'node:process'
13+
import { pathToFileURL } from 'node:url'
14+
1415
import defu from 'defu'
16+
import { resolveModulePath } from 'exsolve'
1517
import { toNodeListener } from 'h3'
1618
import { listen } from 'listhen'
1719
import { resolve } from 'pathe'
1820
import { debounce } from 'perfect-debounce'
1921
import { provider } from 'std-env'
20-
2122
import { joinURL } from 'ufo'
23+
2224
import { clearBuildDir } from '../utils/fs'
2325
import { loadKit } from '../utils/kit'
2426

@@ -162,20 +164,12 @@ export class NuxtDevServer extends EventEmitter<DevServerEventMap> {
162164
renderError(req, res, this._loadingError)
163165
}
164166

165-
async resolveLoadingTemplate() {
166-
const { createJiti } = await import('jiti')
167-
const jiti = createJiti(this.cwd)
168-
const loading = await jiti.import<{ loading: () => string }>('@nuxt/ui-templates').then(r => r.loading).catch(() => {})
169-
170-
return loading || ((params: { loading: string }) => `<h2>${params.loading}</h2>`)
171-
}
172-
173167
async _renderLoadingScreen(req: IncomingMessage, res: ServerResponse) {
174168
res.statusCode = 503
175169
res.setHeader('Content-Type', 'text/html')
176170
const loadingTemplate = this.options.loadingTemplate
177171
|| this._currentNuxt?.options.devServer.loadingTemplate
178-
|| await this.resolveLoadingTemplate()
172+
|| await resolveLoadingTemplate(this.cwd)
179173
res.end(
180174
loadingTemplate({
181175
loading: this._loadingMessage || 'Loading...',
@@ -434,3 +428,12 @@ function createConfigDirWatcher(cwd: string, onReload: (file: string) => void) {
434428

435429
return () => configDirWatcher.close()
436430
}
431+
432+
// Nuxt <3.6 did not have the loading template defined in the schema
433+
export async function resolveLoadingTemplate(cwd: string) {
434+
const nuxtPath = resolveModulePath('nuxt', { from: cwd, try: true })
435+
const uiTemplatesPath = resolveModulePath('@nuxt/ui-templates', { from: nuxtPath || cwd })
436+
const r: { loading: (opts?: { loading?: string }) => string } = await import(pathToFileURL(uiTemplatesPath).href)
437+
438+
return r.loading || ((params: { loading: string }) => `<h2>${params.loading}</h2>`)
439+
}

0 commit comments

Comments
 (0)