nuxt-site-config 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +203 -0
- package/package.json +56 -0
package/README.md
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
<h1 align='center'>nuxt-site-config</h1>
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href='https://github.com/harlan-zw/nuxt-site-config/actions/workflows/test.yml'>
|
|
5
|
+
</a>
|
|
6
|
+
<a href="https://www.npmjs.com/package/nuxt-site-config" target="__blank"><img src="https://img.shields.io/npm/v/nuxt-site-config?style=flat&colorA=002438&colorB=28CF8D" alt="NPM version"></a>
|
|
7
|
+
<a href="https://www.npmjs.com/package/nuxt-site-config" target="__blank"><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/nuxt-site-config?flat&colorA=002438&colorB=28CF8D"></a>
|
|
8
|
+
<a href="https://github.com/harlan-zw/nuxt-site-config" target="__blank"><img alt="GitHub stars" src="https://img.shields.io/github/stars/harlan-zw/nuxt-site-config?flat&colorA=002438&colorB=28CF8D"></a>
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
<p align="center">
|
|
13
|
+
Shared site configuration for Nuxt 3 modules.
|
|
14
|
+
</p>
|
|
15
|
+
|
|
16
|
+
<p align="center">
|
|
17
|
+
<table>
|
|
18
|
+
<tbody>
|
|
19
|
+
<td align="center">
|
|
20
|
+
<img width="800" height="0" /><br>
|
|
21
|
+
<i>Status:</i> <b>Experimental</b> <br>
|
|
22
|
+
<sup> Please report any issues đ</sup><br>
|
|
23
|
+
<sub>Made possible by my <a href="https://github.com/sponsors/harlan-zw">Sponsor Program đ</a><br> Follow me <a href="https://twitter.com/harlan_zw">@harlan_zw</a> đĻ âĸ Join <a href="https://discord.gg/275MBUBvgP">Discord</a> for help</sub><br>
|
|
24
|
+
<img width="800" height="0" />
|
|
25
|
+
</td>
|
|
26
|
+
</tbody>
|
|
27
|
+
</table>
|
|
28
|
+
</p>
|
|
29
|
+
|
|
30
|
+
âšī¸ Looking for a complete SEO solution? Check out [Nuxt SEO Kit](https://github.com/harlan-zw/nuxt-seo-kit).
|
|
31
|
+
|
|
32
|
+
## Background
|
|
33
|
+
|
|
34
|
+
Site config is a general subset of configurations related to common site-wide settings.
|
|
35
|
+
They are often used in many SEO and performance modules.
|
|
36
|
+
Some examples are: site name, description, canonical URL and trailing slashes.
|
|
37
|
+
|
|
38
|
+
At the surface, most of this config is simple.
|
|
39
|
+
However, some config is more complex, such as the site URL.
|
|
40
|
+
This URL can be inferred
|
|
41
|
+
from the request headers, however, what if we're prerendering pages?
|
|
42
|
+
Do we take into effect the base URL?
|
|
43
|
+
|
|
44
|
+
Also,
|
|
45
|
+
we may want some of this config to be powered by environment variables
|
|
46
|
+
(e.g. staging, production environments), whereas maybe it's more
|
|
47
|
+
appropriate to handle this config within runtime logic (multi-tenant app).
|
|
48
|
+
|
|
49
|
+
Things start getting complicated.
|
|
50
|
+
|
|
51
|
+
By creating a standard API for using site config,
|
|
52
|
+
we make life easier for end users with less config, intelligent defaults and powerful overrides.
|
|
53
|
+
Allowing modules to work better together.
|
|
54
|
+
|
|
55
|
+
## Features
|
|
56
|
+
|
|
57
|
+
- đ Zero-config defaults from environment: site URL, name and description
|
|
58
|
+
- đ¨ Multiple config sources: app.config.ts, nuxt.config.ts and environment variables
|
|
59
|
+
- đ¤ Smart stackable overrides for build and runtime
|
|
60
|
+
- Universal runtimes: Use in Nuxt, Nuxt App, Nitro
|
|
61
|
+
- Editable with HMR and reactivity
|
|
62
|
+
|
|
63
|
+
## Install
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
npm install nuxt-site-config
|
|
67
|
+
|
|
68
|
+
# Using yarn
|
|
69
|
+
yarn add nuxt-site-config
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Setup
|
|
73
|
+
|
|
74
|
+
**Modules**
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
export default defineNuxtModule<ModuleOptions>({
|
|
78
|
+
async setup(config, nuxt) {
|
|
79
|
+
// ...
|
|
80
|
+
await installModule('nuxt-site-config')
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
**Nuxt Apps**
|
|
86
|
+
|
|
87
|
+
_nuxt.config.ts_
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
export default defineNuxtConfig({
|
|
91
|
+
modules: [
|
|
92
|
+
'nuxt-site-config',
|
|
93
|
+
],
|
|
94
|
+
})
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Config Schema
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
export interface SiteConfig {
|
|
101
|
+
/**
|
|
102
|
+
* The canonical Site URL.
|
|
103
|
+
* @default `process.env.NUXT_PUBLIC_SITE_URL`
|
|
104
|
+
* Fallback options are:
|
|
105
|
+
* - SSR: Inferred from request headers
|
|
106
|
+
* - SPA: Inferred from `window.location`
|
|
107
|
+
* - Prerendered: Inferred from CI environment
|
|
108
|
+
*/
|
|
109
|
+
url: string
|
|
110
|
+
name: string
|
|
111
|
+
description: string
|
|
112
|
+
image: string
|
|
113
|
+
index: boolean
|
|
114
|
+
titleSeparator: string
|
|
115
|
+
trailingSlash: boolean
|
|
116
|
+
language: string
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Config Resolving
|
|
121
|
+
|
|
122
|
+
Config is resolved in the following order, starting with the lowest priority.
|
|
123
|
+
1. Context-aware defaults. _For example in some CI environments, we can read environment variables to determine the site URL._
|
|
124
|
+
2. Environment Variables
|
|
125
|
+
3. Runtime config
|
|
126
|
+
4. App config
|
|
127
|
+
5. User overrides
|
|
128
|
+
|
|
129
|
+
## Usage
|
|
130
|
+
|
|
131
|
+
### useSiteConfig - Build time
|
|
132
|
+
|
|
133
|
+
### useSiteConfig - Composable
|
|
134
|
+
|
|
135
|
+
## Nuxt Hooks
|
|
136
|
+
|
|
137
|
+
### `site-config:resolve`
|
|
138
|
+
|
|
139
|
+
**Type:** `async (ctx: { urls: SitemapConfig; sitemapName: string }) => void | Promise<void>`
|
|
140
|
+
|
|
141
|
+
This hook allows you to modify the sitemap(s) urls when they're prerendered.
|
|
142
|
+
|
|
143
|
+
Note: For dynamic runtime sitemaps this hook won't do anything.
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
export default defineNuxtConfig({
|
|
147
|
+
hooks: {
|
|
148
|
+
'site-config:resolve': (siteConfig) => {
|
|
149
|
+
if (process.env.FOO)
|
|
150
|
+
siteConfig.name = 'Bar'
|
|
151
|
+
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
})
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Nitro Hooks
|
|
158
|
+
|
|
159
|
+
### `site-config:resolve`
|
|
160
|
+
|
|
161
|
+
**Type:** `async (ctx: { urls: SitemapConfig; sitemapName: string }) => void | Promise<void>`
|
|
162
|
+
|
|
163
|
+
This hook allows you to modify the sitemap.xml as runtime before it is sent to the client.
|
|
164
|
+
|
|
165
|
+
Note: For prerendered sitemaps this hook won't do anything.
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
import { defineNitroPlugin } from 'nitropack/runtime/plugin'
|
|
169
|
+
import { getRequestHost } from 'h3'
|
|
170
|
+
|
|
171
|
+
export default defineNitroPlugin((nitroApp) => {
|
|
172
|
+
nitroApp.hooks.hook('site-config:resolve', async (siteConfig) => {
|
|
173
|
+
const e = useRequestEvent()
|
|
174
|
+
if (getRequestHost(e).startsWith('foo.'))
|
|
175
|
+
siteConfig.name = 'Foo'
|
|
176
|
+
|
|
177
|
+
})
|
|
178
|
+
})
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Site Config
|
|
182
|
+
|
|
183
|
+
If you need further control over the sitemap.xml URLs, you can provide config on the `sitemap` key.
|
|
184
|
+
|
|
185
|
+
### `url`
|
|
186
|
+
|
|
187
|
+
- Type: `string`
|
|
188
|
+
- Default: `undefined`
|
|
189
|
+
- Required: `true`
|
|
190
|
+
|
|
191
|
+
The host of your site. This is required to generate the sitemap.xml. Example: https://example.com
|
|
192
|
+
|
|
193
|
+
### `trailingSlash`
|
|
194
|
+
|
|
195
|
+
- Type: `boolean`
|
|
196
|
+
- Default: `false`
|
|
197
|
+
|
|
198
|
+
Whether to add a trailing slash to the URLs in the sitemap.xml.
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
## License
|
|
202
|
+
|
|
203
|
+
MIT License Š 2022-PRESENT [Harlan Wilton](https://github.com/harlan-zw)
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nuxt-site-config",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.3",
|
|
5
|
+
"packageManager": "pnpm@8.5.0",
|
|
6
|
+
"description": "Shared site configuration for Nuxt 3 modules.",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"funding": "https://github.com/sponsors/harlan-zw",
|
|
9
|
+
"homepage": "https://github.com/harlan-zw/nuxt-site-config#readme",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/harlan-zw/nuxt-site-config.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/harlan-zw/nuxt-site-config/issues"
|
|
16
|
+
},
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/types.d.ts",
|
|
20
|
+
"require": "./dist/module.cjs",
|
|
21
|
+
"import": "./dist/module.mjs"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"main": "./dist/module.cjs",
|
|
25
|
+
"types": "./dist/types.d.ts",
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@nuxt/kit": "3.5.1",
|
|
31
|
+
"defu": "^6.1.2",
|
|
32
|
+
"pkg-types": "^1.0.3",
|
|
33
|
+
"ufo": "^1.1.2"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@antfu/eslint-config": "^0.39.3",
|
|
37
|
+
"@nuxt/kit": "^3.3.1",
|
|
38
|
+
"@nuxt/module-builder": "^0.4.0",
|
|
39
|
+
"@nuxt/test-utils": "3.5.1",
|
|
40
|
+
"@nuxtjs/eslint-config-typescript": "^12.0.0",
|
|
41
|
+
"bumpp": "^9.1.0",
|
|
42
|
+
"eslint": "8.41.0",
|
|
43
|
+
"execa": "^7.1.1",
|
|
44
|
+
"nuxt": "^3.5.1",
|
|
45
|
+
"vitest": "^0.31.1"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"lint": "eslint . --fix",
|
|
49
|
+
"build": "nuxi prepare .playground && nuxt-module-build",
|
|
50
|
+
"dev": "nuxi dev .playground",
|
|
51
|
+
"dev:build": "nuxi build .playground",
|
|
52
|
+
"dev:prepare": "nuxt-module-build --stub && nuxi prepare .playground",
|
|
53
|
+
"release": "bumpp package.json --commit --push --tag",
|
|
54
|
+
"test": "vitest"
|
|
55
|
+
}
|
|
56
|
+
}
|