Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nuxtjs Support #636

Closed
pipinet opened this issue Jul 24, 2021 · 80 comments
Closed

Nuxtjs Support #636

pipinet opened this issue Jul 24, 2021 · 80 comments
Labels
feature request New feature or request

Comments

@pipinet
Copy link

pipinet commented Jul 24, 2021

This function solves the problem (这个功能解决的问题)

Nuxtjs Support

Expected API (期望的 API)

Nuxtjs Support

@github-actions github-actions bot added the feature request New feature or request label Jul 24, 2021
@pipinet pipinet mentioned this issue Jul 24, 2021
@07akioni
Copy link
Collaborator

07akioni commented Jul 24, 2021

Nuxt support is put in official docs now

https://www.naiveui.com/en-US/os-theme/docs/ssr

@wenzheng-yi
Copy link
Contributor

据我所知,Nuxtjs目前是基于vue2的,而vue3驱动的Nuxt3还在开发中,所以,同样基于vue3的naive-ui很难支持当下的Nuxtjs。

As I know, Nuxtjs is based on vue2, and the Nuxt3 powered with vue3 still in heavy development. So, navie-ui that is also based on vue3 is hard to surpport Nuxtjs now.

@07akioni
Copy link
Collaborator

据我所知,Nuxtjs目前是基于vue2的,而vue3驱动的Nuxt3还在开发中,所以,同样基于vue3的naive-ui很难支持当下的Nuxtjs。

As I know, Nuxtjs is based on vue2, and the Nuxt3 powered with vue3 still in heavy development. So, navie-ui that is also based on vue3 is hard to surpport Nuxtjs now.

我不熟 NUXT,不过估计主要的难点在于 css-in-js 导致的 SSR 问题,其他没啥道理不 work。

@pipinet
Copy link
Author

pipinet commented Oct 4, 2021

https://nuxtjs.org/v3

@mndaipre
Copy link

https://v3.nuxtjs.org/ is in beta now

@07akioni
Copy link
Collaborator

If someone has tried it, please tell us what errors you met.

Actually I've never used nuxt and don't know what's required to make it work.

@migueldaipre
Copy link

If someone has tried it, please tell us what errors you met.

Actually I've never used nuxt and don't know what's required to make it work.

I did the default installation steps for nuxt (https://v3.nuxtjs.org/getting-started/installation) and for naive-ui (https://www.naiveui.com/en-US/os- theme/docs/installation) and I'm getting the error "Server Side Rendering Error: Error: Unexpected token 'export'", follow StackBlitz link to help debug https://stackblitz.com/edit/github-ovqtjv?devtoolsheight= 33&file=app.vue. thanks

@07akioni
Copy link
Collaborator

That's quite wired since we do provide a cjs build version in node_modules/naive-ui/lib/

@oceanlvr
Copy link

I'm interested in getting involved in the job, but I'm new if I can get advice on where to start.

@07akioni
Copy link
Collaborator

07akioni commented Oct 14, 2021

I'm interested in getting involved in the job, but I'm new if I can get advice on where to start.

Just try to make a button rendered in nuxt. The most possible barrier could be css-render integration. See SSR section in naive-ui's site for more information if you meet the related issues.

@pipinet
Copy link
Author

pipinet commented Oct 17, 2021

i use nuxtjs v3.0 integrate naive-ui, it looks work.
image
but got some error in console
image

nuxt.config.ts

import {defineNuxtConfig} from 'nuxt3'

export default defineNuxtConfig({
  alias: {},
  modules: [],
  build: {
    transpile: ['naive-ui', 'seemly', 'css-render', 'emotion', 'vooks', 'evtd', 'date-fns'],
  },
  extensions: [],
  app: {
    basePath: '/',
    assetsPath: '/assets',
    cdnURL: null
  },
  srcDir: 'src',
  ssr: true,
  privateRuntimeConfig: {},
  publicRuntimeConfig: {}
})

plugins

import naive from "naive-ui";

export default defineNuxtPlugin((nuxt) => {
    nuxt.app.use(naive)
})

@07akioni
Copy link
Collaborator

There's some errors in ssr so it may fallback to CSR.

@pipinet
Copy link
Author

pipinet commented Oct 17, 2021

@07akioni it means naive-ui or nuxtjs issue?

@07akioni
Copy link
Collaborator

07akioni commented Oct 17, 2021

@07akioni it means naive-ui or nuxtjs issue?

Need to do something to make SSR in nuxt work.

@wobsoriano
Copy link
Contributor

wobsoriano commented Oct 18, 2021

To use naive-ui in nuxt 3 you can temporarily turn off vite in your nuxt.config.ts

import { defineNuxtConfig } from 'nuxt3'

export default defineNuxtConfig({
    vite: false
})

Dev and build works with this.

@pipinet
Copy link
Author

pipinet commented Oct 18, 2021

@wobsoriano it's not a good suggestion.

@wobsoriano
Copy link
Contributor

wobsoriano commented Oct 18, 2021

@wobsoriano it's not a good suggestion.

"temporarily"

nuxt/nuxt#12050

  1. Adding naive-ui to build.transpile - Doesn't work
  2. Adding naive-ui to vite.ssr.noExternal - Doesn't work
  3. Setting vite: false - Works (Uses webpack 5 instead of vite)

This is also my workaround for nuxt3-supabase https://github.com/wobsoriano/nuxt3-supabase#getting-started

@AshenBird
Copy link

@wobsoriano it's not a good suggestion.

"temporarily"

nuxt/nuxt.js#12050 (comment)

  1. Adding naive-ui to build.transpile - Doesn't work
  2. Adding naive-ui to vite.ssr.noExternal - Doesn't work
  3. Setting vite: false - Works (Uses webpack 5 instead of vite)

This is also my workaround for nuxt3-supabase https://github.com/wobsoriano/nuxt3-supabase#getting-started

this is right. nuxt3 replace global with globalThis , but also replace it in path what be uesed import argument. this is nuxt3's matter.

@AshenBird
Copy link

i use nuxtjs v3.0 integrate naive-ui, it looks work. image but got some error in console image

nuxt.config.ts

import {defineNuxtConfig} from 'nuxt3'

export default defineNuxtConfig({
  alias: {},
  modules: [],
  build: {
    transpile: ['naive-ui', 'seemly', 'css-render', 'emotion', 'vooks', 'evtd', 'date-fns'],
  },
  extensions: [],
  app: {
    basePath: '/',
    assetsPath: '/assets',
    cdnURL: null
  },
  srcDir: 'src',
  ssr: true,
  privateRuntimeConfig: {},
  publicRuntimeConfig: {}
})

plugins

import naive from "naive-ui";

export default defineNuxtPlugin((nuxt) => {
    nuxt.app.use(naive)
})

like this , will can not treeshake

@wobsoriano
Copy link
Contributor

@wobsoriano it's not a good suggestion.

"temporarily"
nuxt/nuxt.js#12050 (comment)

  1. Adding naive-ui to build.transpile - Doesn't work
  2. Adding naive-ui to vite.ssr.noExternal - Doesn't work
  3. Setting vite: false - Works (Uses webpack 5 instead of vite)

This is also my workaround for nuxt3-supabase https://github.com/wobsoriano/nuxt3-supabase#getting-started

this is right. nuxt3 replace global with globalThis , but also replace it in path what be uesed import argument. this is nuxt3's matter.

Yep, until nuxt3 fixes it, gonna have to use that workaround

@AshenBird
Copy link

据我所知,Nuxtjs目前是基于vue2的,而vue3驱动的Nuxt3还在开发中,所以,同样基于vue3的naive-ui很难支持当下的Nuxtjs。
As I know, Nuxtjs is based on vue2, and the Nuxt3 powered with vue3 still in heavy development. So, navie-ui that is also based on vue3 is hard to surpport Nuxtjs now.

我不熟 NUXT,不过估计主要的难点在于 css-in-js 导致的 SSR 问题,其他没啥道理不 work。

确实,主要问题就是这个。nuxt3 的文档缺了很多还,不过很多接口与 nuxt2 类似。主要问题就是需要找个时机讲收集到的样式注入模板,我现在倾向于在渲染后响应客户端之前,对要用来响应的 html 进行注入。或者看下有没有机会渲染前在模板上做手脚。

还有一个目前影响不是很大的问题,就是在 GlobalStyle 模块中,document 的调用没有验证环境,服务端渲染阶段可能会抛出异常。虽然有异常,但是不影响整体运行,我就没去看源码,盲猜是个异步执行,加个验证我觉的会好一点。
大概长这样:

[Vue warn]: Unhandled error during execution of setup function
  at <GlobalStyle>
Server Side Rendering Error: ReferenceError: document is not defined
    at setup (D:\projects\private\mcswift.me\node_modules\naive-ui\lib\global-style\src\GlobalStyle.js:12:26)
    at callWithErrorHandling (D:\projects\private\mcswift.me\node_modules\@vue\runtime-core\dist\runtime-core.cjs.js:6599:22)
    at setupStatefulComponent (D:\projects\private\mcswift.me\node_modules\@vue\runtime-core\dist\runtime-core.cjs.js:6225:29)
    at setupComponent (D:\projects\private\mcswift.me\node_modules\@vue\runtime-core\dist\runtime-core.cjs.js:6181:11)
    at renderComponentVNode (D:\projects\private\mcswift.me\node_modules\@vue\server-renderer\dist\server-renderer.cjs.js:198:17)
    at renderVNode (D:\projects\private\mcswift.me\node_modules\@vue\server-renderer\dist\server-renderer.cjs.js:304:22)
    at renderVNodeChildren (D:\projects\private\mcswift.me\node_modules\@vue\server-renderer\dist\server-renderer.cjs.js:319:9)
    at renderVNode (D:\projects\private\mcswift.me\node_modules\@vue\server-renderer\dist\server-renderer.cjs.js:296:13)
    at renderVNodeChildren (D:\projects\private\mcswift.me\node_modules\@vue\server-renderer\dist\server-renderer.cjs.js:319:9)
    at renderElementVNode (D:\projects\private\mcswift.me\node_modules\@vue\server-renderer\dist\server-renderer.cjs.js:370:17)

@07akioni
Copy link
Collaborator

07akioni commented Oct 18, 2021

I've make it work for nuxt 3 now.

  1. set vite: false in nuxt.config.ts
import { defineNuxtConfig } from 'nuxt3'

export default defineNuxtConfig({
  vite: false
})
  1. install @css-render/vue3-ssr
  2. create naive-ui-plugin.server.ts in <nuxt-project-root>/plugins/
import { setup } from "@css-render/vue3-ssr";
import { defineNuxtPlugin } from "#app";

// I'm not sure whether the plugin is called twice in each refreshing
// Maybe it's expected. If you have more information about it, please comment in the issue

export default defineNuxtPlugin((nuxtApp) => {
  const { collect } = setup(nuxtApp.app);
  nuxtApp.ssrContext!.renderMeta = () => {
    return {
      headTags: collect(),
    };
  };
});

Then it should work.


image

image

@hartmut-co-uk
Copy link

pls note after latest changes (3.0.0-27244261.c49f9ca) nuxtApp.vueApp must be used:

export default defineNuxtPlugin((nuxtApp) => {
  const { collect } = setup(nuxtApp.vueApp);
  ...

@hartmut-co-uk
Copy link

hartmut-co-uk commented Oct 20, 2021

build works, but when starting the bundled app it does not seem to work, errors & warnings server-side, frontend is not styled:

yarn build 
yarn start
[Vue warn]: inject() can only be used inside setup() or functional components.
[Vue warn]: inject() can only be used inside setup() or functional components.
[Vue warn]: provide() can only be used inside setup().
[Vue warn]: onBeforeUnmount is called when there is no active component instance to be associated with. Lifecycle injection APIs can only be used during execution of setup(). If you are using async setup(), make sure to register lifecycle hooks before the first await statement.
[Vue warn]: inject() can only be used inside setup() or functional components.
[Vue warn]: inject() can only be used inside setup() or functional components.
[Vue warn]: inject() can only be used inside setup() or functional components.
TypeError: Cannot destructure property 'styles' of 'ssrContext' as it is undefined.
    at Object.ssrAdapter [as adapter] (./.output/server/node_modules/@css-render/vue3-ssr/lib/index.js:15:13)
    at Object.mount (./.output/server/node_modules/css-render/lib/mount.js:58:20)
    at Object.wrappedMount [as mount] (./.output/server/node_modules/css-render/lib/c.js:14:35)
    at mountStyle (./.output/server/node_modules/naive-ui/lib/_mixins/use-theme.js:19:19)
    at useTheme (./.output/server/node_modules/naive-ui/lib/_mixins/use-theme.js:34:13)
    at setup (./.output/server/node_modules/naive-ui/lib/button/src/Button.js:122:49)
    at callWithErrorHandling (file://./.output/server/chunks/index.mjs:7163:22)
    at setupStatefulComponent (file://./.output/server/chunks/index.mjs:6879:29)
    at setupComponent (file://./.output/server/chunks/index.mjs:6860:11)
    at renderComponentVNode (file://./.output/server/chunks/index.mjs:9758:17)

@07akioni
Copy link
Collaborator

07akioni commented Oct 20, 2021

build works, but when starting the bundled app it does not seem to work, errors & warnings server-side, frontend is not styled:

yarn build 
yarn start
[Vue warn]: inject() can only be used inside setup() or functional components.
[Vue warn]: inject() can only be used inside setup() or functional components.
[Vue warn]: provide() can only be used inside setup().
[Vue warn]: onBeforeUnmount is called when there is no active component instance to be associated with. Lifecycle injection APIs can only be used during execution of setup(). If you are using async setup(), make sure to register lifecycle hooks before the first await statement.
[Vue warn]: inject() can only be used inside setup() or functional components.
[Vue warn]: inject() can only be used inside setup() or functional components.
[Vue warn]: inject() can only be used inside setup() or functional components.
TypeError: Cannot destructure property 'styles' of 'ssrContext' as it is undefined.
    at Object.ssrAdapter [as adapter] (./.output/server/node_modules/@css-render/vue3-ssr/lib/index.js:15:13)
    at Object.mount (./.output/server/node_modules/css-render/lib/mount.js:58:20)
    at Object.wrappedMount [as mount] (./.output/server/node_modules/css-render/lib/c.js:14:35)
    at mountStyle (./.output/server/node_modules/naive-ui/lib/_mixins/use-theme.js:19:19)
    at useTheme (./.output/server/node_modules/naive-ui/lib/_mixins/use-theme.js:34:13)
    at setup (./.output/server/node_modules/naive-ui/lib/button/src/Button.js:122:49)
    at callWithErrorHandling (file://./.output/server/chunks/index.mjs:7163:22)
    at setupStatefulComponent (file://./.output/server/chunks/index.mjs:6879:29)
    at setupComponent (file://./.output/server/chunks/index.mjs:6860:11)
    at renderComponentVNode (file://./.output/server/chunks/index.mjs:9758:17)

I'm not sure what happens. However I think those shouldn't happen. Maybe the token isn't injected to the app.

@pipinet
Copy link
Author

pipinet commented Oct 28, 2021

@07akioni 这个怎么破?

@07akioni
Copy link
Collaborator

@07akioni 这个怎么破?

哪个?这个 issue 里面提供了一个能 work 的方式,虽然看起来不是很完美

@07akioni
Copy link
Collaborator

@07akioni NForm在Nuxt3下异常。

NForm doesn't work in nuxt 3.

image

表单placeholder也有些问题(ssr/csr(加client-only标签)都会有问题,csr在dev下没有问题,但是在build后start会有问题)。

Form placeholder has come issues(ssr/csr widht client only). CSR is okay in dev, however has error after build.

image

表单很简单,大致代码如下。

Form is quite easy, see the following code.

<n-form
      ref='formRef'
      class='mt-4'
      :label-width='100'
      :model='formValue'
      :rules='rules'
  >
    <n-form-item label="用户名" path="username">
      <n-input v-model:value="formValue.username" placeholder="请输入用户名/邮箱/手机号码"/>
    </n-form-item>
    <n-form-item label="密码" path="password">
      <n-input
          v-model:value='formValue.password'
          type='password'
          show-password-on="click"
          placeholder='请输入密码'>
      </n-input>
    </n-form-item>
      <n-button class='mt-4' type='primary' size='large' :loading='loading' block @click='handleSubmit'>
        登录
      </n-button>
  </n-form>

Also there is a good news that I've thought of a way to get rid of inline css var style (with some perf regression which is not that important), but it would take a not short time to implement it.

It would be provided as a new prop in config provider.

@pipinet
Copy link
Author

pipinet commented Dec 20, 2021

出错的位置在这里。貌似跟Hydration有关。
image

@07akioni
Copy link
Collaborator

07akioni commented Dec 21, 2021

出错的位置在这里。貌似跟Hydration有关。 image

有没有可能给 Vue 提个 bug?这种问题估摸着我们很难处理

@jahnli
Copy link
Collaborator

jahnli commented Dec 28, 2021

Could you provide a demo? According to your configuration, I have not succeeded

@jahnli
Copy link
Collaborator

jahnli commented Dec 28, 2021

@Talljack @07akioni 找到nuxt3集成naive-ui的方法了。。这样做是可以work的。样式也是ok的,ssr也是正常的。。但是message貌似有点小问题,message第二次显示显示的时候图标丢了。。

I've find the way to integrate nuxt3 with naive-ui. The following code could work, style is okay and ssr is okay. However there are some problem with message. It would lose icon on is second appearance.

plugins/naive-ui.ts

import {defineNuxtPlugin} from "#app";
import {
  create,
  NButton,
  NConfigProvider,
  NDialogProvider,
  NGlobalStyle,
  NLoadingBarProvider,
  NMessageProvider,
  NLayout,
  NLayoutHeader,
  NLayoutContent,
} from "naive-ui";
import {setup} from "@css-render/vue3-ssr";

export default defineNuxtPlugin((nuxtApp) => {
  if (process.server) {
    const {collect} = setup(nuxtApp.vueApp);
    nuxtApp.ssrContext!.renderMeta = () => {
      return {
        headTags: collect(),
      };
    };
  }
  nuxtApp.vueApp.use(create({
    components: [
      NButton,
      NConfigProvider,
      NGlobalStyle,
      NMessageProvider,
      NDialogProvider,
      NLoadingBarProvider,
      NLayout,
      NLayoutHeader,
      NLayoutContent,
    ]
  }))
});

nuxt.config.ts

import {defineNuxtConfig} from 'nuxt3'

// https://v3.nuxtjs.org/docs/directory-structure/nuxt.config
export default defineNuxtConfig({
  srcDir: 'src',
  build: {
    transpile: ["@urql/vue", "naive-ui", "treemate", "seemly", "vueuc", "vdirs"]
  },
  buildModules: [
    'nuxt-windicss',
    '@vueuse/core/nuxt',
    ['@pinia/nuxt', {disableVuex: true}],
  ],
  css: [
    'virtual:windi.css',
  ]
})

Could you provide a demo? According to your configuration, I have not succeeded

@jaulz
Copy link
Contributor

jaulz commented Jan 17, 2022

出错的位置在这里。貌似跟Hydration有关。 image

Though I am not using nuxt I faced a similar issue in my set up and my PR on vue-next repository might fix the issue for you:
https://github.com/vuejs/core/pull/5239/files

@07akioni
Copy link
Collaborator

出错的位置在这里。貌似跟Hydration有关。 image

Though I am not using nuxt I faced a similar issue in my set up and my PR on vue-next repository might fix the issue for you: https://github.com/vuejs/core/pull/5239/files

I'll change all $slots usage in naive later.

@Baiyuetribe
Copy link
Contributor

Baiyuetribe commented Feb 13, 2022

参考自antdv和element,在nuxt.config.ts引入

import { defineNuxtConfig } from 'nuxt3'
import Components from 'unplugin-vue-components/vite';
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers';
// https://v3.nuxtjs.org/docs/directory-structure/nuxt.config
export default defineNuxtConfig({
    vite: {
        plugins: [
            Components({
                resolvers: [NaiveUiResolver()], // naive-ui组件全自动按需引入
            }),
        ],
        // @ts-expect-error: Missing ssr key
        ssr: {
            noExternal: ['moment', 'compute-scroll-into-view', 'naive-ui'],
        },
    }
})

已搭配css-render,最终样式和组件功能都正常,样式在生成文件的head里。
https://github.com/Baiyuetribe/nuxt3-naive-ui-starter
在线demo演示:https://nuxt3-naive-ui.netlify.app/

@07akioni
Copy link
Collaborator

参考自antdv和element,在nuxt.config.ts引入

import { defineNuxtConfig } from 'nuxt3'
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers';
// https://v3.nuxtjs.org/docs/directory-structure/nuxt.config
export default defineNuxtConfig({
    ssr: false, // dev模式下开启报错 src.replace is not a function;Build模式正常
    vite: {
        plugins: [
            AutoImport({
                resolvers: [NaiveUiResolver()],
            }),
            Components({
                resolvers: [NaiveUiResolver()],
            }),
        ],
        // @ts-expect-error: Missing ssr key
        ssr: {
            noExternal: ['moment', 'compute-scroll-into-view', 'naive-ui'],
        },
    }
})

然后开发时一切正常,构建时可以注释掉ssr。ssr值false时等价于SSG。 https://github.com/Baiyuetribe/nuxt3-naive-ui-starter 在线demo演示:https://nuxt3-naive-ui.netlify.app/

这写写法存在问题,ssr 没样式,大家还是尽量参考置顶的写法

@Baiyuetribe
Copy link
Contributor

参考自antdv和element,在nuxt.config.ts引入

import { defineNuxtConfig } from 'nuxt3'
import Components from 'unplugin-vue-components/vite';
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers';
// https://v3.nuxtjs.org/docs/directory-structure/nuxt.config
export default defineNuxtConfig({
    vite: {
        plugins: [
            Components({
                resolvers: [NaiveUiResolver()], // 全自动按需引入naive-ui组件
            }),
        ],
        // @ts-expect-error: Missing ssr key
        ssr: {
            noExternal: ['moment', 'compute-scroll-into-view', 'naive-ui'],
        },
    }
})

然后开发时一切正常,构建时可以注释掉ssr。ssr值false时等价于SSG。 https://github.com/Baiyuetribe/nuxt3-naive-ui-starter 在线demo演示:https://nuxt3-naive-ui.netlify.app/

这写写法存在问题,ssr 没样式,大家还是尽量参考置顶的写法
ssr样式问题已解决,经过引入css-render后样式出现在文件头部里。实测各个组件基本正常运行,唯独grid自适应有点问题。
image

@Extarys
Copy link

Extarys commented Mar 27, 2022

Nuxt 3 is now in RC1.

@ChiChou
Copy link

ChiChou commented Apr 9, 2022

build works, but when starting the bundled app it does not seem to work, errors & warnings server-side, frontend is not styled:

yarn build 
yarn start
[Vue warn]: inject() can only be used inside setup() or functional components.
[Vue warn]: inject() can only be used inside setup() or functional components.
[Vue warn]: provide() can only be used inside setup().
[Vue warn]: onBeforeUnmount is called when there is no active component instance to be associated with. Lifecycle injection APIs can only be used during execution of setup(). If you are using async setup(), make sure to register lifecycle hooks before the first await statement.
[Vue warn]: inject() can only be used inside setup() or functional components.
[Vue warn]: inject() can only be used inside setup() or functional components.
[Vue warn]: inject() can only be used inside setup() or functional components.
TypeError: Cannot destructure property 'styles' of 'ssrContext' as it is undefined.
    at Object.ssrAdapter [as adapter] (./.output/server/node_modules/@css-render/vue3-ssr/lib/index.js:15:13)
    at Object.mount (./.output/server/node_modules/css-render/lib/mount.js:58:20)
    at Object.wrappedMount [as mount] (./.output/server/node_modules/css-render/lib/c.js:14:35)
    at mountStyle (./.output/server/node_modules/naive-ui/lib/_mixins/use-theme.js:19:19)
    at useTheme (./.output/server/node_modules/naive-ui/lib/_mixins/use-theme.js:34:13)
    at setup (./.output/server/node_modules/naive-ui/lib/button/src/Button.js:122:49)
    at callWithErrorHandling (file://./.output/server/chunks/index.mjs:7163:22)
    at setupStatefulComponent (file://./.output/server/chunks/index.mjs:6879:29)
    at setupComponent (file://./.output/server/chunks/index.mjs:6860:11)
    at renderComponentVNode (file://./.output/server/chunks/index.mjs:9758:17)

I'm not sure what happens. However I think those shouldn't happen. Maybe the token isn't injected to the app.

@07akioni 我用 vite 默认创建的 vue 模板就会遇到这个问题("naive-ui": "^2.27.0",

bug 在 @css-render\vue3-ssr\src\index.ts 第 18 行:

if (ssrContext === null) {
    console.error('[css-render/vue3-ssr]: no ssr context found.')
    return
  }

这里应该判断是否为 undefined,参考
https://vuejs.org/api/composition-api-dependency-injection.html#inject

我没有找到 vue3-ssr 的官方源码仓库,而这个库是 naive-ui 直接引入的

@oooowl
Copy link

oooowl commented Apr 14, 2022

nuxt3 useMessage 第二次图标丢失问题 依然存在

@wc-davide
Copy link

参考自antdv和element,在nuxt.config.ts引入

import { defineNuxtConfig } from 'nuxt3'
import Components from 'unplugin-vue-components/vite';
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers';
// https://v3.nuxtjs.org/docs/directory-structure/nuxt.config
export default defineNuxtConfig({
    vite: {
        plugins: [
            Components({
                resolvers: [NaiveUiResolver()], // naive-ui组件全自动按需引入
            }),
        ],
        // @ts-expect-error: Missing ssr key
        ssr: {
            noExternal: ['moment', 'compute-scroll-into-view', 'naive-ui'],
        },
    }
})

已搭配css-render,最终样式和组件功能都正常,样式在生成文件的head里。 https://github.com/Baiyuetribe/nuxt3-naive-ui-starter 在线demo演示:https://nuxt3-naive-ui.netlify.app/

With nuxt@3.0.0-rc.1 this doesn't seem to work anymore due to a unplugin-vue-components error:

 ERROR  Cannot start nuxt:  Invalid plugin. src option is required: {"name":"unplugin-vue-components","enforce":"post","api":{},"vite":{}}

  at normalizePlugin (/C:/xxx/node_modules/@nuxt/kit/dist/index.mjs:166:11)      
  at /C:/xxx/node_modules/@nuxt/kit/dist/index.mjs:181:61
  at Array.filter (<anonymous>)
  at addPlugin (/C:/xxx/node_modules/@nuxt/kit/dist/index.mjs:181:47)
  at setup (/C:/xxx/node_modules/nuxt/dist/index.mjs:348:5)
  at Object.normalizedModule (/C:/xxx/node_modules/@nuxt/kit/dist/index.mjs:577:29)
  at async installModule (/C:/xxx/node_modules/@nuxt/kit/dist/index.mjs:392:3)   
  at async initNuxt (/C:/xxx/node_modules/nuxt/dist/index.mjs:1362:7)
  at async load (/C:/xxx/node_modules/nuxi/dist/chunks/dev.mjs:6734:9)
  at async Object.invoke (/C:/xxx/node_modules/nuxi/dist/chunks/dev.mjs:6777:5)  

I saw the commit that fixes SSR icons not loading the second time... but now I can't run naive anymore, did anyone manage to find a fix or way to make it work? 🙏

@abhay-agarwal
Copy link

build works, but when starting the bundled app it does not seem to work, errors & warnings server-side, frontend is not styled:

yarn build 
yarn start
[Vue warn]: inject() can only be used inside setup() or functional components.
[Vue warn]: inject() can only be used inside setup() or functional components.
[Vue warn]: provide() can only be used inside setup().
[Vue warn]: onBeforeUnmount is called when there is no active component instance to be associated with. Lifecycle injection APIs can only be used during execution of setup(). If you are using async setup(), make sure to register lifecycle hooks before the first await statement.
[Vue warn]: inject() can only be used inside setup() or functional components.
[Vue warn]: inject() can only be used inside setup() or functional components.
[Vue warn]: inject() can only be used inside setup() or functional components.
TypeError: Cannot destructure property 'styles' of 'ssrContext' as it is undefined.
   at Object.ssrAdapter [as adapter] (./.output/server/node_modules/@css-render/vue3-ssr/lib/index.js:15:13)
   at Object.mount (./.output/server/node_modules/css-render/lib/mount.js:58:20)
   at Object.wrappedMount [as mount] (./.output/server/node_modules/css-render/lib/c.js:14:35)
   at mountStyle (./.output/server/node_modules/naive-ui/lib/_mixins/use-theme.js:19:19)
   at useTheme (./.output/server/node_modules/naive-ui/lib/_mixins/use-theme.js:34:13)
   at setup (./.output/server/node_modules/naive-ui/lib/button/src/Button.js:122:49)
   at callWithErrorHandling (file://./.output/server/chunks/index.mjs:7163:22)
   at setupStatefulComponent (file://./.output/server/chunks/index.mjs:6879:29)
   at setupComponent (file://./.output/server/chunks/index.mjs:6860:11)
   at renderComponentVNode (file://./.output/server/chunks/index.mjs:9758:17)

My setup was previously working, but now I receive the same error as above on Nuxt 3.0.0-rc.1. I am using the plugin method from @css-render/vue3-ssr, with a below plugin file (plugins/naiveui.ts)

import {
  create,
  NAlert,
  ...
  NUploadDragger,
} from 'naive-ui'

import { setup } from '@css-render/vue3-ssr'

export default defineNuxtPlugin((nuxtApp) => {
  if (process.server) {
    const { collect } = setup(nuxtApp.vueApp)
    nuxtApp.ssrContext!.renderMeta = () => {
      return {
        headTags: collect(),
      }
    }
  }
  nuxtApp.vueApp.use(
    create({
      components: [
        NAlert,
        ...
        NUploadDragger,
      ],
    })
  )
})

@madsh93
Copy link

madsh93 commented May 1, 2022

Yes there's a bit confusion as to how Naive UI is installed on Nuxt 3. I'm hoping an official guide will be added to the docs.

I myself am experiencing errors when using useMessage from a composable.

@donPuerto
Copy link

Yep officials docs

@Baiyuetribe
Copy link
Contributor

With nuxt@3.0.0-rc.1

I have fixed naive-ui with nuxt@3.0.0-rc.1. see nuxt3-naive-ui-starter

@Kabuda-czh
Copy link

@Talljack @07akioni 找到nuxt3集成naive-ui的方法了。。这样做是可以work的。样式也是ok的,ssr也是正常的。。但是message貌似有点小问题,message第二次显示显示的时候图标丢了。。
plugins/naive-ui.ts

import {defineNuxtPlugin} from "#app";
import {
  create,
  NButton,
  NConfigProvider,
  NDialogProvider,
  NGlobalStyle,
  NLoadingBarProvider,
  NMessageProvider,
  NLayout,
  NLayoutHeader,
  NLayoutContent,
} from "naive-ui";
import {setup} from "@css-render/vue3-ssr";

export default defineNuxtPlugin((nuxtApp) => {
  if (process.server) {
    const {collect} = setup(nuxtApp.vueApp);
    nuxtApp.ssrContext!.renderMeta = () => {
      return {
        headTags: collect(),
      };
    };
  }
  nuxtApp.vueApp.use(create({
    components: [
      NButton,
      NConfigProvider,
      NGlobalStyle,
      NMessageProvider,
      NDialogProvider,
      NLoadingBarProvider,
      NLayout,
      NLayoutHeader,
      NLayoutContent,
    ]
  }))
});

nuxt.config.ts

import {defineNuxtConfig} from 'nuxt3'

// https://v3.nuxtjs.org/docs/directory-structure/nuxt.config
export default defineNuxtConfig({
  srcDir: 'src',
  build: {
    transpile: ["@urql/vue", "naive-ui", "treemate", "seemly", "vueuc", "vdirs"]
  },
  buildModules: [
    'nuxt-windicss',
    '@vueuse/core/nuxt',
    ['@pinia/nuxt', {disableVuex: true}],
  ],
  css: [
    'virtual:windi.css',
  ]
})

为啥丢我也很费解,太诡异了,感觉是 hydration 的 bug

在使用 nuxt@3.0.0-rc3 中配置了会提示这个 在 package.json 中配置了也是无用的

Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)

@Kabuda-czh
Copy link

在最新的 nuxt@3.0.0-rc3 中, 这样是可以实行的
nuxt.config.ts 文件

import { defineNuxtConfig } from "nuxt";
import Components from "unplugin-vue-components/vite";
import { NaiveUiResolver } from "unplugin-vue-components/resolvers";
// https://v3.nuxtjs.org/docs/directory-structure/nuxt.config
export default defineNuxtConfig({
	vite: {
		plugins: [
			Components({
				resolvers: [NaiveUiResolver()],
			}),
		],
	},
	ssr: false,
});

plugins/naive-ui.ts文件

import {setup} from "@css-render/vue3-ssr";

export default defineNuxtPlugin((nuxtApp) => {
  if (process.server) {
    const {collect} = setup(nuxtApp.vueApp);
    nuxtApp.ssrContext!.renderMeta = () => {
      return {
        headTags: collect(),
      };
    };
  }
});

@raducostinean
Copy link

I've tried to install NaiveUI@2.30.0 with Nuxt@3.0.0-rc3 following the documentation, but whenever I open the browser, after few seconds it freezes out. I can't click, select text or scroll. All I can do is to close it.
I am using Ubuntu 18 on VirtualBox and node 16.15.1.

@07akioni
Copy link
Collaborator

07akioni commented Jun 6, 2022

I've tried to install NaiveUI@2.30.0 with Nuxt@3.0.0-rc3 following the documentation, but whenever I open the browser, after few seconds it freezes out. I can't click, select text or scroll. All I can do is to close it. I am using Ubuntu 18 on VirtualBox and node 16.15.1.

Try this, demo repo is updated

image

@07akioni
Copy link
Collaborator

07akioni commented Jun 6, 2022

I've tried to install NaiveUI@2.30.0 with Nuxt@3.0.0-rc3 following the documentation, but whenever I open the browser, after few seconds it freezes out. I can't click, select text or scroll. All I can do is to close it. I am using Ubuntu 18 on VirtualBox and node 16.15.1.

Try this, demo repo is updated

image

The dependency date-fns is mixing esm & cjs together, so the browser can't recognize cjs content.

(Node js supports mix usage so there's no server side error)

@levidavidmurray
Copy link

levidavidmurray commented Aug 18, 2022

 Cannot find module '/home/.../.output/server/node_modules/date-fns/esm' imported from /home/.../.output/server/chunks/app/_nuxt/_...slug_.119e2e67.mjs

I was getting this error in production SSR, and had to add date-fns to build.transpile in order to fix it

@07akioni
Copy link
Collaborator

 Cannot find module '/home/.../.output/server/node_modules/date-fns/esm' imported from /home/.../.output/server/chunks/app/_nuxt/_...slug_.119e2e67.mjs

I was getting this error in production SSR, and had to add date-fns to build.transpile in order to fix it

Nuxt is not stable now, it may introduce in new rc version. Hope it can be stable soon.

@AdrianoCahete
Copy link

Nuxt is not stable now, it may introduce in new rc version. Hope it can be stable soon.

Now it is, but sadly neither the documentation nor demo website works. :(

@chendalin666
Copy link

在nuxt3中n-grid的x-gap,y-gap和cols属性用到ResponsiveDescription类型后,页面刷新会出现不生效的情况,页面宽度变化或者切换页面后恢复正常。代码如下

<n-grid x-gap="xs:16 s:20 m:24 l:28 xl:32" y-gap="xs:16 s:20 m:24 l:28 xl:32" cols="3 xs:1 s:3" item-responsive responsive="screen"> </n-grid>

测试发现,页面刷新后style中gap属性消失。

image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests