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

fix: root import #7224

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/vite/src/node/server/middlewares/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ import type { Connect } from 'types/connect'
export function baseMiddleware({
config
}: ViteDevServer): Connect.NextHandleFunction {
const base = config.base
const { base, root } = config

// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
return function viteBaseMiddleware(req, res, next) {
const url = req.url!
const parsed = parseUrl(url)
const path = parsed.pathname || '/'

if (path.startsWith(root)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should check for path.startsWith(root + base), then replace to req.url = url.replace(root + base, '/').

If we have root as /home/xxx/, and base equals to /home/, the current implementation will fail, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

root + base = /home/xxx/home/ ?

In my test case, path will be <root>/[relative path from root]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, gotcha, you are actually trying to fix the other way around.
I thought the bug was that /home/xxx/home/path was converted into /xxx/home/path, that seems to be an issue currently.
I don't understand your case, I think that absolute paths will be covered by the rule I proposed. And paths relative to the project root will also work.
The only case that won't be covered is is when you are trying to have an absolute path out of root, but in that case Vite will use /@fs/, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I update the desc.

If set the base URL to root on Linux and use vue plugin, vue plugin will import css by absolute path. And vite will replace base to /, and absolute path startWith base path will got a error.

Copy link
Member Author

@poyoho poyoho Mar 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@patak-dev the case is

config with

{
  base: "/home/"
}
  • had absolute path /home/xxx
  • root is /home/xxx/project

so the request must be /home/xxx/project/assets (because the request will call by the vue plugin)

and then replace root to be /assets

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the bug was that /home/xxx/home/path was converted into /xxx/home/path, that seems to be an issue currently.

Is there a way to reproduce this possible? I've been thinking about it for a long time and can't figure it out. 😂

// If set the base URL to root on Linux, need to replace root to emtry string (#7220)
req.url = url.replace(root, '')
return next()
}

if (path.startsWith(base)) {
// rewrite url to remove base.. this ensures that other middleware does
// not need to consider base being prepended or not
Expand Down