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

feat: expose pendingReload to consumers #4101

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/vite/src/node/optimizer/registerMissing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function createMissingImporterRegisterFn(
} finally {
server._isRunningOptimizer = false
pendingResolve && pendingResolve()
server._pendingReload = pendingResolve = null
server.pendingReload = pendingResolve = null
}

// Cached transform results have stale imports (resolved to
Expand All @@ -94,7 +94,7 @@ export function createMissingImporterRegisterFn(
currentMissing[id] = resolved
if (handle) clearTimeout(handle)
handle = setTimeout(() => rerun(ssr), debounceMs)
server._pendingReload = new Promise((r) => {
server.pendingReload = new Promise((r) => {
pendingResolve = r
})
}
Expand Down
15 changes: 9 additions & 6 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ export interface ViteDevServer {
* and hmr state.
*/
moduleGraph: ModuleGraph
/**
* A promise which will resolve once a reload is complete. A reload is
* pending as a result of optimizing missing dependencies.
*
* When no reload is pending it will be null.
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to note here that the pendingReload field might be refreshed. That is, if the user visits a new route, and new dependencies are discovered, it may be re-assigned with a new Promise.

*/
pendingReload: Promise<void> | null
/**
* Programmatically resolve, load and transform a URL and get the result
* without going through the http request pipeline.
Expand Down Expand Up @@ -293,10 +300,6 @@ export interface ViteDevServer {
_registerMissingImport:
| ((id: string, resolved: string, ssr: boolean | undefined) => void)
| null
/**
* @internal
*/
_pendingReload: Promise<void> | null
}

export async function createServer(
Expand Down Expand Up @@ -348,6 +351,7 @@ export async function createServer(
pluginContainer: container,
ws,
moduleGraph,
pendingReload: null,
transformWithEsbuild,
transformRequest(url, options) {
return transformRequest(url, server, options)
Expand Down Expand Up @@ -390,8 +394,7 @@ export async function createServer(
_ssrExternals: null,
_globImporters: {},
_isRunningOptimizer: false,
_registerMissingImport: null,
_pendingReload: null
_registerMissingImport: null
}

server.transformIndexHtml = createDevHtmlTransformFn(server)
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/server/middlewares/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ export function transformMiddleware(
}

if (
server._pendingReload &&
server.pendingReload &&
// always allow vite client requests so that it can trigger page reload
!req.url?.startsWith(CLIENT_PUBLIC_PATH) &&
!req.url?.includes('vite/dist/client')
) {
// missing dep pending reload, hold request until reload happens
server._pendingReload.then(() =>
server.pendingReload.then(() =>
// If the refresh has not happened after timeout, Vite considers
// something unexpected has happened. In this case, Vite
// returns an empty response that will error.
Expand Down