Skip to content

Commit 420782c

Browse files
authoredMar 26, 2023
perf(html): apply preTransformRequest for html scripts (#12599)
1 parent 276725f commit 420782c

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed
 

‎packages/vite/src/node/server/middlewares/indexHtml.ts

+25-6
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ import {
3131
joinUrlSegments,
3232
normalizePath,
3333
processSrcSetSync,
34+
stripBase,
35+
unwrapId,
3436
wrapId,
3537
} from '../../utils'
36-
import type { ModuleGraph } from '../moduleGraph'
3738

3839
interface AssetNode {
3940
start: number
@@ -87,12 +88,12 @@ const processNodeUrl = (
8788
config: ResolvedConfig,
8889
htmlPath: string,
8990
originalUrl?: string,
90-
moduleGraph?: ModuleGraph,
91+
server?: ViteDevServer,
9192
) => {
9293
let url = attr.value || ''
9394

94-
if (moduleGraph) {
95-
const mod = moduleGraph.urlToModuleMap.get(url)
95+
if (server?.moduleGraph) {
96+
const mod = server.moduleGraph.urlToModuleMap.get(url)
9697
if (mod && mod.lastHMRTimestamp > 0) {
9798
url = injectQuery(url, `t=${mod.lastHMRTimestamp}`)
9899
}
@@ -102,14 +103,19 @@ const processNodeUrl = (
102103
// prefix with base (dev only, base is never relative)
103104
const fullUrl = path.posix.join(devBase, url)
104105
overwriteAttrValue(s, sourceCodeLocation, fullUrl)
106+
if (server) preTransformRequest(server, fullUrl, devBase)
105107
} else if (
106108
url[0] === '.' &&
107109
originalUrl &&
108110
originalUrl !== '/' &&
109111
htmlPath === '/index.html'
110112
) {
111113
// prefix with base (dev only, base is never relative)
112-
const replacer = (url: string) => path.posix.join(devBase, url)
114+
const replacer = (url: string) => {
115+
const fullUrl = path.posix.join(devBase, url)
116+
if (server) preTransformRequest(server, fullUrl, devBase)
117+
return fullUrl
118+
}
113119

114120
// #3230 if some request url (localhost:3000/a/b) return to fallback html, the relative assets
115121
// path will add `/a/` prefix, it will caused 404.
@@ -194,6 +200,7 @@ const devHtmlHook: IndexHtmlTransformHook = async (
194200
node.sourceCodeLocation!.endOffset,
195201
`<script type="module" src="${modulePath}"></script>`,
196202
)
203+
preTransformRequest(server!, modulePath, base)
197204
}
198205

199206
await traverseHtml(html, filename, (node) => {
@@ -213,7 +220,7 @@ const devHtmlHook: IndexHtmlTransformHook = async (
213220
config,
214221
htmlPath,
215222
originalUrl,
216-
moduleGraph,
223+
server,
217224
)
218225
} else if (isModule && node.childNodes.length) {
219226
addInlineModule(node, 'js')
@@ -306,3 +313,15 @@ export function indexHtmlMiddleware(
306313
next()
307314
}
308315
}
316+
317+
function preTransformRequest(server: ViteDevServer, url: string, base: string) {
318+
if (!server.config.server.preTransformRequests) return
319+
320+
url = unwrapId(stripBase(url, base))
321+
322+
// transform all url as non-ssr as html includes client-side assets only
323+
server.transformRequest(url).catch((e) => {
324+
// Unexpected error, log the issue but avoid an unhandled exception
325+
server.config.logger.error(e)
326+
})
327+
}

0 commit comments

Comments
 (0)
Please sign in to comment.