Dynamically insert content into anchor href on click#9086
Dynamically insert content into anchor href on click#9086jridgewell merged 6 commits intoampproject:masterfrom
Conversation
src/service/url-replacements-impl.js
Outdated
| * @param {!Element} element An anchor element. | ||
| * @return {string} url | ||
| * @private | ||
| */ |
There was a problem hiding this comment.
We can avoid this entirely by passing in a map as opt_bindings in the #expandSync call above.
There was a problem hiding this comment.
As discussed with you on slack opt_bindings can be used for the replace use case only.
src/service/url-replacements-impl.js
Outdated
| * @return {string} url | ||
| * @private | ||
| */ | ||
| extendURLParams_(url, element) { |
There was a problem hiding this comment.
We already have data-vars-* for <amp-analytics>, maybe use the same?
There was a problem hiding this comment.
- I can make it as
data-vars-urlparamfromdata-amp-addparambut it wont align with naming ofdata-amp-replace. Is that fine ? - Also
data-amp-addparamcan have token values likec=CLIENT_ID(abc)which should be replaced by entries provided ondata-amp-replacelike
a.setAttribute('data-amp-replace', 'QUERY_PARAM CLIENT_ID');
a.setAttribute('data-amp-addparams', 'guid=123 c=CLIENT_ID(abc)');
In this if we have a different data attribute name like data-vars-* I guess it might create confusion . Please let me know your suggestion on this
src/service/url-replacements-impl.js
Outdated
| * @return {string} url | ||
| * @private | ||
| */ | ||
| extendURLParams_(url, element) { |
src/service/url-replacements-impl.js
Outdated
| const additionalURLParamaters = element.getAttribute('data-amp-addparams'); | ||
| if (!additionalURLParamaters) { | ||
| return url; | ||
| } |
There was a problem hiding this comment.
We'll need this to use addParamsToUrl, but unfortunately we'll need to do some parsing.
// This should probably live in src/url.js
function spaceDelimitedParamsToMap(params) {
return parseQueryString(params.replace(/\s+/, '&'));
}
// Here, in this code
return addParamsToUrl(url, spaceDelimitedParamsToMap(additionalURLParamaters));There was a problem hiding this comment.
Added spaceDelimitedParamsToMap to url.js and used addParamsToUrl. Now because of the url.js usage it got simplified
jridgewell
left a comment
There was a problem hiding this comment.
Code wise looks fine (minus the types), @rudygalfi would you like to review?
src/url.js
Outdated
| /** | ||
| * Covert space delimited parameters to Map | ||
| * @param {string} url | ||
| * @return {string} |
There was a problem hiding this comment.
This return is incorrect. {!Object<string>}
There was a problem hiding this comment.
My bad. Missed the JS doc. Checked in these 2 fixes
src/url.js
Outdated
|
|
||
| /** | ||
| * Covert space delimited parameters to Map | ||
| * @param {string} url |
There was a problem hiding this comment.
This should params, not url
|
Is this the correct place to update documentation |
|
@jridgewell https://github.com/ampproject/amphtml/blob/d4bb91f952f8e52fe43ae4c1431933b4fdfde575/spec/amp-var-substitutions.md page has details about substitution. Should we have separate Readme.md file for the addition . Also please review the updated documentation at https://github.com/rajkumarsrk/amphtml/blob/b140953db8d289aab8aa8d14141d683a76332208/spec/amp-managing-user-state.md#task-5-using-client-id-in-linking-and-form-submission and let me know your feedback |
|
These questions are better directed at @bpaduch. 😄 |
| <a href="https://example.com/step2.html?ref_id=CLIENT_ID(uid)" data-amp-replace="CLIENT_ID"> | ||
| ``` | ||
|
|
||
| **Alternative solution for passing Client ID to the outgoing links:** Define the new query parameter `ref_id` as part of the data attribute `data-amp-addparams` and for queries that needs parameter substitution provide those details as part of `data-amp-replace`. With this approach the URL would look clean and the parameters specified on `data-amp-addparams` will be dynamically added |
There was a problem hiding this comment.
@rudygalfi - As you own this doc and are more familiar with the content, can you review that this is accurate and the correct location for it?
|
|
I thought |
|
Updated the |
bb0e735 to
19558fc
Compare
|
Looks like you have to rebase, then we can merge. |
spec/amp-var-substitutions.md
Outdated
| <a href="https://example.com?client_id=CLIENT_ID(bar)&abc=QUERY_PARAM(abc)" data-amp-replace="CLIENT_ID,QUERY_PARAM">Go to my site</a> | ||
| ``` | ||
|
|
||
| Variable substitution works well with dynamic parameters added to link href through `data-amp-addparams`. Parameters provided as part of `data-amp-addparams` that requires variable substitution has to be specified on `data-amp-replace` like |
There was a problem hiding this comment.
I'd like to suggest you:
- Add a level 4 subheading (
#### Appending parameters to the href). - Rewrite the information as follows (of course, assuming I'm technically correct ;) ):
If you need to append dynamic parameters to the href, specify the parameters by using the data-amp-addparams attribute. Any substitution parameters that you specify in data-amp-addparams must also be specified in data-amp-replace, as in the following example:
...
f3ebc14 to
54aeb48
Compare
|
@jridgewell The |
|
Github says it still needs a rebase off current master.
Commas made sense for |
c2eb374 to
8dcddbd
Compare
|
Ugh, unrelated docs failures. @erwinmombay, mind merging? |
|
After rebasing to the latest changes PR failure got fixed |
|
Thanks for the help @rajkumarsrk! |
|
Thanks @jridgewell. Can you let me know when we can expect this change on production ? |
|
It'll be 2 weeks, though you'll be able to test using canary next week. |
Add support to dynamically add URL parameters to the link URL
data-amp-replaceworks where the values are&separateddata-amp-addparamshas the same set of rules as that ofdata-amp-replacedata-amp-addparamsand that need to be replaced should have entry ondata-amp-replaceIf this changes looks good I will update the Readme.md files as well
Fixes #8594