File tree Expand file tree Collapse file tree 3 files changed +16
-9
lines changed
packages/vite/src/node/server/middlewares Expand file tree Collapse file tree 3 files changed +16
-9
lines changed Original file line number Diff line number Diff line change 1
1
import path from 'node:path'
2
2
import fs from 'node:fs'
3
3
import type { Connect } from 'dep-types/connect'
4
- import { createDebugger } from '../../utils'
4
+ import { createDebugger , joinUrlSegments } from '../../utils'
5
5
import { cleanUrl } from '../../../shared/utils'
6
6
import type { DevEnvironment } from '../environment'
7
7
import { FullBundleDevEnvironment } from '../environments/fullBundleEnvironment'
@@ -20,8 +20,9 @@ export function htmlFallbackMiddleware(
20
20
21
21
function checkFileExists ( relativePath : string ) {
22
22
return (
23
- memoryFiles ?. has ( relativePath ) ??
24
- fs . existsSync ( path . join ( root , relativePath ) )
23
+ memoryFiles ?. has (
24
+ relativePath . slice ( 1 ) , // remove first /
25
+ ) ?? fs . existsSync ( path . join ( root , relativePath ) )
25
26
)
26
27
}
27
28
@@ -57,7 +58,7 @@ export function htmlFallbackMiddleware(
57
58
}
58
59
// trailing slash should check for fallback index.html
59
60
else if ( pathname . endsWith ( '/' ) ) {
60
- if ( checkFileExists ( path . join ( pathname , 'index.html' ) ) ) {
61
+ if ( checkFileExists ( joinUrlSegments ( pathname , 'index.html' ) ) ) {
61
62
const newUrl = url + 'index.html'
62
63
debug ?.( `Rewriting ${ req . method } ${ req . url } to ${ newUrl } ` )
63
64
req . url = newUrl
Original file line number Diff line number Diff line change @@ -457,8 +457,10 @@ export function indexHtmlMiddleware(
457
457
// htmlFallbackMiddleware appends '.html' to URLs
458
458
if ( url ?. endsWith ( '.html' ) && req . headers [ 'sec-fetch-dest' ] !== 'script' ) {
459
459
if ( fullBundleEnv ) {
460
- const cleanedUrl = cleanUrl ( url ) . slice ( 1 ) // remove first /
461
- let content = fullBundleEnv . memoryFiles . get ( cleanedUrl )
460
+ const pathname = decodeURIComponent ( url )
461
+ const filePath = pathname . slice ( 1 ) // remove first /
462
+
463
+ let content = fullBundleEnv . memoryFiles . get ( filePath )
462
464
if ( ! content && fullBundleEnv . memoryFiles . size !== 0 ) {
463
465
return next ( )
464
466
}
Original file line number Diff line number Diff line change @@ -17,13 +17,17 @@ export function memoryFilesMiddleware(
17
17
const headers = server . config . server . headers
18
18
19
19
return function viteMemoryFilesMiddleware ( req , res , next ) {
20
- const cleanedUrl = cleanUrl ( req . url ! ) . slice ( 1 ) // remove first /
20
+ const cleanedUrl = cleanUrl ( req . url ! )
21
21
if ( cleanedUrl . endsWith ( '.html' ) ) {
22
22
return next ( )
23
23
}
24
- const file = memoryFiles . get ( cleanedUrl )
24
+
25
+ const pathname = decodeURIComponent ( cleanedUrl )
26
+ const filePath = pathname . slice ( 1 ) // remove first /
27
+
28
+ const file = memoryFiles . get ( filePath )
25
29
if ( file ) {
26
- const mime = mrmime . lookup ( cleanedUrl )
30
+ const mime = mrmime . lookup ( filePath )
27
31
if ( mime ) {
28
32
res . setHeader ( 'Content-Type' , mime )
29
33
}
You can’t perform that action at this time.
0 commit comments