Assets Not Loaded with Backend Integration
Describe the bug
Hi!
I've created a basic backend integration setup. The production build is working. However during development, images in App.tsx aren't working. They're being fetched from the current port (8000), where they don't exist.
http://localhost:8000/dist/vite.svg
http://localhost:8000/assets/react.svg
For the Vite logo, the src is set to dist/vite.svg to make this work for production.
I can't tell if this is a bug or how this would be solved with config.
This bug was reported earlier here by @Zehua-Chen and @jessarcher [https://github.com/vitejs/vite/issues/7778](in this issue), but I could not find an answer on how to actually resolve this problem.
Reproduction
https://github.com/jvanmaaren/vite-backend-integration
Steps to reproduce
I've created a repository to show this issue: https://github.com/jvanmaaren/vite-backend-integration
To test development:
yarn backend-mock-devyarn dev- View
localhost:8000
To test production:
yarn backend-mock-prd- View
localhost:8000
System Info
System:
OS: macOS 13.2.1
CPU: (8) x64 Intel(R) Core(TM) i5-8279U CPU @ 2.40GHz
Memory: 291.48 MB / 16.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 16.15.0 - ~/.nvm/versions/node/v16.15.0/bin/node
Yarn: 1.22.15 - ~/.nvm/versions/node/v16.15.0/bin/yarn
npm: 8.5.5 - ~/.nvm/versions/node/v16.15.0/bin/npm
Browsers:
Chrome: 110.0.5481.177
Firefox: 110.0
Safari: 16.3
npmPackages:
@vitejs/plugin-react-swc: 3.2.0 => 3.2.0
vite: 4.1.4 => 4.1.4
Used Package Manager
yarn
Logs
No response
Validations
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [X] The provided reproduction is a minimal reproducible example of the bug.
- You can set
origintoorigin: 'http://localhost:8080',, so when the backend server is running the request will goes tolccalhost:8080/assets/react.svginstead oflocalhost:8000/assets/react.svg. This will make React SVG appears in dev. - I think you need to proxy the
localhost:8000/vite.svgtolocalhost:8080/vite.svg,servemight not support exteranl forward but https://www.npmjs.com/package/http-server could.
Hi, after the resolution of my original issue, I updated Vite's documentation on backend integration. I hope this helps!
Thanks for your reply @fi3ework and @Zehua-Chen!
I have
- replaced node serve with http-server
- changed port based on development/production server
- removed origin, so it defaults to port
- remove "dist" from "dist/vite.svg"
- pushed code to the example repository
Development now works. However one problem still remains:
On production (built), the Vite logo doesn't work:
It tries to fetch http://127.0.0.1:8000/vite.svg
That should be http://127.0.0.1:8000/dist/vite.svg
Do you know how I can handle this?
Hi @jvanmaaren , sorry for the late reply.
I took a look at your repository. Assets in the public/ folder will be served at Vite server's root. In your repo, public/vite.svg will be served at localhost:8080/vite.svg. Your App.tsx follows this assumption. This is why your development environment works properly.
During build, Vite will copy public/vite.svg to some-other-server/dist/vite.svg, your js bundle and vite generated html is also in some-other-server/dist/. If you load start the server in some-other-server/dist/, vite.svg will be at root and should load properly.
What you are trying to do is to start the server in some-other-server but use the js bundle in some-other-server/dist. Your JS bundle still loads but vite.svg is no longer at the root.
I can think of two solutions
- Don't use a different HTML for production (i.e.
some-other-server/index.html). Vite should generate a html for you insome-other-server/distusingsrc/index.html - Configure Vite to generate output in
some-other-server(without dist), and usesrc/main.tsxas the JS entry point (rather than looking atsrc/index.htmland try to figure out JS entry points, because this will overridesome-other-server/index.html). This can be done using the following snippet
import pluginReactSWC from "@vitejs/plugin-react-swc";
import { defineConfig } from "vite";
export default defineConfig(({ command }) => {
const isDev = command === "serve";
return {
base: isDev ? "" : "/dist/",
root: "./src",
publicDir: "../public",
build: {
outDir: "../some-other-server/",
rollupOptions: {
input: 'src/main.tsx',
output: {
assetFileNames: "[name].[ext]",
entryFileNames: "[name].js",
},
},
},
plugins: [pluginReactSWC()],
server: {
port: isDev ? 8080 : 8000,
},
};
});
Thanks @Zehua-Chen! That indeed seems to be the solution.
In Webpack we can easily load the dev environment (localhost:8080), where any non-react files would be proxied to load from the server (localhost:8000).
But since with Vite that isn't possible (as far as I know), and we're reversing the proxy, loading from different folders just doesn't seem possible with Vite.