Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(html): apply preTransformRequest for html scripts #12599

Merged
merged 1 commit into from Mar 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 25 additions & 6 deletions packages/vite/src/node/server/middlewares/indexHtml.ts
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)
})
}