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

Asset Not Loaded When Integrating With Existing Backend #7778

Closed
7 tasks done
Zehua-Chen opened this issue Apr 17, 2022 · 8 comments · Fixed by #8103
Closed
7 tasks done

Asset Not Loaded When Integrating With Existing Backend #7778

Zehua-Chen opened this issue Apr 17, 2022 · 8 comments · Fixed by #8103
Labels
documentation Improvements or additions to documentation

Comments

@Zehua-Chen
Copy link
Contributor

Zehua-Chen commented Apr 17, 2022

Describe the bug

When I import an asset

import audio from "./do.mp3";

audio resolves to something like base/**/do.mp3 in development mode. I am integrating Vite into an existing backend. The backend is running at localhost:4000 and vite is running at localhost:3000. So during development, the existing backend at localhost:4000 does not yet have base/**/do.mp3.

For production though, it works fine, vite build will produce the files that the existing backend at localhost:4000 will be able to serve

I think that this should be noted in the docs or maybe audio can resolve to something like localhost:3000/base/**/do.mp3 instead

Reproduction

https://stackblitz.com/edit/vitejs-vite-kkwd9h?file=src/main.js

System Info

System:
    OS: macOS 12.3.1
    CPU: (8) arm64 Apple M1 Pro
    Memory: 179.39 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.14.2 - ~/.nvm/versions/node/v16.14.2/bin/node
    Yarn: 1.22.18 - ~/.nvm/versions/node/v16.14.2/bin/yarn
    npm: 8.6.0 - ~/.nvm/versions/node/v16.14.2/bin/npm
  Browsers:
    Chrome: 100.0.4896.88
    Safari: 15.4
  npmPackages:
    vite: ^2.9.1 => 2.9.1

Used Package Manager

yarn

Logs

No response

Validations

@sapphi-red
Copy link
Member

It is mentioned here. Or do you have a different situation?

Also make sure the server is configured to serve static assets in the Vite working directory, otherwise assets such as images won't be loaded properly.
https://vitejs.dev/guide/backend-integration.html#:~:text=Also%20make%20sure%20the%20server%20is%20configured%20to%20serve%20static%20assets%20in%20the%20Vite%20working%20directory%2C%20otherwise%20assets%20such%20as%20images%20won%27t%20be%20loaded%20properly.

@Zehua-Chen
Copy link
Contributor Author

I am sorry, I think I might have misunderstood the term "working directory".

I understood it as Vite and my own server should start in the same working directory, whereas I think what the documentation is saying is I need to redirect static file requests to vite.

It's all fixed now!

@Zehua-Chen
Copy link
Contributor Author

Do you think it's a good idea for me to open a pull request and change the wording a bit?

@sapphi-red
Copy link
Member

Yes it can be solved with proxying static file requests to vite. IIUC it can also be solved with serving static files from the custom backend server.

It would be great to have a PR.

@sapphi-red sapphi-red added documentation Improvements or additions to documentation and removed pending triage labels May 1, 2022
@jessarcher
Copy link
Contributor

I'm also running into this issue with a Laravel backend server.

Would it be reasonable in dev mode for Vite to include the full URL, instead of just the path? E.g. http://localhost:3000/resources/images/logo.png instead of /resources/images/logo.png.

This would avoid the need to proxy or serve the project root. Laravel is already serving a public subdirectory, and it can contain static assets that Vite doesn't know about so it's not trivial to have Laravel conditionally proxy, redirect, or serve Vite assets only in dev.

I have tried specifying http://localhost:3000 as the base option in Vite during dev, but Vite strips out the protocol, host, and port in dev mode.

@innocenzi
Copy link
Contributor

innocenzi commented May 10, 2022

I think you are just missing server.origin. With the proper configuration, I cannot reproduce the issue:

// vite.config.ts
import { defineConfig } from 'vite'

export default defineConfig({
  server: {
    origin: 'http://localhost:4000',
  },
})

Reproduction: https://stackblitz.com/edit/vitejs-vite-4qrrx3?file=vite.config.ts
Related PR: #5104

@Zehua-Chen
Copy link
Contributor Author

I have opened a PR at #8103 to incorporate the discussion in this issue.

patak-dev pushed a commit that referenced this issue May 10, 2022
Co-authored-by: Enzo Innocenzi <enzo@innocenzi.dev>
@jessarcher
Copy link
Contributor

I don't think server.origin fixes it for me.

Hopefully this explains the issue I'm trying to solve:

Example Laravel project structure:

public/ <-- nginx is serving this directory
  images/
    taylor.jpg
resources/
  images/
    abigail.jpg
  js/
    app.js
    Welcome.vue

Example Vite config:

{
    base: command === 'build' ? 'build' : '',
    publicDir: false,
    build: {
        manifest: true,
        outDir: 'public/build',
        rollupOptions: {
           input: 'resources/js/app.js'
        }
    }
}

Welcome.vue:

<!--
    Vite should leave absolute paths as-is so nginx can serve them from the
    public directory.

    The PR at https://github.com/vitejs/vite/pull/6779 hopefully addresses this
    but I haven't tested yet.
-->
<img src="/images/taylor.jpg" />

<!--
    Vite resolves this from disk and rewrites the URL.

    In build mode, the file is versioned in the outDir (e.g. public/build) and
    the URL is correctly rewritten as `/build/assets/abigail.123456.js` which
    nginx can serve.

    In serve mode, Vite serves the file at
    `http://localhost:3000/images/abigail.jpg` but the URL is only rewritten as
    `/images/abigail.jpg`, so the browser makes the request to the backend
    server where the file does not exist publicly. Setting `server.origin` to
    the backend server would only make this unwanted behaviour explicit.
    Ideally, this would be rewritten to include the full URL to the Vite server
    (`http://localhost:3000/images/abigail.jpg`).
-->
<img src="../images/abigail.jpg" />

@github-actions github-actions bot locked and limited conversation to collaborators May 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants