Skip to content

Conversation

stafyniaksacha
Copy link
Contributor

@stafyniaksacha stafyniaksacha commented Oct 10, 2023

resolves #12406

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This adds new option to control output of useAsyncData and useFetch data in order to have ShallowRef instead of Ref

It also introduces a new experimental flag asyncDataShallow to change the default behavior to use shallow by default

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@bolt-new-by-stackblitz
Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@nuxt-studio
Copy link
Contributor

nuxt-studio bot commented Oct 10, 2023

βœ… Live Preview ready!

Name Edit Preview Latest Commit
Nuxt Docs Edit on Studio β†—οΈŽ View Live Preview 0f8ca26

@danielroe
Copy link
Member

What do you think about doing something like { default: shallowRef }?

@stafyniaksacha
Copy link
Contributor Author

What do you think about doing something like { default: shallowRef }?

I think it's less clear about our intentions, default should be keep for default data, here it's more like ref(shallowRef(...))

data: ref(getCachedData() ?? options.default!()),
data: options.shallow
? shallowRef(getCachedData() ?? options.default!())
: ref(getCachedData() ?? options.default!()),
pending: ref(!hasCachedData()),
error: toRef(nuxt.payload._errors, key),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could probably always return error in a shallowRef

Copy link
Contributor Author

@stafyniaksacha stafyniaksacha Oct 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a toShallowRef here, but yes it worth it here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could create a local util until/if it's available in core

Co-authored-by: Julien Huang <julien.h.dev@gmail.com>
Co-authored-by: Daniel Roe <daniel@roe.dev>
name: 'nuxt:async-data-shallow',
enforce: 'pre',
setup (nuxtApp) {
nuxtApp._asyncDataShallow = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use nuxt.config.mjs instead?

export const nuxtConfigTemplate = {
filename: 'nuxt.config.mjs',
getContents: (ctx: TemplateContext) => {
return [
...Object.entries(ctx.nuxt.options.app).map(([k, v]) => `export const ${camelCase('app-' + k)} = ${JSON.stringify(v)}`),
`export const renderJsonPayloads = ${!!ctx.nuxt.options.experimental.renderJsonPayloads}`,
`export const componentIslands = ${!!ctx.nuxt.options.experimental.componentIslands}`,
`export const payloadExtraction = ${!!ctx.nuxt.options.experimental.payloadExtraction}`,
`export const appManifest = ${!!ctx.nuxt.options.experimental.appManifest}`,
`export const remoteComponentIslands = ${ctx.nuxt.options.experimental.componentIslands === 'local+remote'}`,
`export const devPagesDir = ${ctx.nuxt.options.dev ? JSON.stringify(ctx.nuxt.options.dir.pages) : 'null'}`,
`export const devRootDir = ${ctx.nuxt.options.dev ? JSON.stringify(ctx.nuxt.options.rootDir) : 'null'}`,
`export const vueAppRootContainer = ${ctx.nuxt.options.app.rootId ? `'#${ctx.nuxt.options.app.rootId}'` : `'body > ${ctx.nuxt.options.app.rootTag}'`}`
].join('\n\n')
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's defer this to another PR

@danielroe danielroe changed the title feat: add shallow option to useAsyncData and useFetch feat(nuxt): support deep: false for data composables Oct 16, 2023
@danielroe danielroe requested review from antfu and lihbr October 16, 2023 11:47
@danielroe danielroe added this to the 3.8 milestone Oct 16, 2023
@danielroe danielroe added the 3.x label Oct 16, 2023
@danielroe danielroe merged commit 830f4f4 into nuxt:main Oct 16, 2023
@github-actions github-actions bot mentioned this pull request Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AsyncData with a shallow ref
3 participants