Skip to content

Commit

Permalink
refactor: improve page data parsing (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Jun 14, 2022
1 parent 6174362 commit 753f35b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/client/app/router.ts
Expand Up @@ -29,7 +29,7 @@ const getDefaultRoute = (): Route => ({
})

interface PageModule {
__pageData: string
__pageData: PageData
default: Component
}

Expand Down Expand Up @@ -77,8 +77,8 @@ export function createRouter(
route.path = inBrowser ? pendingPath : withBase(pendingPath)
route.component = markRaw(comp)
route.data = import.meta.env.PROD
? markRaw(JSON.parse(__pageData))
: (readonly(JSON.parse(__pageData)) as PageData)
? markRaw(__pageData)
: (readonly(__pageData) as PageData)

if (inBrowser) {
nextTick(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/node/build/render.ts
Expand Up @@ -60,7 +60,7 @@ export async function renderPage(
const { __pageData } = await import(
pathToFileURL(path.join(config.tempDir, pageServerJsFileName)).toString()
)
pageData = JSON.parse(__pageData)
pageData = __pageData
} catch (e) {
if (page === '404.md') {
hasCustom404 = false
Expand Down
4 changes: 2 additions & 2 deletions src/node/markdownToVue.ts
Expand Up @@ -169,9 +169,9 @@ const defaultExportRE = /((?:^|\n|;)\s*)export(\s*)default/
const namedDefaultExportRE = /((?:^|\n|;)\s*)export(.+)as(\s*)default/

function genPageDataCode(tags: string[], data: PageData) {
const code = `\nexport const __pageData = ${JSON.stringify(
const code = `\nexport const __pageData = JSON.parse(${JSON.stringify(
JSON.stringify(data)
)}`
)})`

const existingScriptIndex = tags.findIndex((tag) => {
return (
Expand Down

0 comments on commit 753f35b

Please sign in to comment.