Skip to content

Commit

Permalink
refactor: use application/json script instead of global this
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Jan 2, 2025
1 parent cd59e99 commit 8f83a66
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions preview/stores/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ export let data = readable<Partial<Data>>({}, set => {
let fetchData = async (): Promise<void> => {
let dataValue: Data
if (import.meta.env.MODE === 'production') {
// @ts-ignore
dataValue = globalThis.data as Data
dataValue = JSON.parse(
document.querySelector('#data')?.textContent ?? '{}',
) as Data
} else {
let dataResponse = await fetch('/data.json')
dataValue = (await dataResponse.json()) as Data
Expand Down
2 changes: 1 addition & 1 deletion src/project/generate_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub async fn generate_output(

if let Some(pos) = index_content.find("</head>") {
let script_tag = format!(
"<script>window.data = {};</script>",
"<script id=\"data\" type=\"application/json\">{};</script>",
escaped_json_string
);
index_content.insert_str(pos, &script_tag);
Expand Down
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default defineConfig({
...(isDocumentation
? [
injectBeforeHead(
`<script>window.data = ${JSON.stringify(data)};</script>`,
`<script id="data" type="application/json">${JSON.stringify(data)}</script>`,
),
injectBeforeHead(
'<script defer src="https://analytics.azat.io/script.js" data-website-id="43d46bcc-915b-46c0-92b4-9e290eb8a5dc"></script></head>',
Expand Down

0 comments on commit 8f83a66

Please sign in to comment.