Skip to content

Commit

Permalink
fix: Script with innerHTML not working on Safari (#4861)
Browse files Browse the repository at this point in the history
* fix: Script with innerHTML not working on Safari

* Update cool-camels-tease.md
  • Loading branch information
rishi-raj-jain committed Sep 26, 2022
1 parent 83ed1cc commit 42fe8e0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-camels-tease.md
@@ -0,0 +1,5 @@
---
'astro': patch
---

use const instead of let for define:vars
4 changes: 3 additions & 1 deletion packages/astro/src/runtime/server/render/util.ts
Expand Up @@ -34,7 +34,9 @@ const toStyleString = (obj: Record<string, any>) =>
export function defineScriptVars(vars: Record<any, any>) {
let output = '';
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);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/test/astro-directives.test.js
Expand Up @@ -23,10 +23,10 @@ describe('Directives', async () => {
expect($(script).text().at(-1)).to.equal('}');
if (i < 2) {
// Inline defined variables
expect($(script).toString()).to.include('let foo = "bar"');
expect($(script).toString()).to.include('const foo = "bar"');
} else {
// Convert invalid keys to valid identifiers
expect($(script).toString()).to.include('let dashCase = "bar"');
expect($(script).toString()).to.include('const dashCase = "bar"');
}
i++;
}
Expand Down

0 comments on commit 42fe8e0

Please sign in to comment.