0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-06 22:10:10 -05:00

server-islands: only encode ETAGO delimiter & opening HTML comment

This commit is contained in:
Jacob Groß 2024-07-20 11:13:54 +02:00
parent a6c4e67544
commit 87aa1f633d
2 changed files with 16 additions and 5 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Only escape the script tag ETAGO delimiter and opening HTML comment syntax in server islands to reduce encoding work.

View file

@ -14,13 +14,19 @@ export function containsServerDirective(props: Record<string | number, any>) {
return 'server:component-directive' in props;
}
const scriptRegex = /<\/script/giu;
const commentRegex = /<!--/gu;
const scriptReplacer = '<\\/script';
const commentReplacer = '\\u003C!--';
/**
* Encodes the script end-tag open (ETAGO) delimiter and opening HTML comment syntax for JSON inside a `<script>` tag.
* @see https://mathiasbynens.be/notes/etago
*/
function safeJsonStringify(obj: any) {
return JSON.stringify(obj)
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029')
.replace(/</g, '\\u003c')
.replace(/>/g, '\\u003e')
.replace(/\//g, '\\u002f');
.replace(scriptRegex, scriptReplacer)
.replace(commentRegex, commentReplacer);
}
export function renderServerIsland(