1
1
import {
2
- addImports , addPlugin ,
2
+ addImports , addPlugin , addServerHandler ,
3
3
createResolver ,
4
4
defineNuxtModule ,
5
5
installModule , useLogger ,
@@ -12,6 +12,17 @@ export interface ModuleOptions {
12
12
enabled : boolean
13
13
debug : boolean
14
14
splash : boolean
15
+ /**
16
+ * When enabled, it will redirect any request to the canonical domain (site url) using a 301 redirect on non-dev environments.
17
+ *
18
+ * E.g if the site url is 'www.example.com' and the user visits 'example.com',
19
+ * they will be redirected to 'www.example.com'.
20
+ *
21
+ * This is useful for SEO as it prevents duplicate content and consolidates page rank.
22
+ *
23
+ * @default false
24
+ */
25
+ canonicalDomain : boolean
15
26
}
16
27
17
28
const Modules = [
@@ -26,17 +37,18 @@ const Modules = [
26
37
27
38
export default defineNuxtModule < ModuleOptions > ( {
28
39
meta : {
29
- name : 'nuxt-seo-kit ' ,
40
+ name : 'nuxtseo ' ,
30
41
compatibility : {
31
42
nuxt : '^3.6.5' ,
32
43
bridge : false ,
33
44
} ,
34
- configKey : 'seoKit ' ,
45
+ configKey : 'seo ' ,
35
46
} ,
36
47
defaults ( nuxt ) {
37
48
return {
38
49
enabled : true ,
39
50
debug : false ,
51
+ canonicalDomain : false ,
40
52
splash : nuxt . options . dev ,
41
53
}
42
54
} ,
@@ -79,6 +91,14 @@ export default defineNuxtModule<ModuleOptions>({
79
91
// @ts -expect-error untyped
80
92
nuxt . options . experimental . headNext = true
81
93
94
+ // add redirect middleware
95
+ if ( config . canonicalDomain && nuxt . options . dev === false ) {
96
+ addServerHandler ( {
97
+ handler : resolve ( './runtime/middleware/redirect' ) ,
98
+ middleware : true ,
99
+ } )
100
+ }
101
+
82
102
if ( config . splash ) {
83
103
logger . log ( '' )
84
104
let latestTag = `v${ version } `
0 commit comments