fix: Script with innerHTML not working on Safari (#4861)

* fix: Script with innerHTML not working on Safari

* Update cool-camels-tease.md
This commit is contained in:
Rishi Raj Jain 2022-09-26 23:33:53 +05:30 committed by GitHub
parent 83ed1cc1f2
commit 42fe8e0c7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
use const instead of let for define:vars

View file

@ -34,7 +34,9 @@ const toStyleString = (obj: Record<string, any>) =>
export function defineScriptVars(vars: Record<any, any>) { export function defineScriptVars(vars: Record<any, any>) {
let output = ''; let output = '';
for (const [key, value] of Object.entries(vars)) { for (const [key, value] of Object.entries(vars)) {
output += `let ${toIdent(key)} = ${JSON.stringify(value)};\n`; // Use const instead of let as let global unsupported with Safari
// https://stackoverflow.com/questions/29194024/cant-use-let-keyword-in-safari-javascript
output += `const ${toIdent(key)} = ${JSON.stringify(value)};\n`;
} }
return markHTMLString(output); return markHTMLString(output);
} }

View file

@ -23,10 +23,10 @@ describe('Directives', async () => {
expect($(script).text().at(-1)).to.equal('}'); expect($(script).text().at(-1)).to.equal('}');
if (i < 2) { if (i < 2) {
// Inline defined variables // Inline defined variables
expect($(script).toString()).to.include('let foo = "bar"'); expect($(script).toString()).to.include('const foo = "bar"');
} else { } else {
// Convert invalid keys to valid identifiers // Convert invalid keys to valid identifiers
expect($(script).toString()).to.include('let dashCase = "bar"'); expect($(script).toString()).to.include('const dashCase = "bar"');
} }
i++; i++;
} }