I'm running into an interesting issue with Google Apps Script HTML force-printing scriptlets (<?!= ... ?>) where a JSON object is being strangely truncated directly before the double forward slashes of a URL (//) on the client side.
My objective is to insert these JSON objects into the HTML code to be used for JS code in a <script> element, for them to be used by the JS as if the JSON object were hard-coded into the HTML file.
What can I do to ensure all of the data I want to send to the client via scriptlets is correctly received/not truncated? Why does the data get truncated?
Minimal Reproducible Example
Server-side GAS code:
function doGet() {
const template = HtmlService.createTemplateFromFile('fileName');
template.data = JSON.stringify({
"propertyWithSingleQuote": "T'Challa",
"url": "https://stackoverflow.com/users/23479790/brodblox09"
});
return template.evaluate();
}
Server-side HTML code:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<script>
const closingParagraphTag = `</p>`; // '
console.log(<?!= data ?>);
</script>
</body>
</html>
Received by client:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<script>
const closingParagraphTag = `</p>`; // '
console.log({"propertyWithSingleQuote":"T'Challa","url":"https:
</script>
</body>
</html>
Expected output:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<script>
const closingParagraphTag = `</p>`; // '
console.log({
"propertyWithSingleQuote": "T'Challa",
"url": "https://stackoverflow.com/users/23479790/brodblox09"
});
</script>
</body>
</html>
The issue appears directly linked to the server-side HTML code, and does not seem to be the fault of the server-side GAS code. If the entire const closingParagraphTag = `</p>`; // ' line is removed, the force-printing scriptlet works just fine. If just the // ' comment is removed, the force-printing scriptlet works just fine. If just the `</p>` part of the line is removed/modified, e.g. changed to '</p>' or "</p>", the force-printing scriptlet also works just fine.
Utilities.base64Encode()in GAS andatob()in HTML so that this bug-seeming issue with GAS scriptlets can be circumvented, but I would still like a more appropriate/industry-correct solution to this if it exists."time"property from the example JSON object as that is not required to demonstrate the issue. Please let me know if there is anything else I can do to make my question clearer, and thank you for your support thus far.