Skip to content

Commit

Permalink
perf: skip resolving internal requests for static content, only reset…
Browse files Browse the repository at this point in the history
… decoded url when url changed
  • Loading branch information
hannoeru committed Aug 26, 2021
1 parent 4ba6301 commit 501bd03
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
22 changes: 13 additions & 9 deletions packages/vite/src/node/server/middlewares/static.ts
Expand Up @@ -8,6 +8,7 @@ import {
ensureLeadingSlash,
fsPathFromId,
isImportRequest,
isInternalRequest,
isWindows,
slash
} from '../../utils'
Expand All @@ -34,12 +35,10 @@ export function servePublicMiddleware(dir: string): Connect.NextHandleFunction {

// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
return function viteServePublicMiddleware(req, res, next) {
// skip import request
if (isImportRequest(req.url!)) {
// skip import request and internal requests `/@fs/ /@vite-client` etc...
if (isImportRequest(req.url!) || isInternalRequest(req.url!)) {
return next()
}
// reset sirv decoded url
delete req._decoded
serve(req, res, next)
}
}
Expand All @@ -52,15 +51,19 @@ export function serveStaticMiddleware(

// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
return function viteServeStaticMiddleware(req, res, next) {
const url = decodeURI(req.url!)

// only serve the file if it's not an html request
// so that html requests can fallthrough to our html middleware for
// special processing
if (path.extname(cleanUrl(url)) === '.html') {
// also skip internal requests `/@fs/ /@vite-client` etc...
if (
path.extname(cleanUrl(req.url!)) === '.html' ||
isInternalRequest(req.url!)
) {
return next()
}

const url = decodeURI(req.url!)

// apply aliases to static requests as well
let redirected: string | undefined
for (const { find, replacement } of config.resolve.alias) {
Expand All @@ -77,9 +80,10 @@ export function serveStaticMiddleware(
redirected = redirected.slice(dir.length)
}
req.url = redirected
// reset sirv decoded url
delete req._decoded
}
// reset sirv decoded url
delete req._decoded

serve(req, res, next)
}
}
Expand Down
17 changes: 16 additions & 1 deletion packages/vite/src/node/utils.ts
Expand Up @@ -4,7 +4,13 @@ import fs from 'fs'
import os from 'os'
import path from 'path'
import { pathToFileURL, URL } from 'url'
import { FS_PREFIX, DEFAULT_EXTENSIONS, VALID_ID_PREFIX } from './constants'
import {
FS_PREFIX,
DEFAULT_EXTENSIONS,
VALID_ID_PREFIX,
CLIENT_PUBLIC_PATH,
ENV_PUBLIC_PATH
} from './constants'
import resolve from 'resolve'
import builtins from 'builtin-modules'
import { FSWatcher } from 'chokidar'
Expand Down Expand Up @@ -122,8 +128,17 @@ export const isJSRequest = (url: string): boolean => {
}

const importQueryRE = /(\?|&)import=?(?:&|$)/
const internalPrefixes = [
FS_PREFIX,
VALID_ID_PREFIX,
CLIENT_PUBLIC_PATH,
ENV_PUBLIC_PATH
]
const InternalPrefixRE = new RegExp(`^(?:${internalPrefixes.join('|')})`)
const trailingSeparatorRE = /[\?&]$/
export const isImportRequest = (url: string): boolean => importQueryRE.test(url)
export const isInternalRequest = (url: string): boolean =>
InternalPrefixRE.test(url)

export function removeImportQuery(url: string): string {
return url.replace(importQueryRE, '$1').replace(trailingSeparatorRE, '')
Expand Down

0 comments on commit 501bd03

Please sign in to comment.