BattlefyBlogHistoryOpen menu
Close menuHistory

Secret superpowers of URIs

Ronald ChenNovember 15th 2021

Don’t you mean URL? Nope. URL is just a URI that points to a resource over a network. Uniform Resource Identifiers are more useful.

Always scheming

The first superpower of URIs is their secret identity. The most common usage is website URLs starting with https, but URIs are everywhere.

All URIs start with a scheme followed by a : colon. This allows consumers of the URI to quickly know if they support the scheme or not.

Looking at all the different examples of URIs, one might assume anything goes after the scheme, but that is not true. URIs have structure.

Infinite flexibility but orderly

The next superpower of URIs is the ability to shape-shift into all sorts of different use-cases. Using the most common scheme, https, here is a URI with all the various parts labelled.

Uniform Resource Identifier. (2021, November 12). On Wikipedia. https://en.wikipedia.org/wiki/Uniform_Resource_Identifier

More formally, the URI syntax is

URI = scheme ":" ["//" authority] path ["?" query] ["#" fragment]

The parts between [square brackets] are optional. Note how each optional part has a prefix? This allows URIs to be parsed with ease.

The path is separated with /forward/slashes/ and also shape-shifts. The path represents a tree structure. On web servers, this might be literally the file system tree with corresponding folders and files.

Similarly query represent a key-value map, but depending on the format, it could represent a full JSON document using the extended query string library qs.

The URI fragment (also called #hash) has some additional superpowers. It is used to refer to something within the resource the URI resolves to.

In HTML, the fragment is used to scroll id matching the fragment. In addition, since browsers know the fragment only has meaning for the client, fragments are not sent to the server when requesting the HTML document. This is why in OAuth 2.0, the access token is found in the fragment! Secrets in the fragment are not leaked CDN servers, they stay within the browser. Suddenly, seemingly esoteric method names such as parseHash actually make sense.

In JSON Pointer the fragment is used to refer to a JSON value, which could be in the current document. This allows circular JSON documents to be described.

But Paul Rudd is sexier

The third superpower of URIs is the ability to shrink using URI references. These are strings that can be resolved to full URIs given some additional context.

In HTML, let’s say we are on https://example.com/some/path, here are some examples of URI references and what they resolve to.

//example.com/some-path resolves to https://example.com/some-path

Omitting the scheme means continuing to use the current scheme. This was useful back in the day during thehttp to https transition to avoid mixed content.


. resolves to https://example.com/some/path

./other resolves to https://example.com/some/path/other

A single dot means the current path.


.. resolves to https://example.com/some

../other resolves to https://example.com/other

Two dots mean parent path.


/ resolves tohttps://example.com

/other resolves tohttps://example.com/other

Leading forward slash means absolute path.


#frag resolves to https://example.com/some/path#frag

other#frag resolves to https://example.com/some/path/other#frag


In JSON Reference, URI references are used to resolve to other JSON documents.

Apply liberally

URIs are incredibly powerful and I’m sure I’ve missed some of its superpowers. Define your own URIs with a custom scheme when you want to identify a resource. battlefy://global/people/jobs

Releasing sooner than MVP
November 22nd 2021

2022

Powered by
BATTLEFY