Skip to content

Commit c49aefe

Browse files
elemdosclaude
andcommitted
fix: production build issues with assets and script escaping
- Add project_id to $tinykit.asset() URLs in production builds - Escape </script> in hydration JS to prevent HTML breakage 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent b36ef2f commit c49aefe

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/routes/tinykit/lib/api.svelte.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,14 +521,16 @@ function generate_production_html({ body, head = '', hydration_js, config }: Pro
521521
const data_url = `data:text/javascript,${encodeURIComponent(data_module)}`
522522

523523
// Generate $tk module (tinykit utilities)
524-
const tinykit_module = generate_tinykit_module()
524+
const tinykit_module = generate_tinykit_module(config.project_id || '')
525525
const tinykit_url = `data:text/javascript,${encodeURIComponent(tinykit_module)}`
526526

527527
// Build hydration script only if there's client JS
528+
// Escape </script> in the code to prevent breaking out of the script tag
529+
const escaped_js = hydration_js ? JSON.stringify(hydration_js).replace(/<\/script>/gi, '<\\/script>') : ''
528530
const hydration_script = hydration_js ? `
529531
<script type="module">
530532
// Hydrate the pre-rendered HTML with client-side interactivity
531-
const code = ${JSON.stringify(hydration_js)};
533+
const code = ${escaped_js};
532534
const blob = new Blob([code], { type: 'text/javascript' });
533535
const url = URL.createObjectURL(blob);
534536
@@ -874,14 +876,16 @@ export default db
874876
/**
875877
* Generate the $tk module code (tinykit utilities)
876878
*/
877-
function generate_tinykit_module(): string {
879+
function generate_tinykit_module(project_id: string): string {
878880
return `
881+
const PROJECT_ID = '${project_id}'
882+
879883
export function asset(filename, options) {
880884
if (!filename) return ''
881885
if (filename.startsWith('http://') || filename.startsWith('https://')) {
882886
return filename
883887
}
884-
let url = '/_tk/assets/' + filename
888+
let url = '/_tk/assets/' + PROJECT_ID + '/' + filename
885889
const params = []
886890
if (options?.thumb) params.push('thumb=' + options.thumb)
887891
if (options?.download) params.push('download=1')

0 commit comments

Comments
 (0)