Skip to content

Commit

Permalink
fix: sanitise ssrRefs
Browse files Browse the repository at this point in the history
should solve warning: Cannot stringify POJOs with symbolic keys
  • Loading branch information
danielroe committed May 26, 2020
1 parent ac15e98 commit fcb4a9d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
6 changes: 4 additions & 2 deletions src/ssr-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export function setSSRContext(ssrContext: any) {
const isProxyable = (val: unknown): val is Record<string, unknown> =>
val && typeof val === 'object'

const sanitise = (val: unknown) => val
const sanitise = (val: unknown) =>
(val && JSON.parse(JSON.stringify(val))) || val

/**
* `ssrRef` will automatically add ref values to `window.__NUXT__` on SSR if they have been changed from their initial value. It can be used outside of components, such as in shared utility functions, and it supports passing a factory function that will generate the initial value of the ref. **At the moment, an `ssrRef` is only suitable for one-offs, unless you provide your own unique key.**
Expand Down Expand Up @@ -60,8 +61,9 @@ export const ssrRef = <T>(value: T | (() => T), key?: string): Ref<T> => {
return Reflect.get(target, prop)
},
set(obj, prop, val) {
const result = Reflect.set(obj, prop, val)
data[key] = sanitise(_ref.value)
return Reflect.set(obj, prop, val)
return result
},
})

Expand Down
12 changes: 8 additions & 4 deletions test/fixture/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// eslint-disable-next-line
const { resolve } = require('path')

const routes = ['/context/a']

module.exports = {
rootDir: resolve(__dirname, '../..'),
buildDir: resolve(__dirname, '.nuxt'),
Expand All @@ -16,9 +18,7 @@ module.exports = {
? {
generate: {
dir: 'dist/fixture',
routes: [
'/context/a'
]
routes
},
router: {
base: '/fixture/',
Expand All @@ -27,7 +27,11 @@ module.exports = {
publicPath: 'fixture',
},
}
: {}),
: {
generate: {
routes,
}
}),
buildModules: [
process.env.NODE_ENV === 'test'
? require('../..').default
Expand Down
13 changes: 4 additions & 9 deletions test/fixture/pages/ssr-ref.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ export function fetcher(result, time = 100) {
export default defineComponent({
setup() {
const refValue = ssrRef('') // changed => in __NUXT__
const prefetchValue = ssrRef('') // changed => in __NUXT__
const funcValue = ssrRef(() => 'runs SSR or client-side') // function => in __NUXT__
const noChange = ssrRef('initValue') // no Change => not in __NUXT__
const refValue = ssrRef('')
const prefetchValue = ssrRef('')
const funcValue = ssrRef(() => 'runs SSR or client-side')
const noChange = ssrRef('initValue')
const shallow = shallowSsrRef({ v: { deep: 'init' } })
if (process.server) shallow.value = { v: { deep: 'server' } }
Expand All @@ -67,11 +67,6 @@ export default defineComponent({
fetcher(process.server ? 'server' : 'client', 500)
)
// Error handeling
// useAsync(() => {
// throw '42'
// })
return {
computedVal,
funcValue,
Expand Down

0 comments on commit fcb4a9d

Please sign in to comment.