urlpattern icon indicating copy to clipboard operation
urlpattern copied to clipboard

make URLPattern and URLPatternList serializable/structured cloneable

Open wanderview opened this issue 5 years ago • 17 comments

During the call in w3c/ServiceWorker#1535 @asutherland asked if these types will be serializable. I interpreted this as asking if they can be structured cloned. I think it makes sense to so and we should include it in the proposal.

wanderview avatar Oct 09 '20 20:10 wanderview

To clarify, yes, you interpreted this right, but I think the current HTML spec terminology (and WebIDL attribute) is serializable, and the term cloneable is legacy but reflected in terminology used in engines.

asutherland avatar Oct 10 '20 00:10 asutherland

I'm not sure I will get to this in the first iteration, so tagging as an enhancement.

wanderview avatar Aug 11 '21 19:08 wanderview

URL is not serializable, why should this be? Can it not be serialized to a string?

annevk avatar Sep 11 '23 14:09 annevk

Hmm yeah, see also https://github.com/whatwg/url/issues/370

domenic avatar Sep 11 '23 14:09 domenic

AIUI, the reason for not serializing/cloning URL objects is that they have mutable attributes. Is that true?

URLPattern objects are immutable.

Are there other reasons why URL is not serializable or structure clonable?

wanderview avatar Sep 11 '23 14:09 wanderview

I think the main reason is that you can use strings already.

annevk avatar Sep 11 '23 14:09 annevk

A string isn’t guaranteed to be a valid URL - isn’t this akin to the entire motivation for trusted types?

ljharb avatar Sep 11 '23 14:09 ljharb

A URL instance also isn't guaranteed to be a valid URL (it only guarantees it's parseable). But since the receiver can mutate it I'm not sure if that property really buys you much.

annevk avatar Sep 11 '23 15:09 annevk

Again, I'm not sure those URL properties apply to URLPattern.

wanderview avatar Sep 11 '23 16:09 wanderview

URLPattern cannot be serialized?

annevk avatar Sep 12 '23 07:09 annevk

Maybe I misunderstood. I thought you were arguing URLPattern should not be serializable/clonable because URL is not serializable/clonable. But AIUI that restriction on URL is because its mutable, but URLPattern is immutable. (Maybe this is only an issue for cloning.)

wanderview avatar Sep 12 '23 12:09 wanderview

I think Anne was arguing that URLPattern should not be serializable/cloneable because you can just send its toString() version across and reconstruct on the other side, which is a similar story to URL.

My personal feeling is that this is an argument for deprioritizing the work, but not against the work.

My general stance on cloning/serializing is that we should probably do it a lot more across the web platform. But, since we don't today, it can be a bit strange for one API to come out and add serializibility. I can understand the fear that maybe we'll end up with a strange lumpy platform where what's serializable largely depends on the initiative of individual proposal authors.

domenic avatar Sep 12 '23 13:09 domenic

because you can just send its toString()

This is actually not true; I didn't realize. I think this has been discussed before, but I can't find it... Basically there is currently no algorithm for serializing an arbitrary URLPattern to a string, only for going from string -> URLPattern.

domenic avatar Sep 12 '23 13:09 domenic

I believe you can JSON.stringify(urlpattern) and then new URLPattern(JSON.parse(stringified)). Basically the URLPattern has the same form as the URLPatternInit dictionary, so if you can serialize it as an object it should predictably recreate the pattern.

I expect @asutherland might prefer something more efficient than this, though.

wanderview avatar Sep 12 '23 13:09 wanderview

I believe you can JSON.stringify(urlpattern) and then new URLPattern(JSON.parse(stringified)).

Unfortunately this doesn't quite work for at least two reasons:

  1. JSON.stringify(...) returns '{}' for URLPattern (it has no own properties, and no special JSONification).
  2. This would also drop options like {ignoreCase: true}.

jeremyroman avatar Sep 12 '23 14:09 jeremyroman

I expect @asutherland might prefer something more efficient than this, though.

I don't have a concern about efficiency. But I do have 2 different concerns:

  1. Developer ergonomics. URLPattern feels very much like a data primitive and it seems like hostile API design when putting a data primitive in a postMessage payload will suddenly cause the call to postMessage to throw. I hate responding to bug reports where a developer got bitten by something like this and my answer might as well start with "Well, actually...". The URL situation feels very similar; I understand why it doesn't make sense to let someone structured serialize/transfer an IDBTransaction instance because it's mutable, but it feels pedantic in the case of URLs. This also complicates encouraging developers to move things off the main thread and into workers.
  2. Ensuring web-compat for APIs that use URLPattern like ServiceWorkers by making it possible to specify a cross-browser concept of size for URLPattern, at least once https://github.com/whatwg/html/issues/4914 is addressed. As I touch on in https://github.com/WICG/urlpattern/issues/18#issuecomment-1716372503 there are 2 storage-related concerns that come up with potential uses of URLPattern: a) web-compat and b) avoiding allowing sites to abuse things that are not meant to be synchronously available storage APIs as sync storage APIs. Web-compat is the critical thing, though, and so it would be nice to make sure that we can specify per the WHATWG infra spec algorithm limit suggests a minimum size on URLPattern support across browsers. Since the meta concern is storage/memory size rather than arbitrary algorithm limits on specific fields, supporting structured serialization is a nice way to capture the size limits in a single value.

asutherland avatar Sep 12 '23 20:09 asutherland

I don't necessarily disagree, but also https://github.com/whatwg/url/issues/370 had like no demand in a period of five years whereas other URL API features were repeatedly requested over that same time period. Given that we have finite time, I'm not sure I'd invest it here.

annevk avatar Sep 13 '23 08:09 annevk