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

Import a path from public folder by import.meta.glob has a unexpected behavior #15190

Open
4 tasks done
s3xysteak opened this issue Nov 30, 2023 · 5 comments
Open
4 tasks done

Comments

@s3xysteak
Copy link

Description

For example, we got a file named 'bar.json' at /public/lfs/bar.json. We can use import to get the path.

import url from '/lfs/bar.json?url'

console.log(url) 
// Will log '/lfs/bar.json'

However, by using import.meta.glob:

  1. import.meta.glob('/lfs/bar.json') will get nothing, because there is no 'bar.json' in /lfs/, it is actually in /public/lfs/
  2. import.meta.glob('/public/lfs/bar.json') will get /public/lfs/bar.json. But what we want is /lfs/bar.json

Suggested solution

None :(

Alternative

Instead of import.meta.glob('/public/lfs/bar.json'), using import.meta.glob('/lfs/bar.json') like import()

Additional context

No response

Validations

@yaolunmao
Copy link

我也遇到了相同的问题,单独引入是可以的,批量引入必须带/public,而且批量引入控制台会警告,请问您解决了吗

@0529bill
Copy link

I face the same problem, rn I use regex to change the path according to different env modes!

if (import.meta.env.PROD) { const regex = /\/public(.+)/; imgPath = imgPath.replace(regex, "$1"); }

@Career200
Copy link

Career200 commented Jun 1, 2024

Yeah, encountering the same problem with import.meta.glob("/public/avatar/*.png") - either I use /public path and get images along with Assets in the public directory are served at the root path warning, or remove public and get no avatars. Weird and confusing...

@jcarrano
Copy link

jcarrano commented Feb 1, 2025

I'm having the same problem here. As an example.

The following works:

const f1path = import("/fonts/aldrich-v21-latin-regular.woff2?url");

The following does not work:

const fontPaths = import.meta.glob("/fonts/*.woff2", {
  eager: true,
  query: "?url",
  import: "default",
});

However if I change /fonts/ by /public/fonts it works, but it returns all paths with public and produces a warning:

Assets in the public directory are served at the root path.
Instead of /public/fonts/aldrich-v21-latin-regular.woff2?url, use /fonts/aldrich-v21-latin-regular.woff2?url.

@jcarrano
Copy link

jcarrano commented Feb 1, 2025

There is one additional problem: when building the application (as opposed to running the dev server), the generated URLs are inside the client/assets directory, which does not match the URL used in the CSS, so the preloads I'm trying to generate are ineffective.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants