Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

小程序中使用这个方法解析服务端 url,获取不到正确的 pathname #42

Open
alun-430 opened this issue Oct 10, 2022 · 0 comments

Comments

@alun-430
Copy link

parsePath('https://www.baidu.com/123?a=456')

image

看了一下实现

try {
var parsed = new URL(url);
output.protocols = protocols(parsed);
output.protocol = output.protocols[0];
output.port = parsed.port;
output.resource = parsed.hostname;
output.host = parsed.host;
output.user = parsed.username || "";
output.password = parsed.password || "";
output.pathname = parsed.pathname;
output.hash = parsed.hash.slice(1);
output.search = parsed.search.slice(1);
output.href = parsed.href;
output.query = Object.fromEntries(parsed.searchParams);
} catch (e) {
// TODO Maybe check if it is a valid local file path
// In any case, these will be parsed by higher
// level parsers such as parse-url, git-url-parse, git-up
output.protocols = ["file"];
output.protocol = output.protocols[0];
output.port = "";
output.resource = "";
output.user = "";
output.pathname = "";
output.hash = "";
output.search = "";
output.href = url;
output.query = {};
output.parse_failed = true;
}

使用 try 中使用的是 new URL(), 在微信小程序中 new 这个实例会出现报错走到 catch,还是低版本 parse-path 的会更通用

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant