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

Relative links not handled properly in nested html files #4517

Closed
7 tasks done
andylizi opened this issue Aug 5, 2021 · 6 comments
Closed
7 tasks done

Relative links not handled properly in nested html files #4517

andylizi opened this issue Aug 5, 2021 · 6 comments

Comments

@andylizi
Copy link
Contributor

andylizi commented Aug 5, 2021

Describe the bug

Vite doesn't handle relative URLs when generating nested html files. Given the following file structure

`-- dist/
    |-- assets/
    |   |-- main.447a9352.js
    |   `-- nested.d75058e9.js
    |-- nested/
    |   `-- index.html
    `-- index.html

links in nested/index.html should be

<script type="module" crossorigin src="../assets/nested.d75058e9.js"></script>

but is

<script type="module" crossorigin src="assets/nested.d75058e9.js"></script>

Reproduction

  1. npm init vite@latest using vanilla preset, npm install etc.
  2. Copy index.html to nested/index.html.
  3. Create a vite.config.js file, set the base to "" and follow the offical example for MPA:
import { resolve } from "path";

export default {
    base: "",
    build: {
        rollupOptions: {
            input: {
                main: resolve(__dirname, "index.html"),
                nested: resolve(__dirname, "nested/index.html")
            }
        }
    }
}
  1. npm run build

System Info

System:
    OS: Windows 10 10.0.19042
    CPU: (8) x64 Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz
    Memory: 6.07 GB / 15.70 GB
  Binaries:
    Node: 16.3.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 7.15.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1023.0), Chromium (92.0.902.62)
    Internet Explorer: 11.0.19041.1
  npmPackages:
    vite: ^2.4.4 => 2.4.4

Used Package Manager

npm

Logs

No response

Validations

@dejour
Copy link
Contributor

dejour commented Aug 6, 2021

do not set base, then url will be absolute, and it will work.

@andylizi
Copy link
Contributor Author

andylizi commented Aug 6, 2021

do not set base, then url will be absolute, and it will work.

Yeah, that can work as a workaround. Empty base is explicitly said to be valid in the docs through, as is the nested MPA use case, so it's still an issue.

@dejour
Copy link
Contributor

dejour commented Aug 6, 2021

empty base is not the same as the default value "/", and according to the docs, the nested MPA use case does not set base option which make its value default

@andylizi
Copy link
Contributor Author

andylizi commented Aug 6, 2021

the nested MPA use case does not set base option which make its value default

The docs doesn't say base must be default for it to work either, and it doesn't make much sense for two seemly unrelated options to be conflicting - nested JS files handle relative URLs correctly, for example. The general assumption is that two features of the same program can coexist with each other, unless it's explicitly said otherwise. After all, what would be the point of a configuration if one can only copy-paste offical examples without changing anything?

@naufalfarras
Copy link

hi @dejour i also have this problem and i set the base to empty string because i need to upload dist folder to a subfolder inside a domain, so i cannot use absolute path, do you have any idea?

@andylizi
Copy link
Contributor Author

Fixed by #7644.

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

Successfully merging a pull request may close this issue.

3 participants