Context
You can launch an ASCF atomic service in different ways and pass custom parameters. For details, see Launching an ASCF Atomic Service with Parameters.
ascfPara is a built-in object provided by ASCF for receiving external parameters passed to the ASCF atomic service. ascfPara.path specifies the custom path of the page to be redirected to. When the ASCF atomic service is launched, the home page is displayed first and then the specified page is opened.
When configuring ascfPara.path, you can append custom parameters using ?, with multiple parameters separated by &. These custom parameters can then be obtained in the onLoad callback of the page.
Symptom
A developer creates a page with route pages/h5/index to load different HTML5 links. These HTML5 links are passed through custom parameters, and the page route is separated from custom parameters by ?. If the value of the custom parameter is also a URL containing ?, the content following ? in the URL will be missing.
For example, if you configure custom parameter ascfPara={"path": "pages/h5/index?url=https://www.example.com?param=value¶m1=value1"}, only url=https://www.example.com can be obtained from the URL in the onLoad callback of the pages/h5/index page, and param and param1 cannot be obtained.
Cause
The custom parameter url is an HTML5 link that carries its own query arguments (with ?). When the URL is embedded as a value into the outer path, two ? characters appear in the path string. ASCF extracts the query string using path.split('?')[1]. This logic can only obtain the content following the first ? and cannot distinguish the boundary between the outer parameters and the embedded URL's own parameters. As a result, the query parameters of the embedded URL are truncated or incorrectly parsed, leading to parameter loss.
Solution
When configuring ascfPara.path, it is not recommended that the parameter values contain key characters such as ? and &, as these will cause incorrect parameter truncation.
If these characters are required, you must first encode the parameter value using encodeURIComponent and then encode the entire ascfPara value using encodeURIComponent to produce a double-encoded string. ASCF decodes the entire ascfPara value only once when processing parameters, so the nested parameters can be parsed correctly.
Example
Encode the url value https://www.example.com?param=value¶m1=value1 using encodeURIComponent to obtain https%3A%2F%2Fwww.example.com%3Fparam%3Dvalue%26param1%3Dvalue1.
Encode the ascfPara.path value using encodeURIComponent to obtain pages%2Fh5%2Findex%3Furl%3Dhttps%253A%252F%252Fwww.example.com%253Fparam%253Dvalue%2526param1%253Dvalue1.
Retrieve parameters in onLoad.
// pages/h5/index.js
Page({
onLoad(options = {}) {
let url = options.url;
if (url) {
url = decodeURIComponent(url);
// https://www.example.com?param=value¶m1=value1
console.info('url', url);
}
},
})