vite icon indicating copy to clipboard operation
vite copied to clipboard

Assets Not Loaded with Backend Integration

Open jvanmaaren opened this issue 3 years ago • 5 comments

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-dev
  • yarn 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

jvanmaaren avatar Feb 28 '23 12:02 jvanmaaren

  1. You can set origin to origin: 'http://localhost:8080',, so when the backend server is running the request will goes to lccalhost:8080/assets/react.svg instead of localhost:8000/assets/react.svg. This will make React SVG appears in dev.
  2. I think you need to proxy the localhost:8000/vite.svg to localhost:8080/vite.svg, serve might not support exteranl forward but https://www.npmjs.com/package/http-server could.

fi3ework avatar Feb 28 '23 17:02 fi3ework

Hi, after the resolution of my original issue, I updated Vite's documentation on backend integration. I hope this helps!

zehuac2 avatar Mar 03 '23 18:03 zehuac2

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?

jvanmaaren avatar Mar 06 '23 14:03 jvanmaaren

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

  1. Don't use a different HTML for production (i.e. some-other-server/index.html). Vite should generate a html for you in some-other-server/dist using src/index.html
  2. Configure Vite to generate output in some-other-server (without dist), and use src/main.tsx as the JS entry point (rather than looking at src/index.html and try to figure out JS entry points, because this will override some-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,
    },
  };
});

zehuac2 avatar Mar 18 '23 19:03 zehuac2

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.

jvanmaaren avatar Mar 20 '23 13:03 jvanmaaren