forked from WordPress/openverse-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nuxt.config.ts
359 lines (344 loc) · 10.8 KB
/
nuxt.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import path from 'path'
import fs from 'fs'
import promBundle from 'express-prom-bundle'
import pkg from './package.json'
import locales from './src/locales/scripts/valid-locales.json'
import { searchTypes } from './src/constants/media'
import { VIEWPORTS } from './src/constants/screens'
import { isProd } from './src/utils/node-env'
import { sentryConfig } from './src/utils/sentry-config'
import { env } from './src/utils/env'
import type { NuxtConfig, ServerMiddleware } from '@nuxt/types'
import type { LocaleObject } from '@nuxtjs/i18n'
/**
* The default metadata for the site. Can be extended and/or overwritten per page. And even in components!
* See the Nuxt.js docs for more info.
* {@link https://nuxtjs.org/guides/features/meta-tags-seo} Nuxt.js Docs
*/
const meta = [
{ charset: 'utf-8' },
{
name: 'viewport',
content: 'width=device-width,initial-scale=1',
},
// Tells Google to only crawl Openverse when iframed
{ hid: 'googlebot', name: 'googlebot', content: 'noindex,indexifembedded' },
{
vmid: 'monetization',
name: 'monetization',
content: '$ilp.uphold.com/edR8erBDbRyq',
},
{
hid: 'theme-color',
name: 'theme-color',
content: '#ffffff',
},
{
name: 'description',
content:
'Search over 600 million free and openly licensed images, photos, audio, and other media types for reuse and remixing.',
},
{ hid: 'og:title', name: 'og:title', content: 'Openverse' },
{
hid: 'og:image',
name: 'og:image',
content: '/openverse-default.jpg',
},
{
hid: 'og:description',
name: 'og:description',
content:
'Search over 600 million free and openly licensed images, photos, audio, and other media types for reuse and remixing.',
},
{ name: 'twitter:card', content: 'summary_large_image' },
{ name: 'twitter:site', content: '@WPOpenverse' },
]
if (process.env.NODE_ENV === 'production') {
meta.push({
// @ts-expect-error: 'http-equiv' isn't allowed here by Nuxt
'http-equiv': 'Content-Security-Policy',
content: 'upgrade-insecure-requests',
})
}
const favicons = [
// SVG favicon
{
rel: 'icon',
href: '/openverse-logo.svg',
},
// SVG favicon for Safari
{
rel: 'mask-icon',
href: '/opvenverse-logo.svg',
color: '#30272E',
},
// Fallback iPhone Icon
{
rel: 'apple-touch-icon',
href: '/openverse-logo-180.png',
},
]
// Default html head
const head = {
title: 'Openly Licensed Images, Audio and More | Openverse',
meta,
link: [
...favicons,
{
rel: 'preconnect',
href: env.apiUrl,
crossorigin: '',
},
{
rel: 'dns-prefetch',
href: env.apiUrl,
},
{
rel: 'search',
type: 'application/opensearchdescription+xml',
title: 'Openverse',
href: '/opensearch.xml',
},
],
}
const baseProdName = process.env.CI ? '[name]' : '[contenthash:7]'
const filenames: NonNullable<NuxtConfig['build']>['filenames'] = {
app: ({ isDev, isModern }) =>
isDev
? `[name]${isModern ? '.modern' : ''}.js`
: `${baseProdName}${isModern ? '.modern' : ''}.js`,
chunk: ({ isDev, isModern }) =>
isDev
? `[name]${isModern ? '.modern' : ''}.js`
: `${baseProdName}${isModern ? '.modern' : ''}.js`,
css: ({ isDev }) => (isDev ? '[name].css' : `css/${baseProdName}.css`),
img: ({ isDev }) =>
isDev ? '[path][name].[ext]' : `img/${baseProdName}.[ext]`,
font: ({ isDev }) =>
isDev ? '[path][name].[ext]' : `fonts/${baseProdName}.[ext]`,
video: ({ isDev }) =>
isDev ? '[path][name].[ext]' : `videos/${baseProdName}.[ext]`,
}
const config: NuxtConfig = {
// eslint-disable-next-line no-undef
version: pkg.version, // used to purge cache :)
cache: {
pages: ['/'],
store: {
type: 'memory', // 'redis' would be nice
max: 100,
ttl: process.env.MICROCACHE_DURATION || 60,
},
},
srcDir: 'src/',
// Necessary to fix pm2 + nuxt issues
rootDir: process.cwd(),
buildDir: process.cwd() + '/.nuxt/',
modern: 'client',
server: {
port: process.env.PORT || 8443,
https: process.env.LOCAL_SSL
? {
key: fs.readFileSync(path.resolve(__dirname, 'localhost+1-key.pem')),
cert: fs.readFileSync(path.resolve(__dirname, 'localhost+1.pem')),
}
: undefined,
},
router: {
middleware: 'middleware',
},
components: [
{ path: '~/components', extensions: ['vue'], pathPrefix: false },
],
plugins: [
{ src: '~/plugins/url-change' },
{ src: '~/plugins/migration-notice' },
{ src: '~/plugins/ua-parse' },
{ src: '~/plugins/focus-visible', mode: 'client' },
],
css: ['~/styles/tailwind.css', '~/assets/fonts.css', '~/styles/accent.css'],
head,
env,
dev: !isProd,
buildModules: [
'@nuxt/typescript-build',
'@nuxtjs/composition-api/module',
'@nuxt/postcss8',
'@nuxtjs/style-resources',
'@nuxtjs/svg',
'@nuxtjs/eslint-module',
'@pinia/nuxt',
],
modules: [
'@nuxtjs/i18n',
'@nuxtjs/redirect-module',
'@nuxtjs/sentry',
'@nuxtjs/sitemap',
'cookie-universal-nuxt',
],
serverMiddleware: [
{ path: '/healthcheck', handler: '~/server-middleware/healthcheck.js' },
],
i18n: {
locales: [
{
// unique identifier for the locale in Vue i18n
code: 'en',
name: 'English',
// ISO code used for SEO purposes (html lang attribute)
iso: 'en',
// wp_locale as found in GlotPress
wpLocale: 'en_US',
file: 'en.json',
},
...(locales ?? []),
].filter((l) => Boolean(l.iso)) as LocaleObject[],
lazy: true,
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.
*
* Such detection is handled at the parent level in WP.org.
*
* More info about the Nuxt i18n:
*
* - [detectBrowserLanguage](https://i18n.nuxtjs.org/options-reference/#detectbrowserlanguage)
* - [Browser language detection info](https://i18n.nuxtjs.org/browser-language-detection)
* */
detectBrowserLanguage: false,
vueI18n: '~/plugins/vue-i18n',
},
/**
* Map the old route for /photos/_id page to /image/_id permanently to keep links working.
* See the redirect module for more info.
* {@link https://github.com/nuxt-community/redirect-module#usage}
*/
redirect: [{ from: '^/photos/(.*)$', to: '/image/$1', statusCode: 301 }],
sentry: sentryConfig,
hooks: {
render: {
/**
* When modifying this function in development with automatic rebuilds enabled
* it _will_ crash your server with an error about duplicate metric name registration.
* To debug this function's behavior locally you will have to tolerate stopping and
* starting `pnpm dev` between each change to nuxt.config.ts. To prevent this from
* being a _general_ problem the metrics middleware is disabled in development
* mode, otherwise any change to `nuxt.config.ts` would cause this crash. If you
* need to debug metrics locally, comment out the early return in development.
*/
setupMiddleware: (app) => {
if (process.env.NODE_ENV === 'development') return
const bypassMetricsPathParts = [
/**
* Exclude static paths. Remove once we've moved static file
* hosting out of the SSR server's responsibilities.
*/
'_nuxt',
/**
* Only include these paths in development. They do not exist in production
* so we can happily skip the extra iterations these path parts introduce.
*/
...(process.env.NODE_ENV === 'development'
? ['__webpack', 'sse']
: []),
]
/**
* Register this here so that it's registered at the absolute top
* of the middleware stack. Using server-middleware puts it
* after a whole host of stuff.
*
* Note: The middleware only has access to server side navigations,
* as it is indeed an express middleware, not a Nuxt page middleware.
* There's no safe way to pipe client side metrics to Prometheus with
* this set up and if we wanted that anyway we'll want to invest into
* an actual RUM solution, not a systems monitoring solution like
* Prometheus. The implication of this is that the metrics will only
* include SSR'd requests. SPA navigations or anything else that
* happens exclusively on the client will not be measured. This is the
* expected behavior!
*
* @see {@link https://github.com/nuxt/nuxt.js/blob/dev/packages/server/src/server.js#L70-L138}
*/
app.use(
promBundle({
bypass: (req) =>
bypassMetricsPathParts.some((p) => req.originalUrl.includes(p)),
includeMethod: true,
includePath: true,
normalizePath: [
...searchTypes.map(
// Normalize single result pages with IDs in the path
(t) => [`/${t}/.*`, `/${t}/#id`] as [string, string]
),
],
/**
* promBundle creates an Express middleware function, which is type-incompatible
* with Nuxt's "connect" middleware functions. I guess they _are_ compatible,
* in actual code, or maybe just in the particular implementation of the prometheus
* bundle middleware. In any case, this cast is necessary to appeas TypeScript
* without an ugly ts-ignore.
*/
}) as unknown as ServerMiddleware
)
},
},
},
build: {
templates: [
{
src: './nuxt-template-overrides/App.js',
dst: 'App.js',
},
{
src: './nuxt-template-overrides/index.js',
dst: 'index.js',
},
],
filenames,
friendlyErrors: false,
postcss: {
plugins: {
tailwindcss: {},
autoprefixer: {},
'postcss-focus-visible': {},
},
},
extend(config, ctx) {
// Enables use of IDE debuggers
config.devtool = ctx.isClient ? 'source-map' : 'inline-source-map'
// Mitigates import errors for Pinia
config.module?.rules.push({
test: /\.mjs$/,
include: /node_modules/,
type: 'javascript/auto',
})
},
},
storybook: {
port: 6006, // standard port for Storybook
stories: ['~/**/*.stories.@(mdx|js)'],
addons: [
{
name: '@storybook/addon-essentials',
options: {
backgrounds: false,
viewport: true,
toolbars: true,
},
},
],
parameters: {
options: {
storySort: {
order: ['Introduction', ['Openverse UI']],
},
},
viewport: {
viewports: VIEWPORTS,
},
},
},
}
export default config