Skip to content

Commit 8f83a66

Browse files
committed
refactor: use application/json script instead of global this
1 parent cd59e99 commit 8f83a66

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

preview/stores/data.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ export let data = readable<Partial<Data>>({}, set => {
3636
let fetchData = async (): Promise<void> => {
3737
let dataValue: Data
3838
if (import.meta.env.MODE === 'production') {
39-
// @ts-ignore
40-
dataValue = globalThis.data as Data
39+
dataValue = JSON.parse(
40+
document.querySelector('#data')?.textContent ?? '{}',
41+
) as Data
4142
} else {
4243
let dataResponse = await fetch('/data.json')
4344
dataValue = (await dataResponse.json()) as Data

src/project/generate_output.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub async fn generate_output(
3939

4040
if let Some(pos) = index_content.find("</head>") {
4141
let script_tag = format!(
42-
"<script>window.data = {};</script>",
42+
"<script id=\"data\" type=\"application/json\">{};</script>",
4343
escaped_json_string
4444
);
4545
index_content.insert_str(pos, &script_tag);

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export default defineConfig({
6767
...(isDocumentation
6868
? [
6969
injectBeforeHead(
70-
`<script>window.data = ${JSON.stringify(data)};</script>`,
70+
`<script id="data" type="application/json">${JSON.stringify(data)}</script>`,
7171
),
7272
injectBeforeHead(
7373
'<script defer src="https://analytics.azat.io/script.js" data-website-id="43d46bcc-915b-46c0-92b4-9e290eb8a5dc"></script></head>',

0 commit comments

Comments
 (0)