-
Notifications
You must be signed in to change notification settings - Fork 22
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
Why are baseUrl and path separate fields? #1
Comments
Partly its allow future extension to control parts of the origin via patterns as well. I'm not sure if that would be an Also, the existing Finally, we have to provide base URLs in some way in order to support relative paths. This seems valuable to me in general, but is also needed for backward compat with the service worker scopes behavior. |
Would those be alternatives to If you intend this to be an origin, it should be called
That's fairly different from the origin. The Do you intend But then it would be simpler to just combine into a single field.
I don't really understand this one. SW scope today is resolved against the script URL as base (so it can be relative). If we combined |
The baseUrl is not just for origin. It is to support relative paths. Not sure how to more clearly state that. For example, the intent is so someone can write their script like:
And then the app can be re-hosted to a new location at a possibly different sub-path and it will still work correctly to its relative |
But why does the user need to supply an absolute URL at all? Let's say you combined let pattern = new URLPattern({
originAndPath: 'https://example.com/myapp/content/index.html'
}); or, if you wanted a relative URL, you'd simply omit the origin and any part of the path that's part of the current page's location, supplying a relative URL as a pattern: let pattern = new URLPattern({
originAndPath: 'content/index.html'
}); There's no need to explicitly supply |
I guess a relative path is the same as a pattern with a variable host, start-of-path, etc. That could be considered, but it is definitely not something we would want for service workers. We must have an absolute URL prefix in order to do scope matching. I can't recall if I explicitly called this out above, but the other reason I favor separate fields like |
The current
Scope is resolved relative to the URL of the document calling Why can't you resolve the URLPattern against the document URL in the same way? |
Personally I don't mind an implicit base URL, but I anticipate opposition to that like in whatwg/url#71. |
I view whatwg/url#71 as being somewhat of a special case. That is a discussion about the behaviour of the URL constructor itself, wherein I agree with the opposition: the constructor of a simple data-holding class such as URL should not consume data from the surrounding environment, they should just hold the data you give them. On the other hand, nearly every other thing on the entire web platform that consumes a URL string does so relative to the document URL (or the worker script URL, if inside a worker). A few examples that come to mind:
In fact, this is so common that the HTML spec defines "parse a URL" wrapper around the URL parser, which automatically resolves the given URL string against the document URL. I think any new web API that takes a URL string should allow it to be relative and resolve it against the document URL. Technically, you're not taking a URL string, you're defining a new syntax for a "URL pattern", but I think the same principle should apply. |
And if we remove the dictionary in favor of simple strings like you request in #4 then we end up with pretty much the same constructor API shape as Anyway, I'd really like to get @annevk's input on this. I'm hoping we can discuss at TPAC. |
Note, if we dropped |
The way I would view it is as an analogy with URL: we wouldn't be saying that when you construct a URLPattern, it is automatically resolved against the document URL (just like when you construct a URL, it isn't automatically resolved against the document URL). Rather, there would be "absolute URLPatterns" and "relative URLPatterns" (based on the same logic as a URL: if the URLPattern begins with a scheme then it's absolute, otherwise relative), then when the URLPattern is applied in a real-world usage such as being used in a service worker scope, relative patterns are resolved against the base URL (just like whenever a URL is used in a specific context, it gets resolved against the base URL). For example, EDIT: Oops, I realised the above is a mistake, since the URL constructor does not allow a relative URL string (i.e., there is no such thing as a "relative URL" (object), but there is a "relative URL string"). Still, I think the principle applies: ideally we would want there to be such a thing as a "relative URL pattern". The awkward thing about comparing this to URL is that even though there is a URL class defined, it is never used for any APIs --- all APIs take strings to represent URLs; the URL class is just a convenience class for users and it also defines the behaviour of internal URL parsing. So I think we want URLPattern to follow the lead of URL strings, not URL objects, then what I said above still makes sense. |
Personally I agree that there shouldn't be an implicit base URL. However, I am still unsure whether including baseURL + path is a good idea. In particular the following alternatives seem nicer to me: // Actually orthogonal components, but just as powerful:
new URLPattern({
origin: self.origin,
pathname: "foo/bar"
});
new URLPattern({
origin: self.origin,
pathname: new URL("relative/bar", self.location).pathname
});
// Combined
new URLPattern({
prefix: new URL("foo/bar", self.origin)
});
new URLPattern({
prefix: new URL("foo/bar", self.location)
}); The |
Why are we trying to eliminate the "relative URL pattern"? Relative URLs are super useful as a concept; for one thing they let you build a site that's independent of where it's hosted, and it seems like being able to specify patterns (such as service worker scope) relative is just as useful. (Current service worker scope is possible to use a relative URL prefix.) Why invent a less natural way to do that when the concept of a relative URL already exists? The https://url.spec.whatwg.org/#url-apis-elsewhere :
So if you're going to use Alternatively, if you want
I don't understand why those alternatives seem nicer. All of those require at least some dynamic logic as opposed to being completely declarative. None of these are possible in the web app manifest where it's just JSON (no code). Why not |
I've attempted to the split the difference in the new revamped proposal. Some of this is in the detailed design doc, however, since I tried to keep the explainer focused on use cases. To summarize the new proposal:
So, if you are trying to match any resources loading a jpg, can you write it briefly as:
An example of the short form might be matching any subdomain html documents:
I'm hoping this is an acceptable compromise between the need I feel for a verbose featureful syntax and the desire for a more concise syntax. |
I'm going to close this for now, but please feel free to re-open it if you think further discussion is needed. Or possibly open a new issue specific to the new revamped proposal. Thank you! |
Old thread but since it doesn’t appear to have been mentioned: one benefit of a distinct baseURL is that it helps avoid common concatenation errors like the one in the initial post: baseUrl: self.origin + 'foo/?*' It’s pretty easy to forget that "/" is the start of the pathname component rather than the end of the origin component — this would be e.g. |
It seems simpler and equally expressive to just have a single field representing the origin and path parts of the scope. I'm not sure what to call it, perhaps just
baseUrl
?becomes:
Alternatively, if you want to keep them separate, why not rename
baseUrl
toorigin
?The text was updated successfully, but these errors were encountered: