Skip to content

Commit

Permalink
perf: pre transform requests while opening the browser (#12809)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Oct 16, 2023
1 parent da80372 commit 96a4ce3
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'node:path'
import type * as net from 'node:net'
import { get as httpGet } from 'node:http'
import type * as http from 'node:http'
import { performance } from 'node:perf_hooks'
import connect from 'connect'
Expand Down Expand Up @@ -447,6 +448,31 @@ export async function _createServer(
? new URL(options.open, url).href
: url

// We know the url that the browser would be opened to, so we can
// start the request while we are awaiting the browser. This will
// start the crawling of static imports ~500ms before.
setTimeout(() => {
httpGet(
path,
{
headers: {
// Allow the history middleware to redirect to /index.html
Accept: 'text/html',
},
},
(res) => {
res.on('end', () => {
// Ignore response, scripts discovered while processing the entry
// will be preprocessed (server.config.server.preTransformRequests)
})
},
)
.on('error', () => {
// Ignore errors
})
.end()
}, 0)

_openBrowser(path, true, server.config.logger)
} else {
server.config.logger.warn('No URL available to open in browser')
Expand Down

0 comments on commit 96a4ce3

Please sign in to comment.