Skip to content

Commit

Permalink
server-islands: only encode ETAGO delimiter & opening HTML comment
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtextrem committed Jul 20, 2024
1 parent a6c4e67 commit 87aa1f6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-socks-end.md
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 11 additions & 5 deletions packages/astro/src/runtime/server/render/server-islands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 87aa1f6

Please sign in to comment.