Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

feat: make asyncData works with <script setup nuxt> - #220

Merged
pi0 merged 7 commits into
mainfrom
feat/async-data-setup
Jun 16, 2021
Merged

pi0 merged 7 commits into
mainfrom
feat/async-data-setup

Conversation

@antfu

@antfu antfu commented Jun 15, 2021

Copy link
Copy Markdown
Member
  • Make asyncData works with <script setup>
    • currently Vite only via a vite plugin, do we have a better way to write isomorphic transform for vite and webpack?
  • Proposal <script nuxt setup> syntax:
<script nuxt setup lang="ts">
import { asyncData } from '@nuxt/app'

const { data } = asyncData('time', () => $fetch('/api/count'))
</script>

<template>
  <div>
    Page visits: {{ data.count }}
  </div>
</template>

Just a quick and dirty support, just proposing this idea, feel free to close if you don't like it.

@antfu

antfu commented Jun 15, 2021

Copy link
Copy Markdown
Member Author

Ah, don't we have Draft PR for this repo?

@antfu antfu added the wip label Jun 15, 2021
@pi0

pi0 commented Jun 15, 2021

Copy link
Copy Markdown
Member

Awesome! I think we also need to implement webpack plugin before being ready to merge :)

Ah, don't we have Draft PR for this repo?

Private repos on gh free don't have it. This is only way to have unlimited contribs on private repo...

@antfu

antfu commented Jun 15, 2021

Copy link
Copy Markdown
Member Author

So we will need to write plugins for vite and webpack separately right? Do we have any plan to make a somewhat transform layer so we can share the logics?

@pi0

pi0 commented Jun 15, 2021 •

Copy link
Copy Markdown
Member

@antfu Yes we need both. In the future might make some utils (in unjs) to make it easier creating cross webpack-vite plugins

@antfu

antfu commented Jun 15, 2021

Copy link
Copy Markdown
Member Author

Should be done now. Do we need to add tests for it?

@danielroe

danielroe commented Jun 15, 2021 •

Copy link
Copy Markdown
Member

Tests would be great - maybe a page in the basic fixture?

I'm experiencing the following issue (on client-side) - reproducible with example project:

Error while mounting app: ReferenceError: $fetch is not defined

@antfu

antfu commented Jun 15, 2021

Copy link
Copy Markdown
Member Author

I'm experiencing the following issue (on client-side) - reproducible with example project

This is what nuxt/nuxt#11043 is talking about, and already been there before this PR 👀

@atinux

atinux commented Jun 16, 2021

Copy link
Copy Markdown
Member

Great work @antfu it looks really sexy to use 😍

I am thinking of exposing also $fetch from @nuxt/app to avoid any global issues or user having to set a tsconfig.json in order to have typings. What do you think @pi0 @danielroe ?

@pi0

pi0 commented Jun 16, 2021

Copy link
Copy Markdown
Member

@atinux It is already exposed from both (nuxt/bridge#120)

@atinux

atinux commented Jun 16, 2021

Copy link
Copy Markdown
Member

I was thinking of supporting:

import { asyncData, $fetch } from '@nuxt/app'

@pi0

pi0 commented Jun 16, 2021

Copy link
Copy Markdown
Member

@atinux Seems a good idea to have but let's first have useFetch composable to see how common it is directly using $fetch within components. Registering global types is probably necessary anyway that fixes type issue...

@pi0 pi0 changed the title feat: make asyncData works with <script setup> feat: make asyncData works with <script setup nuxt> Jun 16, 2021
@pi0
pi0 merged commit 11a5a3e into main Jun 16, 2021
@pi0
pi0 deleted the feat/async-data-setup branch June 16, 2021 12:43
pi0 added a commit that referenced this pull request Jun 16, 2021
@danielroe danielroe added the 3.x label Jan 19, 2023

This branch was successfully deployed

1 active deployment
Preview — ac51b07a Deployed Jun 16, 2021 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants