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

Feature: return an object with id keys and style values for vue3-ssr #1108

Open
jbmolle opened this issue Aug 30, 2023 · 0 comments
Open

Feature: return an object with id keys and style values for vue3-ssr #1108

jbmolle opened this issue Aug 30, 2023 · 0 comments

Comments

@jbmolle
Copy link

jbmolle commented Aug 30, 2023

Hi,

Would it be possible to have a function similar to collect in vue3-ssr but instead of returning a joined string of all the styles, it would be an object { [id]: styleString }?
Nuxt v3.7.0 has just updated how they manage SSR Head with unhead. Before we could return an object { headTags: collect() } in nuxtApp.ssrContext.renderMeta. Now we are using useServerHead({ style: [xxx] }).

So to integrate NaiveUI with Nuxt, we were creating a plugin like this:

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(),
      }
    }
  }
})

But now, we have to do:

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

export default defineNuxtPlugin((nuxtApp) => {
    const { collect } = setup(nuxtApp.vueApp)
    useServerHead({
        style: () => {
            const stylesString = collect()
            const stylesArray = stylesString.split(/<\/style>/g).filter(style => style)
            return stylesArray.map((styleString: string) => {
                const match = styleString.match(/<style cssr-id="([^"]*)">([\s\S]*)/)
                if (match) {
                    const id = match[1]
                    return { 'cssr-id': id, children: match[2] }
                }
                return {}
            })
        }
    })
})

so it would be easier to have a function collectAsObject in setup that would return the map of ids and corresponding CSS styles.

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

No branches or pull requests

1 participant