refactor: improve page data parsing (#778)

pull/791/head
meteorlxy 2 years ago committed by GitHub
parent 6174362c53
commit 753f35bf7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -29,7 +29,7 @@ const getDefaultRoute = (): Route => ({
})
interface PageModule {
__pageData: string
__pageData: PageData
default: Component
}
@ -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(() => {

@ -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

@ -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 (

Loading…
Cancel
Save