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

preload for downloaded fonts #182

Open
codeflorist opened this issue Feb 29, 2024 · 2 comments
Open

preload for downloaded fonts #182

codeflorist opened this issue Feb 29, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@codeflorist
Copy link

SEO- and performance-wise it makes sense to add a <link rel="preload" tag for the fonts used by the site. The option preload: true however only works, if download: false is set. It would be great, if it also works with downloaded fonts.

@ricardogobbosouza ricardogobbosouza added the enhancement New feature or request label Mar 14, 2024
@riddla
Copy link

riddla commented May 29, 2024

I'm currently using a quick and dirty local Nuxt module for this:

import { defineNuxtModule } from "nuxt/kit";
import { globby } from "globby";
import { basename, extname } from "node:path";

export default defineNuxtModule({
  hooks: {
    ready: async (nuxt) => {
      if (!nuxt.options.nitro.publicAssets) {
        return;
      }

      const googleFontsModuleDir = nuxt.options.nitro.publicAssets.find(
        (assetDirectory) => assetDirectory?.dir?.includes("nuxt-google-fonts")
      );

      if (!googleFontsModuleDir) {
        return;
      }

      const outputDir = googleFontsModuleDir.dir;
      const outputFonts = await globby(outputDir!);

      outputFonts.forEach((fontFilePath) => {
        nuxt.options.app.head.link!.push({
          rel: "preload",
          as: "font",
          type: `font/woff2`,
          href: `/fonts/${basename(fontFilePath)}`,
        });
      });
    },
  },
});

@codeflorist
Copy link
Author

@riddla

Nice, thanks!

It's also worth mentioning, that https://github.com/nuxt/fonts has the preload-feature on-board, and it seems, that nuxt/fonts will be the successor package of nuxt-modules/google-fonts (see nuxt/fonts#57)

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

No branches or pull requests

3 participants