Skip to content

Commit

Permalink
fix(nuxt): use destr in more places over JSON.parse (#22997)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Sep 5, 2023
1 parent b27740c commit 1a08079
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/nuxt/src/app/components/test-component-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { parseURL } from 'ufo'
import { defineComponent, h } from 'vue'
import { parseQuery } from 'vue-router'
import { resolve } from 'pathe'
import destr from 'destr'
// @ts-expect-error virtual file
import { devRootDir } from '#build/nuxt.config.mjs'

Expand All @@ -10,7 +11,7 @@ export default (url: string) => defineComponent({

async setup (props, { attrs }) {
const query = parseQuery(parseURL(url).search)
const urlProps = query.props ? JSON.parse(query.props as string) : {}
const urlProps = query.props ? destr<Record<string, any>>(query.props as string) : {}
const path = resolve(query.path as string)
if (!path.startsWith(devRootDir)) {
throw new Error(`[nuxt] Cannot access path outside of project root directory: \`${path}\`.`)
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxt/src/app/composables/chunk.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import destr from 'destr'
import { useNuxtApp } from '#app/nuxt'

export interface ReloadNuxtAppOptions {
Expand Down Expand Up @@ -34,7 +35,7 @@ export function reloadNuxtApp (options: ReloadNuxtAppOptions = {}) {

let handledPath: Record<string, any> = {}
try {
handledPath = JSON.parse(sessionStorage.getItem('nuxt:reload') || '{}')
handledPath = destr(sessionStorage.getItem('nuxt:reload') || '{}')
} catch {}

if (options.force || handledPath?.path !== path || handledPath?.expires < Date.now()) {
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxt/src/app/plugins/restore-state.client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import destr from 'destr'
import { defineNuxtPlugin, useNuxtApp } from '#app/nuxt'

export default defineNuxtPlugin({
Expand All @@ -9,7 +10,7 @@ export default defineNuxtPlugin({
const state = sessionStorage.getItem('nuxt:reload:state')
if (state) {
sessionStorage.removeItem('nuxt:reload:state')
Object.assign(nuxtApp.payload.state, JSON.parse(state)?.state)
Object.assign(nuxtApp.payload.state, destr<Record<string, any>>(state)?.state)
}
} catch {}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/nuxt/src/app/plugins/revive-payload.client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { reactive, ref, shallowReactive, shallowRef } from 'vue'
import destr from 'destr'
import { definePayloadReviver, getNuxtClientPayload } from '#app/composables/payload'
import { createError } from '#app/composables/error'
import { defineNuxtPlugin, useNuxtApp } from '#app/nuxt'
Expand All @@ -8,8 +9,8 @@ import { componentIslands } from '#build/nuxt.config.mjs'

const revivers: Record<string, (data: any) => any> = {
NuxtError: data => createError(data),
EmptyShallowRef: data => shallowRef(data === '_' ? undefined : data === '0n' ? BigInt(0) : JSON.parse(data)),
EmptyRef: data => ref(data === '_' ? undefined : data === '0n' ? BigInt(0) : JSON.parse(data)),
EmptyShallowRef: data => shallowRef(data === '_' ? undefined : data === '0n' ? BigInt(0) : destr(data)),
EmptyRef: data => ref(data === '_' ? undefined : data === '0n' ? BigInt(0) : destr(data)),
ShallowRef: data => shallowRef(data),
ShallowReactive: data => shallowReactive(data),
Ref: data => ref(data),
Expand Down

0 comments on commit 1a08079

Please sign in to comment.