Skip to content

Commit 753f35b

Browse files
authored
refactor: improve page data parsing (#778)
1 parent 6174362 commit 753f35b

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

src/client/app/router.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const getDefaultRoute = (): Route => ({
2929
})
3030

3131
interface PageModule {
32-
__pageData: string
32+
__pageData: PageData
3333
default: Component
3434
}
3535

@@ -77,8 +77,8 @@ export function createRouter(
7777
route.path = inBrowser ? pendingPath : withBase(pendingPath)
7878
route.component = markRaw(comp)
7979
route.data = import.meta.env.PROD
80-
? markRaw(JSON.parse(__pageData))
81-
: (readonly(JSON.parse(__pageData)) as PageData)
80+
? markRaw(__pageData)
81+
: (readonly(__pageData) as PageData)
8282

8383
if (inBrowser) {
8484
nextTick(() => {

src/node/build/render.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export async function renderPage(
6060
const { __pageData } = await import(
6161
pathToFileURL(path.join(config.tempDir, pageServerJsFileName)).toString()
6262
)
63-
pageData = JSON.parse(__pageData)
63+
pageData = __pageData
6464
} catch (e) {
6565
if (page === '404.md') {
6666
hasCustom404 = false

src/node/markdownToVue.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ const defaultExportRE = /((?:^|\n|;)\s*)export(\s*)default/
169169
const namedDefaultExportRE = /((?:^|\n|;)\s*)export(.+)as(\s*)default/
170170

171171
function genPageDataCode(tags: string[], data: PageData) {
172-
const code = `\nexport const __pageData = ${JSON.stringify(
172+
const code = `\nexport const __pageData = JSON.parse(${JSON.stringify(
173173
JSON.stringify(data)
174-
)}`
174+
)})`
175175

176176
const existingScriptIndex = tags.findIndex((tag) => {
177177
return (

0 commit comments

Comments
 (0)