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

vite base full url option seems not work in development #3107

Closed
5 of 6 tasks
lovetingyuan opened this issue Apr 23, 2021 · 9 comments
Closed
5 of 6 tasks

vite base full url option seems not work in development #3107

lovetingyuan opened this issue Apr 23, 2021 · 9 comments

Comments

@lovetingyuan
Copy link

Describe the bug

vite base full url option seems not work in development

export default defineConfig({
  base: 'http://localhost:3000/test/',
  plugins: [vue({
    template: {
      transformAssetUrls: {
        base: 'http://localhost:3000/test/',
      }
    }
  })]
})

image

Reproduction

https://github.com/lovetingyuan/vite-base-full-url

System Info

Output of npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers:

  System:
    OS: macOS 10.15.7
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 342.65 MB / 16.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 14.15.4 - ~/.nvm/versions/node/v14.15.4/bin/node
    Yarn: 1.22.5 - /usr/local/bin/yarn
    npm: 6.14.10 - ~/.nvm/versions/node/v14.15.4/bin/npm
  Browsers:
    Chrome: 90.0.4430.85
    Edge: 90.0.818.42
    Firefox: 82.0.2
    Safari: 14.0
  npmPackages:
    @vitejs/plugin-vue: ^1.2.1 => 1.2.1 
    vite: ^2.1.5 => 2.2.1 

Used package manager:
yarn


Before submitting the issue, please make sure you do the following

@joeldenning
Copy link

I also have encountered this, and investigated the cause. It seems related to the following code:

if (isExternalUrl(base)) {
if (!isBuild) {
// get base from full url during dev
const parsed = parseUrl(base)
base = parsed.pathname || '/'
}

Notice how in the above code, the base is converted to only its path name during the serve target. The path (without the host/origin part of the base) is then used in the code below when creating the vue sfc compiler's assetUrlOptions.

assetUrlOptions = {
base:
options.devServer.config.base +
slash(path.relative(options.root, path.dirname(filename)))
}

I explored a possible fix by commenting out the logic in resolveBaseUrl that causes this. It got close to working, but the vite web socket connection was made to the incorrect URL. Not sure if that was the correct approach, but it seems that if the web socket logic is updated to support absolute base urls, that everything might work.

@TechAkayy
Copy link

This has been hitting me very badly past few days, hope this gets prioritised.. Thank you!

@alfeg
Copy link

alfeg commented Nov 8, 2021

Is there some sane workaround without hardcoding ulrs and string replacing?

@jonaskuske
Copy link
Contributor

@alfeg You can try the <base> HTML element.

Add (e.g.) <base href="http://localhost:3000" /> to your HTML <head>, then something like <script src="/foo.js"> will resolve to http://localhost:3000/foo.js.

<base> can mess with quite a few other parts of your site however (you might not want all URLs with unspecified host to resolve to your base, data URIs can break, ..), so whether it solves your use case or not is a bit hit or miss.

@cshawaus
Copy link

cshawaus commented Nov 9, 2021

This is a highly-simplified version of what I use for client projects but it accomplishes the same thing. I typically have a custom global set called BASE_URL which I use in place of import.meta.env.BASE_URL. This is not a perfect solution but it solves the issue nicely without needing to change much.

export default defineConfig(({ command }) => {
  const baseUrl = command === 'serve' ? 'http://localhost:3000/' : '';

  return {
    define: {
      BASE_URL: JSON.stringify(baseUrl),
    },
  };
});

@innocenzi
Copy link
Contributor

I think this is fixed by #5104

@alfeg
Copy link

alfeg commented Feb 5, 2022

@innocenzi awesome!

Can confirm that at least in v2.7.12 this bug is no longer reproduced:

Added to vite.config

{
    ...
    server: {
       ...
       origin: "http://localhost:3000",
    }
    ...
}

Now all assets are loading without issues

@Niputi Niputi closed this as completed Feb 5, 2022
@Valian
Copy link

Valian commented Feb 11, 2022

@innocenzi @alfeg Sadly I'm not sure what's going on, but I still have a problem with this... Could you tell me what I'm doing wrong? Vite in development is proxied through the backend server, so real URL is localhost:8080. For various reasons I'm proxying only index.html, all assets should load directly from localhost:3000.

Minimal repro, fresh project

npm create [email protected]
cd vite-project
vim vite.config.js  # add server.origin
npm run dev
# vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
  plugins: [vue()],
  server: {origin: "http://localhost:8080/"}
})

Response from dev server curl localhost:3000

<!DOCTYPE html>
<html lang="en">
  <head>
    <script type="module" src="/@vite/client"></script>
    <!-- ... -->
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/main.ts"></script>
  </body>
</html>

Both /@vite/client and /src/main.ts are served without origin preprended. <base href="address"> in index.html also doesn't work, since /@vite/client is added to the very top of <head>, before <base>.

@alfeg how does it work for you?

@cshawaus
Copy link

@Valian The intended purpose behind origin is to change the URL behaviour for assets. Your use case appears to be proxying Vite's DevServer which you would require configuring the proxy settings along with some other settings.

I would recommend raising a separate ticket with a working example to better understand as it is not related to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Feb 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants