docs: change docs to include more details about public directory
Description
This makes it clearer to understand how to use the public directory by saying how the fetch() function can be used instead of import to retrieve files from the public directory.
When I first started using Vite, I was confused with how to use the public directory, and I only found that fetch() could be used with it after searching for it online. I hope this makes it immediately clear how to use this feature.
Run & review this pull request in StackBlitz Codeflow.
Thanks for the PR! This may end up confusing others too. An asset can also be referenced in HTML and CSS for example. There are other ways to use a public asset other than fetch(). I think it may be better to keep the current wording. Leaving the PR open for a bit to see what others think.
Thanks for the PR! This may end up confusing others too. An asset can also be referenced in HTML and CSS for example. There are other ways to use a public asset other than
fetch(). I think it may be better to keep the current wording. Leaving the PR open for a bit to see what others think.
Hello, thank you for the comment. Out of interest, what other ways are there to retrieve public assets within JS other than fetch()? I'm working on some projects using Vite myself and it would be helpful to learn about how to handle files within public. Thanks!
If you're using a framework like Vue, then you normally use the public URL in the template. For example, see how the Vite logo in the public folder is used here. Maybe we could add the comment though with a different wording. The current line also doesn't match with the way our React template is constructed (see https://vite.new/react)
Yeah we actually have conflicting description with the docs and the template. I've also opened a discussion about that https://github.com/vitejs/vite/discussions/9276. I think we've not removed the restriction from the docs because we don't want to guarantee it to work, but at this point, I don't think we can realistically break it again, so maybe we should just remove that line 🤔
Well I guess if there aren't many possible issues from fetch() ing from JS then removing the line seems like a pretty good idea.
I've updated it so that the line is removed entirely.
Yeah, that makes it a lot clearer. Thx.