-
Notifications
You must be signed in to change notification settings - Fork 43
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: respect fragment in trailing slash utils #175
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #175 +/- ##
==========================================
+ Coverage 95.11% 95.27% +0.15%
==========================================
Files 7 7
Lines 840 867 +27
Branches 180 187 +7
==========================================
+ Hits 799 826 +27
Misses 41 41 ☔ View full report in Codecov by Sentry. |
src/utils.ts
Outdated
@@ -52,35 +52,50 @@ export function isScriptProtocol(protocol?: string) { | |||
const TRAILING_SLASH_RE = /\/$|\/\?/; | |||
|
|||
export function hasTrailingSlash(input = "", queryParameters = false): boolean { | |||
const [urlWithoutFragment] = input.split("#"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am concerned about the performance implications of doing this (as this is a very low level util). Maybe we can update TRAILING_SLASH_RE
and add another for !queryParameters
branch to use reges based testing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand. Will ping you and update with a better approach ensuring it won't cause much overhead 👍
Two questions: Would you prefer another argument (e.g. Do you think the perf implication applies to (updated my PR to add the functionality to the |
Another thing: Trailing slash should only applied if it is http/https protocol or relative link. (so no tel/mailto protocol) |
@pi0 I've updated the PR to include protocol handling and also wrapped fragment handling so perf overhead is reduced Do you have further recommendations? BenchmarkTL;DR - Perf overhead was only the protocol checking. I've added another param for it in the PR. Current mainwithTrailingSlash, no fragment in url x 105,797,840 ops/sec ±1.41% (93 runs sampled) withTrailingSlash, no fragment in url, queryParams true x 51,973,496 ops/sec ±1.33% (93 runs sampled) hasTrailingSlash, no fragment in url x 97,867,504 ops/sec ±4.83% (81 runs sampled) hasTrailingSlash, no fragment in url, queryParams true x 45,007,719 ops/sec ±1.53% (90 runs sampled) ( This PRwithTrailingSlash, no fragment in url x 108,017,450 ops/sec ±1.05% (97 runs sampled) withTrailingSlash, no fragment in url, queryParams true x 51,390,197 ops/sec ±1.03% (92 runs sampled) withTrailingSlash, no fragment in url, queryParams true, protocol true x 32,238,491 ops/sec ±0.94% (95 runs sampled) withTrailingSlash, no fragment in url, queryParams false, protocol true x 47,730,336 ops/sec ±1.00% (95 runs sampled) hasTrailingSlash, no fragment in url x 117,062,355 ops/sec ±1.01% (95 runs sampled) hasTrailingSlash, no fragment in url, queryParams true x 42,962,486 ops/sec ±1.99% (88 runs sampled) Code for benchesimport Benchmark from "benchmark"
import { withTrailingSlash, hasTrailingSlash } from "ufo"
const { log } = console
const suite = new Benchmark.Suite()
suite.on("cycle", (event) => {
log(String(event.target))
})
suiteHelper('withTrailingSlash', withTrailingSlash)
suiteHelper('hasTrailingSlash', hasTrailingSlash, false)
suite.run()
function suiteHelper(name, fn, hasProtocol = true) {
suite.add(`${name}, no fragment in url`, () => {
fn('/abc/')
})
suite.add(`${name} fragment in url`, () => {
fn('/abc/#abc')
})
suite.add(`${name}, no fragment in url, queryParams true`, () => {
fn('/abc/?test=true', true)
})
suite.add(`${name}, fragment in url, queryParams true`, () => {
fn('/abc/?test=true#abc', true)
})
if (hasProtocol) {
suite.add(`${name}, no fragment in url, queryParams true, protocol true`, () => {
fn('/abc/?test=true', true, true)
})
suite.add(`${name}, fragment in url, queryParams true, protocol true`, () => {
fn('/abc/?test=true#abc', true, true)
})
suite.add(`${name}, no fragment in url, queryParams false, protocol true`, () => {
fn('/abc/', false, true)
})
suite.add(`${name} fragment in url, queryParams false, protocol true`, () => {
fn('/abc/#abc', false, true)
})
}
}
|
Last changes fix the issue that a sole fragment should never be changed
|
Resolves #174
It also will not change the trailing slash behavior if there is a protocol other than
http
orhttps
present (e.g.mailto:
)