|
1 | 1 | import {
|
2 |
| - addImports, |
3 |
| - addPlugin, |
4 |
| - addServerHandler, |
5 |
| - createResolver, |
6 | 2 | defineNuxtModule,
|
7 |
| - hasNuxtModule, |
8 | 3 | installModule,
|
9 |
| - useLogger, |
| 4 | + resolvePath, |
10 | 5 | } from '@nuxt/kit'
|
11 |
| -import { colors } from 'consola/utils' |
12 |
| -import { installNuxtSiteConfig } from 'nuxt-site-config-kit' |
13 |
| -import { $fetch } from 'ofetch' |
14 |
| -import { readPackageJSON } from 'pkg-types' |
| 6 | +import { modules } from './const' |
15 | 7 |
|
16 | 8 | export interface ModuleOptions {
|
17 |
| - /** |
18 |
| - * Will ensure a title is always set by providing a fallback title based on the casing the last slug segment. |
19 |
| - * |
20 |
| - * @default true |
21 |
| - */ |
22 |
| - fallbackTitle?: boolean |
23 |
| - /** |
24 |
| - * Will set up a number of defaults for meta tags and Schema.org, if the modules and config are available. |
25 |
| - * |
26 |
| - * @default true |
27 |
| - */ |
28 |
| - automaticDefaults?: boolean |
29 |
| - /** |
30 |
| - * When enabled, it will whitelist the query parameters that are allowed in the canonical URL. |
31 |
| - */ |
32 |
| - canonicalQueryWhitelist?: string[] |
33 |
| - /** |
34 |
| - * When enabled, it will redirect any request to the canonical domain (site url) using a 301 redirect on non-dev environments. |
35 |
| - * |
36 |
| - * E.g if the site url is 'www.example.com' and the user visits 'example.com', |
37 |
| - * they will be redirected to 'www.example.com'. |
38 |
| - * |
39 |
| - * This is useful for SEO as it prevents duplicate content and consolidates page rank. |
40 |
| - * |
41 |
| - * @default false |
42 |
| - */ |
43 |
| - redirectToCanonicalSiteUrl?: boolean |
44 | 9 | /**
|
45 | 10 | * Whether the module should be loaded.
|
46 | 11 | */
|
47 | 12 | enabled: boolean
|
48 |
| - /** |
49 |
| - * Whether the debugging mode should be enabled. |
50 |
| - * |
51 |
| - * @default `nuxt.options.debug` |
52 |
| - */ |
53 |
| - debug: boolean |
54 |
| - /** |
55 |
| - * Whether the Nuxt SEO splash should be shown when Nuxt is started. |
56 |
| - * |
57 |
| - * @default `nuxt.options.dev` |
58 |
| - */ |
59 |
| - splash: boolean |
60 | 13 | }
|
61 | 14 |
|
62 |
| -const Modules = [ |
63 |
| - '@nuxtjs/robots', |
64 |
| - '@nuxtjs/sitemap', |
65 |
| - 'nuxt-og-image', |
66 |
| - 'nuxt-schema-org', |
67 |
| - 'nuxt-seo-experiments', |
68 |
| - 'nuxt-link-checker', |
69 |
| -] |
70 |
| - |
71 | 15 | export default defineNuxtModule<ModuleOptions>({
|
72 | 16 | meta: {
|
73 | 17 | name: 'nuxtseo',
|
74 | 18 | compatibility: {
|
75 | 19 | nuxt: '>=3.7.0',
|
76 | 20 | bridge: false,
|
77 | 21 | },
|
78 |
| - configKey: 'seo', |
79 | 22 | },
|
80 |
| - defaults(nuxt) { |
81 |
| - return { |
82 |
| - enabled: true, |
83 |
| - debug: nuxt.options.debug, |
84 |
| - redirectToCanonicalSiteUrl: false, |
85 |
| - splash: false, // nuxt.options.dev, - figure out a solution for this in the future |
86 |
| - automaticDefaults: true, |
87 |
| - fallbackTitle: true, |
88 |
| - } |
| 23 | + defaults: { |
| 24 | + enabled: true, |
89 | 25 | },
|
90 | 26 | async setup(config, nuxt) {
|
91 |
| - const { resolve, resolvePath } = createResolver(import.meta.url) |
92 |
| - const { name, version } = await readPackageJSON(resolve('../package.json')) |
93 |
| - const logger = useLogger(name) |
94 |
| - logger.level = config.debug ? 4 : 3 |
95 |
| - if (config.enabled === false) { |
96 |
| - logger.debug('The module is disabled, skipping setup.') |
| 27 | + if (!config.enabled) { |
97 | 28 | return
|
98 | 29 | }
|
99 |
| - |
100 |
| - await installNuxtSiteConfig() |
101 |
| - |
102 |
| - // TODO disable modules if SSR is disabled |
103 |
| - // if (!nuxt.options.ssr) { |
104 |
| - // nuxt.options.schemaOrg = defu({ enabled: false}, nuxt.options.schemaOrg) |
105 |
| - // nuxt.options.ogImage = defu({ enabled: false}, nuxt.options.ogImage) |
106 |
| - // logger.warn('SSR is required for Nuxt OG Image and Nuxt Schema.org, disabling them.') |
107 |
| - // } |
108 |
| - |
109 |
| - for (const module of Modules) |
110 |
| - await installModule(await resolvePath(module)) |
111 |
| - |
112 |
| - nuxt.options.runtimeConfig.public['nuxt-seo'] = { |
113 |
| - canonicalQueryWhitelist: config.canonicalQueryWhitelist || [ |
114 |
| - 'page', |
115 |
| - 'sort', |
116 |
| - 'filter', |
117 |
| - 'search', |
118 |
| - 'q', |
119 |
| - 'category', |
120 |
| - 'tag', |
121 |
| - ], |
122 |
| - } |
123 |
| - |
124 |
| - if (config.automaticDefaults) { |
125 |
| - // i18n complicates things, we need to run the server plugin at the right time, client is fine |
126 |
| - if (hasNuxtModule('@nuxtjs/i18n')) { |
127 |
| - addPlugin({ |
128 |
| - src: resolve(`./runtime/nuxt/plugin/defaultsWaitI18n`), |
129 |
| - }) |
130 |
| - } |
131 |
| - else { |
132 |
| - addPlugin({ |
133 |
| - src: resolve(`./runtime/nuxt/plugin/defaults`), |
134 |
| - }) |
135 |
| - } |
136 |
| - } |
137 |
| - if (config.fallbackTitle) { |
138 |
| - addPlugin({ |
139 |
| - src: resolve('./runtime/nuxt/plugin/titles'), |
140 |
| - }) |
141 |
| - } |
142 |
| - |
143 |
| - if (!hasNuxtModule('@nuxtjs/i18n')) { |
144 |
| - addImports({ |
145 |
| - from: resolve(`./runtime/nuxt/composables/polyfills`), |
146 |
| - name: 'useI18n', |
147 |
| - }) |
148 |
| - } |
149 |
| - |
150 |
| - addImports({ |
151 |
| - from: resolve(`./runtime/nuxt/composables/useBreadcrumbItems`), |
152 |
| - name: 'useBreadcrumbItems', |
153 |
| - }) |
154 |
| - |
155 |
| - // TODO blocked by https://github.com/nuxt/nuxt/issues/25532 |
156 |
| - // if (nuxt.options.experimental?.defaults?.nuxtLink && typeof nuxt.options.experimental?.defaults?.nuxtLink?.trailingSlash == 'undefined') |
157 |
| - // nuxt.options.experimental.defaults.nuxtLink.trailingSlash = siteConfig.trailingSlash ? 'append' : 'remove' |
158 |
| - |
159 |
| - // if user disables certain modules we need to pollyfill the imports |
160 |
| - const polyfills: Record<string, string[]> = { |
161 |
| - schemaOrg: ['useSchemaOrg', 'defineWebSite', 'defineWebPage'], |
162 |
| - } |
163 |
| - for (const [module, composables] of Object.entries(polyfills)) { |
164 |
| - if (nuxt.options[module as keyof typeof nuxt.options]?.enable === false) { |
165 |
| - composables.forEach((name) => { |
166 |
| - // add pollyfill |
167 |
| - addImports({ |
168 |
| - from: resolve('./runtime/nuxt/composables/polyfills'), |
169 |
| - name, |
170 |
| - }) |
171 |
| - }) |
172 |
| - } |
173 |
| - } |
174 |
| - nuxt.options.experimental.headNext = true |
175 |
| - |
176 |
| - // add redirect middleware |
177 |
| - if (!nuxt.options.dev && config.redirectToCanonicalSiteUrl) { |
178 |
| - addServerHandler({ |
179 |
| - handler: resolve('./runtime/nitro/middleware/redirect'), |
180 |
| - middleware: true, |
181 |
| - }) |
182 |
| - } |
183 |
| - |
184 |
| - if (config.splash && !version!.includes('rc') && nuxt.options.dev) { |
185 |
| - logger.log('') |
186 |
| - let latestTag = `v${version}` |
187 |
| - latestTag = (await $fetch<any>('https://ungh.unjs.io/repos/harlan-zw/nuxt-seo/releases/latest', { |
188 |
| - timeout: 2000, |
189 |
| - }) |
190 |
| - .catch(() => { |
191 |
| - return { release: { tag: `v${version}` } } |
192 |
| - })).release.tag |
193 |
| - const upToDate = latestTag === `v${version}` |
194 |
| - logger.log(`${colors.green('Nuxt SEO')} ${colors.yellow(`v${version}`)} ${colors.gray(`by ${colors.underline('@harlan_zw')}`)}`) |
195 |
| - if (!upToDate) |
196 |
| - logger.log(`${colors.gray(' ├─ ')}🎉 New version available!${colors.gray(` Run ${colors.underline(`npm i @nuxtjs/seo@${latestTag}`)} to update.`)}`) |
197 |
| - |
198 |
| - logger.log(colors.dim(' └─ 🧪 Help get Nuxt SEO stable by providing feedback https://github.com/harlan-zw/nuxt-seo/discussions/108')) |
199 |
| - logger.log('') |
200 |
| - } |
| 30 | + for (const module of modules) |
| 31 | + await installModule(await resolvePath(module.npm), {}, nuxt) |
201 | 32 | },
|
202 | 33 | })
|
0 commit comments