I recently used the base
config API in my Vite app for deployment reasons:
// vite.config.js
export default defineConfig({
plugins: [vue()],
base: "/f2e/",
});
The file structure, in a nutshell, looks like this:
app
╞-public
| └-foobar.jpg
└-src
└-App.vue
As you see, there’s an image in my app, using:
<!-- src.App.vue -->
<template>
<img src="/foobar.jpg" />
</template>
Not surprisingly, the <img />
element is broken since the path is incorrect:
In /foobar.jpg
The server is configured with a public base URL of /f2e/ – did you mean to visit /f2e/foobar.jpg instead?
I know that we can use /f2e/foobar.jpg
to fix the path, but, are there any APIs built in Vite that can access the base
config? Just something like:
<!-- src.App.vue -->
<template>
<img :src="locdBasePath() + '/foobar.jpg'" />
</template>
Because I don’t think attaching the /f2e/
path in an app is a good practice, and refactoring all paths takes a lot of effect.
Have read Configuring Vite but nothing useful for my situation.
2
Answers
Usually
base
is meant for serving project that is (and must) have its public root in some subdirectory on the server (remember the oldsite.com/~myusername
thing, it is something alike).What you need is, I believe, is to redefine a project root.
Another option would be to allow for serving
foobar.jpg
from other folders. For that you will need to add the folder containing it intoserver.fs.allow
list:From the docs for Public Base Path, I would have expected it to just work…
However, if it is not working you can always prepend the injected
import.meta.env.BASE_URL