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

Base URL with Custom Protocol #13540

Closed
7 tasks done
Monadic-Cat opened this issue Jun 16, 2023 · 3 comments · Fixed by #17369
Closed
7 tasks done

Base URL with Custom Protocol #13540

Monadic-Cat opened this issue Jun 16, 2023 · 3 comments · Fixed by #17369
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@Monadic-Cat
Copy link

Describe the bug

So, this is a repeat of #8577. I need to set a base URL with a custom protocol, and Vite doesn't recognize example_no_http://website as an external URL.

(Because the issue is exactly identical, I'm reusing the reproduction example.)

The relevant code in Vite is:

export const externalRE = /^(https?:)?\/\//
export const isExternalUrl = (url: string): boolean => externalRE.test(url)

This can be fixed by either:

  1. Adjusting the regex externalRE to accept any valid URI protocol.
  2. Changing isExternalUrl to use an existing URL parser (say, url.parse() in node) and check if there's a protocol provided.
    import url from 'node:url';
    export const isExternalUrl = (url_: string): boolean => url.parse(url_).protocol !== null

My use case: I am using Tauri and need to serve some assets under a different host than the page which requests them, because I am using a CSP to prohibit scripts loaded from the same domain from running. I therefore cannot use relative URLs for asset loading, and ran into this problem because generally speaking custom protocols are how you serve assets in a Tauri app.

(https://docs.rs/tauri/1.4.0/tauri/struct.Builder.html#method.register_uri_scheme_protocol)

Reproduction

https://stackblitz.com/edit/vitejs-vite-bu8uph?file=dist/index.html

Steps to reproduce

  1. pnpm build

System Info

System:
    OS: Linux 5.4 Ubuntu 20.04.6 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
    Memory: 1.54 GB / 15.50 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 18.13.0 - ~/.nvm/versions/node/v18.13.0/bin/node
    Yarn: 1.22.5 - /usr/bin/yarn
    npm: 8.19.3 - ~/.nvm/versions/node/v18.13.0/bin/npm
    Watchman: 4.9.0 - /usr/bin/watchman
  Browsers:
    Chrome: 114.0.5735.106
    Firefox: 114.0.1
  npmPackages:
    vite: ^4.3.9 => 4.3.9

Used Package Manager

npm

Logs

No response

Validations

@stackblitz
Copy link

stackblitz bot commented Jun 16, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@patak-dev patak-dev added p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Jul 28, 2023
@patak-dev
Copy link
Member

Your 2 method sounds good to try it out. PR welcome, and we can check using ecosystem-ci if things look good. We can then move with it in Vite 5 if others are ok with this change too.

@bluwy
Copy link
Member

bluwy commented Aug 2, 2023

I think we can try method one and allow all kinds of protocol. Parsing it feels a bit expensive especially that isExternalUrl is hot code.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants