Skip to content

Allows local urls for sprites - #9225

Closed
webdeb wants to merge 1 commit into
mapbox:masterfrom
webdeb:wd-allow-local-urls-for-sprites
Closed

webdeb wants to merge 1 commit into
mapbox:masterfrom
webdeb:wd-allow-local-urls-for-sprites

Conversation

@webdeb

@webdeb webdeb commented Jan 23, 2020

Copy link
Copy Markdown
Contributor

Launch Checklist

  • briefly describe the changes in this PR
  • write tests for all new functionality

Local urls for sprites like "sprite: /assets/sprites" were not possible due to the implicit requirement of (protocol://). Slightly changed the "urlRe" to ignore missing protocol and the same for formatUrl. Additional tests were added, no breaking changes.

@webdeb
webdeb requested a review from mourner January 23, 2020 21:02
@andrewharvey

Copy link
Copy Markdown
Collaborator

Related tickets #8461 and #3636

@webdeb

webdeb commented Jan 24, 2020 •

Copy link
Copy Markdown
Contributor Author

Thanks for the links @andrewharvey

Seems to be an ongoing discussion since long time ago, so long, I don't remember the actual date. :)
I don't mind if this will get rejected, my current approach was to use

`${window.location.origin}/assets/map-sprite`

But, this is somewhat ugly imo, especially with ssr, you have to built on top of that workaround, mock the window obj.

Or this beauty here #3636 (comment)
It got 2 x 👍 (I am not sure, whats likeable about it, its frustrating...)

IMHO its natural to have custom sprites served from the current server, so please allow us to make our lives flexible, even if its dangerous and will break in other contexts, we know what we do (most of the time).

@kkaefer

kkaefer commented Jan 24, 2020

Copy link
Copy Markdown
Member

IMHO its natural to have custom sprites served from the current server

Styles are intended to be consumed by multiple different platforms, e.g. iOS applications, Android apps, and websites. Out of those, websites are the only one that have the concept of "current server" since they have an origin. Allowing relative URLs to styles means that styles won't work on those platforms anymore.

However, there is a much cleaner way to allow relative URLs for your website by providing a transformRequest callback function when initializing the Map object. E.g. adding this callback:

transformRequest: (url, resourceType) => {
    if (/^local:\/\//.test(url)) {
        return { url: new URL(url.substr('local://'.length), location.href).href };
    }
}

allows you to specify relative sprite URLs like "sprite": "local://./path/to/sprite" in your style. Please note that the ./ is necessary to denote that it's a relative path. you can leave out the . to specify a domain-relative path. Note that local:// is an arbitrary protocol; you can use whatever string you want there as long as it's alphanumeric (matches the regex (\w+):\/\/).

See the documentation for options.transformRequest for more information.

Given that there's no reasonable cross-platform way of adding support for relative URLs, but a JS-specific solution, I'm going to close this PR. Thanks for your contribution!

@webdeb

webdeb commented Jan 24, 2020 •

Copy link
Copy Markdown
Contributor Author

@kkaefer thanks for your explanation.

But your suggestion, to use local://./path in combination with transformRequest (even that it looks better and more elegant, than other suggestions), still adds complexity and performance impact on all requests to solve this problem, where my PR aimed to eliminate it and avoid further discussions on that topic. Furthermore, by applying it, I do not see any impact for people and apps, that run mapbox in their native server-free env.

@allthesignals

Copy link
Copy Markdown
Contributor

@kkaefer thanks for your explanation.

But your suggestion, to use local://./path in combination with transformRequest (even that it looks better and more elegant, than other suggestions), still adds complexity and performance impact on all requests to solve this problem, where my PR aimed to eliminate it and avoid further discussions on that topic. Furthermore, by applying it, I do not see any impact for people and apps, that run mapbox in their native server-free env.

Just want to also add that this solution seems to require configuring a local environment to support HTTPS (http would not work in this case...)

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.

4 participants