mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 07:05:11 +00:00
fix(nuxt): deduplicate app.head
arrays (#27480)
This commit is contained in:
parent
5785332626
commit
34ea5ae2f2
@ -7,6 +7,7 @@ import { resolvePath as _resolvePath } from 'mlly'
|
||||
import type { Nuxt, NuxtHooks, NuxtModule, NuxtOptions, RuntimeConfig } from 'nuxt/schema'
|
||||
import type { PackageJson } from 'pkg-types'
|
||||
import { readPackageJSON, resolvePackageJSON } from 'pkg-types'
|
||||
import { hash } from 'ohash'
|
||||
|
||||
import escapeRE from 'escape-string-regexp'
|
||||
import fse from 'fs-extra'
|
||||
@ -583,6 +584,11 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
|
||||
options.appDir = options.alias['#app'] = resolve(distDir, 'app')
|
||||
options._majorVersion = 3
|
||||
|
||||
// De-duplicate key arrays
|
||||
for (const key in options.app.head || {}) {
|
||||
options.app.head[key as 'link'] = deduplicateArray(options.app.head[key as 'link'])
|
||||
}
|
||||
|
||||
// Nuxt DevTools only works for Vite
|
||||
if (options.builder === '@nuxt/vite-builder') {
|
||||
const isDevToolsEnabled = typeof options.devtools === 'boolean'
|
||||
@ -668,3 +674,18 @@ async function checkDependencyVersion (name: string, nuxtVersion: string): Promi
|
||||
}
|
||||
|
||||
const RESTART_RE = /^(?:app|error|app\.config)\.(?:js|ts|mjs|jsx|tsx|vue)$/i
|
||||
|
||||
function deduplicateArray<T = unknown> (maybeArray: T): T {
|
||||
if (!Array.isArray(maybeArray)) { return maybeArray }
|
||||
|
||||
const fresh = []
|
||||
const hashes = new Set<string>()
|
||||
for (const item of maybeArray) {
|
||||
const _hash = hash(item)
|
||||
if (!hashes.has(_hash)) {
|
||||
hashes.add(_hash)
|
||||
fresh.push(item)
|
||||
}
|
||||
}
|
||||
return fresh as T
|
||||
}
|
||||
|
@ -64,6 +64,7 @@ describe('route rules', () => {
|
||||
expect(headHtml).toContain('<meta name="description" content="Nuxt Fixture">')
|
||||
expect(headHtml).toContain('<meta charset="utf-8">')
|
||||
expect(headHtml).toContain('<meta name="viewport" content="width=1024, initial-scale=1">')
|
||||
expect(headHtml.match(/<meta name="viewport" content="width=1024, initial-scale=1">/g)).toHaveLength(1)
|
||||
|
||||
const { script, attrs } = parseData(headHtml)
|
||||
expect(script.serverRendered).toEqual(false)
|
||||
|
@ -1,3 +1,11 @@
|
||||
export default defineNuxtConfig({
|
||||
modules: [undefined],
|
||||
app: {
|
||||
head: {
|
||||
meta: [
|
||||
{ name: 'viewport', content: 'width=1024, initial-scale=1' },
|
||||
{ charset: 'utf-8' },
|
||||
],
|
||||
},
|
||||
},
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user