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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid outdated module to crash in importAnalysis after restart #13231

Merged
merged 8 commits into from May 17, 2023
Merged
6 changes: 3 additions & 3 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -254,9 +254,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// since we are already in the transform phase of the importer, it must
// have been loaded so its entry is guaranteed in the module graph.
const importerModule = moduleGraph.getModuleById(importer)!
if (!importerModule && depsOptimizer?.isOptimizedDepFile(importer)) {
// Ids of optimized deps could be invalidated and removed from the graph
// Return without transforming, this request is no longer valid, a full reload
if (!importerModule) {
// When the server is restarted, the module graph is cleared, so we
// return without transforming. This request is no longer valid, a full reload
// is going to request this id again. Throwing an outdated error so we
// properly finish the request with a 504 sent to the browser.
throwOutdatedRequest(importer)
Comment on lines -257 to 262
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be the fix for #13030, the rest of the PR are extra guards I think we should also apply.

Expand Down
12 changes: 10 additions & 2 deletions packages/vite/src/node/server/middlewares/indexHtml.ts
Expand Up @@ -28,13 +28,15 @@ import {
ensureWatchedFile,
fsPathFromId,
injectQuery,
isJSRequest,
joinUrlSegments,
normalizePath,
processSrcSetSync,
stripBase,
unwrapId,
wrapId,
} from '../../utils'
import { isCSSRequest } from '../../plugins/css'
import { checkPublicFile } from '../../plugins/asset'
import { getCodeWithSourcemap, injectSourcesContent } from '../sourcemap'

Expand Down Expand Up @@ -82,6 +84,12 @@ function getHtmlFilename(url: string, server: ViteDevServer) {
}
}

function shouldPreTransform(url: string, config: ResolvedConfig) {
return (
!checkPublicFile(url, config) && (isJSRequest(url) || isCSSRequest(url))
)
}

const processNodeUrl = (
attr: Token.Attribute,
sourceCodeLocation: Token.Location,
Expand All @@ -104,7 +112,7 @@ const processNodeUrl = (
// prefix with base (dev only, base is never relative)
const fullUrl = path.posix.join(devBase, url)
overwriteAttrValue(s, sourceCodeLocation, fullUrl)
if (server && !checkPublicFile(url, config)) {
if (server && shouldPreTransform(url, config)) {
preTransformRequest(server, fullUrl, devBase)
}
} else if (
Expand All @@ -116,7 +124,7 @@ const processNodeUrl = (
// prefix with base (dev only, base is never relative)
const replacer = (url: string) => {
const fullUrl = path.posix.join(devBase, url)
if (server && !checkPublicFile(url, config)) {
if (server && shouldPreTransform(url, config)) {
preTransformRequest(server, fullUrl, devBase)
}
return fullUrl
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/transformRequest.ts
Expand Up @@ -73,7 +73,7 @@ export function transformRequest(
const pending = server._pendingRequests.get(cacheKey)
if (pending) {
return server.moduleGraph
.getModuleByUrl(removeTimestampQuery(url), options.ssr)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to the PR, the timestamp is already removed by getModuleByUrl. We can move it to a separate PR if it is too noisy here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't mind mixing it here as it's only few line changes, so I'm fine either ways 🙂

.getModuleByUrl(url, options.ssr)
bluwy marked this conversation as resolved.
Show resolved Hide resolved
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
.then((module) => {
if (!module || pending.timestamp > module.lastInvalidationTimestamp) {
// The pending request is still valid, we can safely reuse its result
Expand Down