Skip to content

Commit

Permalink
perf(html): apply preTransformRequest for html scripts (#12599)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Mar 26, 2023
1 parent 276725f commit 420782c
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions packages/vite/src/node/server/middlewares/indexHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ import {
joinUrlSegments,
normalizePath,
processSrcSetSync,
stripBase,
unwrapId,
wrapId,
} from '../../utils'
import type { ModuleGraph } from '../moduleGraph'

interface AssetNode {
start: number
Expand Down Expand Up @@ -87,12 +88,12 @@ const processNodeUrl = (
config: ResolvedConfig,
htmlPath: string,
originalUrl?: string,
moduleGraph?: ModuleGraph,
server?: ViteDevServer,
) => {
let url = attr.value || ''

if (moduleGraph) {
const mod = moduleGraph.urlToModuleMap.get(url)
if (server?.moduleGraph) {
const mod = server.moduleGraph.urlToModuleMap.get(url)
if (mod && mod.lastHMRTimestamp > 0) {
url = injectQuery(url, `t=${mod.lastHMRTimestamp}`)
}
Expand All @@ -102,14 +103,19 @@ const processNodeUrl = (
// prefix with base (dev only, base is never relative)
const fullUrl = path.posix.join(devBase, url)
overwriteAttrValue(s, sourceCodeLocation, fullUrl)
if (server) preTransformRequest(server, fullUrl, devBase)
} else if (
url[0] === '.' &&
originalUrl &&
originalUrl !== '/' &&
htmlPath === '/index.html'
) {
// prefix with base (dev only, base is never relative)
const replacer = (url: string) => path.posix.join(devBase, url)
const replacer = (url: string) => {
const fullUrl = path.posix.join(devBase, url)
if (server) preTransformRequest(server, fullUrl, devBase)
return fullUrl
}

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

await traverseHtml(html, filename, (node) => {
Expand All @@ -213,7 +220,7 @@ const devHtmlHook: IndexHtmlTransformHook = async (
config,
htmlPath,
originalUrl,
moduleGraph,
server,
)
} else if (isModule && node.childNodes.length) {
addInlineModule(node, 'js')
Expand Down Expand Up @@ -306,3 +313,15 @@ export function indexHtmlMiddleware(
next()
}
}

function preTransformRequest(server: ViteDevServer, url: string, base: string) {
if (!server.config.server.preTransformRequests) return

url = unwrapId(stripBase(url, base))

// transform all url as non-ssr as html includes client-side assets only
server.transformRequest(url).catch((e) => {
// Unexpected error, log the issue but avoid an unhandled exception
server.config.logger.error(e)
})
}

0 comments on commit 420782c

Please sign in to comment.