Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve page data parsing #778

Merged
merged 1 commit into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/client/app/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const getDefaultRoute = (): Route => ({
})

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

Expand Down Expand Up @@ -76,8 +76,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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(
brc-dd marked this conversation as resolved.
Show resolved Hide resolved
JSON.stringify(data)
)}`
)})`

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