vite icon indicating copy to clipboard operation
vite copied to clipboard

Enforce case sensitivity when serving static files

Open benmccann opened this issue 1 year ago • 1 comments

Description

I'd like to serve a file if the case matches what's on the filesystem. That's how it works on Linux, but Mac and Windows have a different behavior. It'd be nice to enforce case sensitivity so that it works if you develop on one OS and then deploy to another

Suggested solution

If possible, add this as an option to sirv (https://github.com/lukeed/sirv/issues/141) and then turn on that option. Otherwise, implement it directly here:

/**
 * Determine if a file is being requested with the correct case,
 * to ensure consistent behaviour between dev and prod and across
 * operating systems. Note that we can't use realpath here,
 * because we don't want to follow symlinks
 * @param {string} file
 * @param {string} assets
 * @returns {boolean}
 */
function has_correct_case(file, assets) {
	if (file === assets) return true;

	const parent = path.dirname(file);

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

	return false;
}

Alternative

No response

Additional context

SvelteKit historically hasn't used the publicDir and base options, but does its own static asset serving. I put together a branch to use Vite's implementation, but some of our tests are failing with it due to this issue, which is a blocker for us.

https://github.com/sveltejs/kit/pull/5601/files#r925101030

Validations

benmccann avatar Jul 20 '22 23:07 benmccann

Not sure if needed here but this exists in case you want to know if the os is insensitive. https://github.com/vitejs/vite/blob/1e078ad1902ae980741d6920fc3a72d182fcf179/packages/vite/src/node/utils.ts#L203

dominikg avatar Jul 21 '22 05:07 dominikg