Selectors in AEM
Asked Answered
M

1

6

What is the use of selectors in Sling?

http://www.resourcePath.selector.extension

I have read the documents online about the usage of selectors:

  1. Some of the documents say that it is used for caching response from a page which cannot be done when using query parameters.

  2. While some suggest that Selectors are used to respond to different conditions using the same resource. For example we had an implementation where if the page(cq:page) is the last page in the hierarchy, it should display lets say a html-block while if its not the last page (i.e. if it has child pages), it should not display the said block. Here we used a script in the component and added this script name in the URL as selector based on the condition whether it is the last page or not.

But i am not sure which one of it is true.

Thanks in advance for any guidance.

Mcarthur answered 17/3, 2017 at 9:7 Comment(0)
T
12

A selector is a special form of parameter for the URL. Just like a query string, it is there to change the behaviour of a HTTP request/response based on parameter.

These are mostly implementation dependant but the general convention is to interpret them based on location.

Good simple example will be of tree.json

tree.1.json (1 is the depth selector) is just another way of saying tree.json?depth=1

similarly tree.2.json is kind of tree.json?depth=2

Semantically, this allows paths which are absolute without a query string and can be cached by dispatcher or CDN (as they don't have ? or # modifiers). Most proxies don't cache parameterised pages (? in the URL) but a selector works around the rule. Also, this allows the servlet to pre-define selector values (if required) and ignore unnecessary parameters.

In light of above, here is direct response to your questions (post edit):

Some of the documents say that it is used for caching response from a page which cannot be done when using query parameters.

Yes, that is true, specially if you put it in the context of reverse proxy like Dispatcher (recommended for publisher in a typical AEM deployment). By default, query string bypass and refresh the cache on a dispatcher. By using a selector, you are able to cache the response on a dispatcher as it does not initiate the cache flush. It is normally a good practice to use selectors if base version of data can be cached and selector depends on the same state.

While some suggest that Selectors are used to respond to different conditions using the same resource. For example we had an implementation where if the page(cq:page) is the last page in the hierarchy, it should display lets say a html-block while if its not the last page (i.e. if it has child pages), it should not display the said block. Here we used a script in the component and added this script name in the URL as selector based on the condition whether it is the last page or not.

Without looking at the code, I am assuming this was something like:

page.last-page.html

where last-page is the selector you used to differentiate the response. This is one way of doing it and as long as the page can be cached, this selector variant can also be cached without any issues. This is a good use of URL semantics and avoiding caching issues. If this was implemented as:

page.html?last-page=true

it may not have been cache friendly.

Sling has various ways of binding (resolving) a script or servlet to a URL. This resolution is done based on extension, selector(s) and/or path. You can add new functionality to an existing selector or extension by creating new selector based servlets without modifying existing servlets. Consider an example servlet which returns JSON data for a path (we know this exists OOTB in AEM but just an example).

content/mypage.json

returns JSON representation of the node.

Now, let's say you want a new JSON format which is tidier. Using tradition query string, you will do something like

content/mypage.json?tidy=true

This may not be possible if you don't have access to source code of original servlet. So the workaround would be:

content/mypage.tidy.json

This could be a new servlet which accepts the tidy selector and overrides the function. This makes selectors powerful when it comes to script resolution and code extensions. Yes you can do it with query parameters also but there would be limitations unless you own all the code.

Trinitytrinket answered 17/3, 2017 at 10:24 Comment(8)
So does this mean selectors have the same exact function as a query parameter (i.e. to pass parameters to a function). But the only difference is their usage around caching?Mcarthur
In a nutshell, yes. However, they are not a replacement for query string parameters. Your URL can still use query string parameters in presence of selectors. It enhances caching and provides (IMHO) a better semantic structure to the URL.Trinitytrinket
What about the point 2 in my question ? Is that not the main usage of Selectors?Mcarthur
It's one use case. It's not the main usage. AEM uses 'partial' selector to render partial HTML for controls (extJS). It is actually a good usage but there is no reason that it cannot be done with query parameters so you can't say that it's the main usage.Trinitytrinket
sorry if i am dragging it long. But how can this be done using queryParameters? As far as i know, Sling resolves the selector to a script itself without us having to do anything and this is not what it will do with query parameter.Mcarthur
A script (servlet) is resolved based on selector, extension and/or path. Query parameters are addition to that logic. The script resolution is completely different to how a servlet actually works on the parameters. If you want I can elaborate more on this in my answer.Trinitytrinket
I think I understand your point. To sum up your answer, the usage of selectors can be both (1) and (2) from my question. But since (2) can be achieved by even query parameter, its just a use case. Have a look at the following blog too just in case you might find it helpful: BLOG about selectors This keeps my confusion about selectors!Mcarthur
You are right in your understanding. I won't comment on the blog as that will just be my opinion against a blogger's opinion (nothing wrong with that) but I would rather stick to the extensive Sling documentation which you have already read and make your own use case/judgement for the project's need.Trinitytrinket

© 2022 - 2024 — McMap. All rights reserved.