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: handle static assets in case-sensitive manner #10475

Merged
merged 5 commits into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
31 changes: 17 additions & 14 deletions packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { openBrowser } from './server/openBrowser'
import compression from './server/middlewares/compression'
import { proxyMiddleware } from './server/middlewares/proxy'
import { resolveHostname, resolveServerUrls } from './utils'
import { resolveHostname, resolveServerUrls, shouldServe } from './utils'
import { printServerUrls } from './logger'
import { resolveConfig } from '.'
import type { InlineConfig, ResolvedConfig } from '.'
Expand Down Expand Up @@ -112,21 +112,24 @@ export async function preview(
// static assets
const distDir = path.resolve(config.root, config.build.outDir)
const headers = config.preview.headers
app.use(
previewBase,
sirv(distDir, {
etag: true,
dev: true,
single: config.appType === 'spa',
setHeaders(res) {
if (headers) {
for (const name in headers) {
res.setHeader(name, headers[name]!)
}
const assetServer = sirv(distDir, {
etag: true,
dev: true,
single: config.appType === 'spa',
setHeaders(res) {
if (headers) {
for (const name in headers) {
res.setHeader(name, headers[name]!)
}
}
})
)
}
})
app.use(previewBase, async (req, res, next) => {
if (shouldServe(req.url!, distDir)) {
return assetServer(req, res, next)
}
next()
})

// apply post server hooks from plugins
postHooks.forEach((fn) => fn && fn())
Expand Down
6 changes: 5 additions & 1 deletion packages/vite/src/node/server/middlewares/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
isInternalRequest,
isParentDirectory,
isWindows,
shouldServe,
slash
} from '../../utils'

Expand Down Expand Up @@ -52,7 +53,10 @@ export function servePublicMiddleware(
if (isImportRequest(req.url!) || isInternalRequest(req.url!)) {
return next()
}
serve(req, res, next)
if (shouldServe(req.url!, dir)) {
return serve(req, res, next)
}
next()
}
}

Expand Down
39 changes: 39 additions & 0 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,45 @@ export const isNonDriveRelativeAbsolutePath = (p: string): boolean => {
return windowsDrivePathPrefixRE.test(p)
}

/**
* Determine if a file is being requested with the correct case, to ensure
* consistent behaviour between dev and prod and across operating systems.
*/
export function shouldServe(url: string, assetsDir: string): boolean {
// viteTestUrl is set to something like http://localhost:4173/ and then many tests make calls
// like `await page.goto(viteTestUrl + '/example')` giving us URLs beginning with a double slash
const pathname = decodeURIComponent(
new URL(url.startsWith('//') ? url.substring(1) : url, 'http://example.com')
.pathname
)
Copy link
Member

Choose a reason for hiding this comment

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

Looks like sirv is able to handle // because it uses @polka/url. We could probably use that too as it caches the result to req, but also something we can improve later.

const file = assetsDir + pathname
benmccann marked this conversation as resolved.
Show resolved Hide resolved
if (
!fs.existsSync(file) ||
(!fs.statSync(file).isDirectory() &&
isCaseInsensitiveFS && // can skip case check on Linux
benmccann marked this conversation as resolved.
Show resolved Hide resolved
!hasCorrectCase(file, assetsDir))
) {
return false
}
return true
}

/**
* Note that we can't use realpath here, because we don't want to follow
* symlinks.
*/
function hasCorrectCase(file: string, assets: string): boolean {
if (file === assets) return true

const parent = path.dirname(file)

if (fs.readdirSync(parent).includes(path.basename(file))) {
return hasCorrectCase(parent, assets)
}

return false
}

export function joinUrlSegments(a: string, b: string): string {
if (!a || !b) {
return a || b || ''
Expand Down
7 changes: 7 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fetch from 'node-fetch'
import { describe, expect, test } from 'vitest'
import {
browserLogs,
Expand All @@ -12,6 +13,7 @@ import {
readFile,
readManifest,
untilUpdated,
viteTestUrl,
watcher
} from '~utils'

Expand All @@ -27,6 +29,11 @@ test('should have no 404s', () => {
})
})

test('should get a 404 when using incorrect case', async () => {
benmccann marked this conversation as resolved.
Show resolved Hide resolved
expect((await fetch(viteTestUrl + 'icon.png')).status).toBe(200)
expect((await fetch(viteTestUrl + 'ICON.png')).status).toBe(404)
})

describe('injected scripts', () => {
test('@vite/client', async () => {
const hasClient = await page.$(
Expand Down