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

allow symlinked assets #5089

Merged
merged 7 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/violet-walls-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

allow symlinked static assets in dev
25 changes: 22 additions & 3 deletions packages/kit/src/core/dev/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,7 @@ export const sveltekit = function (svelte_config) {
const file = svelte_config.kit.files.assets + pathname;

if (fs.existsSync(file) && !fs.statSync(file).isDirectory()) {
const has_correct_case = fs.realpathSync.native(file) === path.resolve(file);

if (has_correct_case) {
if (has_correct_case(file, svelte_config.kit.files.assets)) {
req.url = encodeURI(pathname); // don't need query/hash
asset_server(req, res);
return;
Expand Down Expand Up @@ -501,6 +499,27 @@ async function find_deps(vite, node, deps) {
await Promise.all(branches);
}

/**
* 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;
}

/**
* @param {import('types').ValidatedConfig} svelte_config
* @return {import('vite').Plugin[]}
Expand Down
1 change: 1 addition & 0 deletions packages/kit/test/apps/basics/static/symlink-from
1 change: 1 addition & 0 deletions packages/kit/test/apps/basics/static/symlink-to/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
6 changes: 6 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2583,6 +2583,12 @@ test.describe.parallel('Static files', () => {
const response = await request.get('/static.JSON');
expect(response.status()).toBe(404);
});

test('Serves symlinked asset', async ({ request }) => {
const response = await request.get('/symlink-from/hello.txt');
expect(response.status()).toBe(200);
expect(await response.text()).toBe('hello');
});
});

test.describe.parallel('Matchers', () => {
Expand Down