Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

5.5 KB
Raw Blame
import { fileURLToPath } from "node:url"

import { defineNuxtConfig } from "nuxt/config"
import tailwindcss from "@tailwindcss/vite"

import { disallowedBots } from "./shared/constants/disallowed-bots"
import locales from "./i18n/data/valid-locales.json"

import type { LocaleObject } from "@nuxtjs/i18n"

export default defineNuxtConfig({
  srcDir: "src/",
  serverDir: "server/",
  // Nuxt 4 resolves `public/` from the root dir, not `srcDir`.
  dir: {
    public: "src/public",
  },
  devServer: {
    port: 8443,
    host: "0.0.0.0",
  },
  devtools: { enabled: true },
  imports: {
    autoImport: false,
  },
  compatibilityDate: "2024-07-23",
  css: ["~/styles/tailwind.css", "~/assets/fonts.css", "~/styles/accent.css"],
  vite: {
    plugins: [tailwindcss()],
  },
  typescript: {
    tsConfig: {
      // Nuxt 4 enables this by default; opt out until the codebase is migrated.
      compilerOptions: { noUncheckedIndexedAccess: false },
      include: ["../typings/**/*"],
      // Storybook is disabled until it supports Nuxt 4 with @nuxtjs/i18n.
      exclude: ["../src/**/*.stories.ts", "../.storybook"],
    },
  },
  /**
   * Define available runtime configuration values and their defaults.
   *
   * See linked documentation for details, including how to override defaults
   * with runtime values using environment variables.
   *
   * @see {@link https://nuxt.com/docs/api/nuxt-config#runtimeconfig-1}
   */
  alias: {
    // Use the source of the workspace package, so that it doesn't need to be
    // built before the frontend.
    "@openverse/api-client": fileURLToPath(
      new URL("../packages/js/api-client/src/index.ts", import.meta.url)
    ),
  },
  runtimeConfig: {
    apiClientId: "",
    apiClientSecret: "",
    // The API address that Nitro uses for proxied requests, overridden by
    // NUXT_API_URL_INTERNAL. Falls back to `public.apiUrl` when empty.
    apiUrlInternal: "",
    // Signs the `ovSession` cookie, overridden by NUXT_HMAC_SIGNING_SECRET.
    // Must be at least 32 characters long.
    hmacSigningSecret: "",
    // Signs Nitro's API requests so that the API's Cloudflare rules skip them,
    // overridden by NUXT_API_REQUEST_SIGNING_SECRET.
    apiRequestSigningSecret: "",
    rateLimit: {
      // `log` only logs the requests that would be limited, `enforce` rejects them.
      mode: "log",
      windowSeconds: 60,
      sessionLimit: 120,
      noSessionLimit: 30,
      ipLimit: 1200,
      // Server-rendered page loads per session. After the first page, people
      // navigate in the browser, so few of their pages load from the server.
      sessionPageLimit: 20,
      // Page loads without a session that one Nuxt task renders per window,
      // from all clients together. Every new visitor's first page counts here.
      noSessionPageLimit: 600,
    },
    public: {
      deploymentEnv: "local",
      apiUrl: "https://api.openverse.org/",
      savedSearchCount: 4,
      site: {
        trailingSlash: false,
      },
      plausible: {
        ignoredHostnames: ["localhost", "staging.openverse.org"],
        logIgnoredEvents: true,
        apiHost: "http://localhost:50290",
        domain: "localhost",
      },
    },
  },
  /**
   * Disable debug mode to prevent excessive timing logs.
   */
  debug: false,
  experimental: {
    /**
     * Improve router performance, see https://nuxt.com/blog/v3-10#%EF%B8%8F-build-time-route-metadata
     */
    scanPageMeta: true,
  },
  modules: [
    "@pinia/nuxt",
    "@nuxtjs/i18n",
    "@nuxtjs/plausible",
    "@nuxt/test-utils/module",
    "@nuxtjs/sitemap",
    "@nuxtjs/robots",
  ],
  routeRules: {
    "/photos/**": { redirect: { to: "/image/**", statusCode: 301 } },
    "/meta-search": { redirect: { to: "/about", statusCode: 301 } },
    "/external-sources": { redirect: { to: "/about", statusCode: 301 } },
  },
  /**
   * Robots.txt rules are configured here via the \@nuxtjs/robots package.
   * @see {@link https://nuxtseo.com/robots/guides/nuxt-config}
   */
  robots: {
    disallow: [
      // robots rules are prefixed-based, so there's no need to configure specific media type searches
      "/search",
      // Other routes have more complex requirements; we configure those with `useRobotsRule` as needed
    ],
    groups: [
      ...disallowedBots.map((bot) => ({
        userAgent: [bot],
        disallow: ["/"], // block disallowed bots from all routes
      })),
    ],
  },
  i18n: {
    locales: [
      {
        /* Nuxt i18n fields */

        code: "en", // unique identifier for the locale in Vue i18n
        dir: "ltr",
        file: "en.json",
        language: "en", // used for SEO purposes (html lang attribute)
        isCatchallLocale: true, // the catchall locale for `en` locales

        /* Custom fields */

        name: "English",
        nativeName: "English",
      },
      ...locales,
    ].filter((l) => Boolean(l.language)) as LocaleObject[],
    langDir: "locales",
    defaultLocale: "en",
    /**
     * `detectBrowserLanguage` must be false to prevent nuxt/i18n from automatically
     * setting the locale based on headers or the client-side `navigator` object.
     *
     * More info about the Nuxt i18n:
     *
     * - [Browser language detection info](https://i18n.nuxtjs.org/docs/guide/browser-language-detection)
     * */
    detectBrowserLanguage: false,
    trailingSlash: false,
    vueI18n: "./vue-i18n.ts",
    experimental: {
      // Keep query strings in hreflang links (v10 strips them), e.g. `/image/collection?tag=cat`.
      alternateLinkCanonicalQueries: false,
    },
  },
})