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

updated dependencies, added trailingSlash option #19

Closed
wants to merge 1 commit into from
Closed

updated dependencies, added trailingSlash option #19

wants to merge 1 commit into from

Conversation

rauberdaniel
Copy link
Contributor

Updated Dependencies: the dependencies for testing (mocha, should) were out of date. I updated them which also makes test output much nicer.

added trailingSlash option: Using url-join with the arguments of an array of url parts and an object for settings now allows to set the trailingSlash option, which will keep slashes in front of # and ? instead of removing them.

Sample:

urljoin(['https://google.com', '#', 'foobar'])                        // outputs https://google.com#/foobar
urljoin(['https://google.com', '#', 'foobar'], {trailingSlash: true}) // outputs https://google.com/#/foobar

@rauberdaniel
Copy link
Contributor Author

This will probably also solve #17 in a more general way as well as #6 and #10

@srph
Copy link

srph commented Dec 4, 2016

Agreed. Closing #17 for this. The tests worked seamlessly, but my solution might run into some some issues with edge cases later on.

@srph srph mentioned this pull request Dec 4, 2016
@ArmorDarks
Copy link

I'd be more happy with ability to configure options globally.

For example:

urlJoin.config({
  trailingSlashes: true
})

urljoin(...) // -> will put trailling slashes

However, particularly in described case it's is unclear why would we need this option at all. # is url fragment, and as any url fragment, it always have to have slash before it.

In other words. in this example

urljoin(['https://google.com', '#', 'foobar']) // outputs https://google.com#/foobar

output is wrong not because of lack of option, but because of urljoin wrongly handles hashbangs during escaping. '#' for some reason not treated as an url fragment, while it should, because it is just an explicitly passed string.

I can not imagine situation, when you would not want slashe before #.

So, I'm against this PR until more real usecase will be shown. But I'm 👍 for fixing issue with hashbang.

@jfromaniello
Copy link
Owner

I agree with @ArmorDarks

@rauberdaniel
Copy link
Contributor Author

@ArmorDarks The most basic usecase would be https://example.org/whatever.html#headline2 which is a anchor to the element with ID headline2. The most basic functionality which should not be broken simply by adding a slash automatically.

@ArmorDarks
Copy link

ArmorDarks commented Mar 2, 2017

@rauberdaniel

In such case you should write not this

urljoin('https://example.org', 'whatever', '#', 'headline2')

because it should produce https://example.org/whatever/#/headline2. You've told explicitly that # should be an url fragment.

Since library can't guess did you want it to be be segmented, or #headline2, you should be more explicit:

urljoin('https://example.org', 'whatever', '#headline2')

Note how you specify # with headline in single segment, because it should be together.

This should produce https://example.org/whatever/#headline2.

/ before # might seem odd here for you, but in fact that's exactly what servers returns when you're asking for https://example.org/whatever — it gets resolved into https://example.org/whatever/, so it can be considered as enforcement of good practice.

Particularly your use case is a bit trickier, because it contains link to page, and we indeed can't add trailing slash here.

So this

urljoin('https://example.org', 'whatever.html', '#headline2')

Will result in invalid url https://example.org/whatever.html/#headline2.

I think it is possible to avoid by looking for segment before # occurrence and determination, does it contain extension. If it does, then next # will not have trailing slash. Slightly tricky, but should be possible.

Hm, though, it might be tricky because of possible domain name in previous segment.

I really start to think that merging hashes and queries this way isn't good idea at all, and that they should be passed as object with parameters, like { hash: heading2 }. Then it will be easier for lib to construct proper url

@mangelsnc
Copy link

Are there plans to merge this? We are stuck with an issue that will be fixed with this PR. We need this library preserves the last trailing slash before the query string.

@ArmorDarks
Copy link

@mangelsnc read my post above. Maybe we can think out better solution.

I don't think that rushing with adding new options without considering all other solutions is good idea. It's like patching huge hole with thin paper.

@jfromaniello jfromaniello deleted the branch jfromaniello:master March 23, 2022 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants