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

Nuxt3 Support #9

Closed
pisandelli opened this issue Mar 23, 2022 · 7 comments
Closed

Nuxt3 Support #9

pisandelli opened this issue Mar 23, 2022 · 7 comments

Comments

@pisandelli
Copy link

pisandelli commented Mar 23, 2022

Hello guys! I tried to add the plugin into a NUXT3 app using vite, but it seems not to be working. Am I doing something wrong? I'm using the latest version (RC4) from Nuxt3.
My nuxt.config.ts is like this:

import { defineNuxtConfig } from 'nuxt'
import { VitePluginFonts } from 'vite-plugin-fonts'

export default defineNuxtConfig({

  vite: {
    plugins: [
      VitePluginFonts({
        google: {
          families: [{name:'Barlow', styles:'wght@100;300;400'}]
        },
      })
    ]
  }

})
@pisandelli
Copy link
Author

Hello! Any help?
Thanks in advance.

@RobbieTheWagner
Copy link

@pisandelli Did you ever figure this out? What is not working exactly? For me, I get a 404. GET http://localhost:3000/_nuxt/@vite-plugin-fonts/fonts.css net::ERR_ABORTED 404 (Not Found)

@stafyniaksacha
Copy link
Member

This plugin can not work with nuxt3 since it uses transformIndexHtml vite function which is not used by nuxt

We have to create a nuxt3 module which injects tags inside the nuxt app.head config

@pisandelli
Copy link
Author

@pisandelli Did you ever figure this out? What is not working exactly? For me, I get a 404. GET http://localhost:3000/_nuxt/@vite-plugin-fonts/fonts.css net::ERR_ABORTED 404 (Not Found)

Hey @rwwagner90 for me the fonts are not being added.
But with this answer from @stafyniaksacha everything make sense now.
I'm using useHead from VueUse as a solution.

Thanks for @stafyniaksacha for clarifying this issue.

@RobbieTheWagner
Copy link

I just manually added my fonts to the app.head config for now. @pisandelli do you have an example to share of how to make this work?

@stafyniaksacha
Copy link
Member

For nuxt and google font you can use this:

import { defineNuxtConfig } from 'nuxt'

export default defineNuxtConfig({
  app: {
    head: {
      link: [
        { rel: 'preconnect', href: 'https://fonts.googleapis.com' },
        {
          rel: 'preconnect',
          href: 'https://fonts.gstatic.com',
          crossorigin: true,
        },
        {
          rel: 'stylesheet',
          href: 'https://fonts.googleapis.com/css2?family=Roboto+Flex:wght@200..800&display=swap',
        },
      ],
    },
  },
})

@pisandelli
Copy link
Author

pisandelli commented Aug 8, 2022

I'm using a composable.
Much better the app.head approach, though.

//composabels/useGoogleFonts.ts

import { useHead } from '@vueuse/head'

export function useGoogleFonts() {
  useHead({
    link: [
      {
        rel: 'preconnect',
        href: 'https://fonts.googleapis.com'
      },
      {
        rel: 'preconnect',
        href: 'https://fonts.gstatic.com',
        crossorigin: 'crossorigin'
      },
      {
        rel: 'stylesheet',
        href: 'https://fonts.googleapis.com/css2?family=Lato:wght@300;400;700&display=swap'
      }
    ],
  })
}

And call it in my app.vue

<script lang="ts" setup>
 import { useGoogleFonts } from './composables/useGoogleFonts'
 useGoogleFonts()
</script>

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

3 participants