nuxt-site-config 0.6.0 → 0.7.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.
Files changed (35) hide show
  1. package/README.md +36 -36
  2. package/package.json +7 -13
  3. package/dist/kit.d.ts +0 -16
  4. package/dist/kit.mjs +0 -179
  5. package/dist/module.cjs +0 -5
  6. package/dist/module.d.ts +0 -41
  7. package/dist/module.json +0 -9
  8. package/dist/module.mjs +0 -83
  9. package/dist/runtime/component/SiteLink.vue +0 -33
  10. package/dist/runtime/composables/updateSiteConfig.d.ts +0 -2
  11. package/dist/runtime/composables/updateSiteConfig.mjs +0 -10
  12. package/dist/runtime/composables/useSiteConfig.d.ts +0 -1
  13. package/dist/runtime/composables/useSiteConfig.mjs +0 -12
  14. package/dist/runtime/composables/utils.d.ts +0 -5
  15. package/dist/runtime/composables/utils.mjs +0 -30
  16. package/dist/runtime/nitro/composables/updateSiteConfig.d.ts +0 -3
  17. package/dist/runtime/nitro/composables/updateSiteConfig.mjs +0 -5
  18. package/dist/runtime/nitro/composables/useNitroOrigin.d.ts +0 -2
  19. package/dist/runtime/nitro/composables/useNitroOrigin.mjs +0 -25
  20. package/dist/runtime/nitro/composables/useSiteConfig.d.ts +0 -2
  21. package/dist/runtime/nitro/composables/useSiteConfig.mjs +0 -5
  22. package/dist/runtime/nitro/middleware/init.d.ts +0 -2
  23. package/dist/runtime/nitro/middleware/init.mjs +0 -13
  24. package/dist/runtime/plugins/siteConfig.d.ts +0 -2
  25. package/dist/runtime/plugins/siteConfig.mjs +0 -28
  26. package/dist/runtime/siteConfig/env.d.ts +0 -2
  27. package/dist/runtime/siteConfig/env.mjs +0 -22
  28. package/dist/runtime/siteConfig/index.d.ts +0 -2
  29. package/dist/runtime/siteConfig/index.mjs +0 -2
  30. package/dist/runtime/siteConfig/stack.d.ts +0 -2
  31. package/dist/runtime/siteConfig/stack.mjs +0 -32
  32. package/dist/runtime/siteConfig/util.d.ts +0 -2
  33. package/dist/runtime/siteConfig/util.mjs +0 -7
  34. package/dist/type-1234fa1d.d.ts +0 -71
  35. package/dist/types.d.ts +0 -19
package/README.md CHANGED
@@ -27,52 +27,64 @@ Shared site configuration for Nuxt 3 modules.
27
27
  </table>
28
28
  </p>
29
29
 
30
- ℹ️ Looking for a complete SEO solution? Check out [Nuxt SEO Kit](https://github.com/harlan-zw/nuxt-seo-kit).
31
-
32
30
  ## Background
33
31
 
34
32
  Site config is a general subset of configurations related to common site-wide settings.
35
33
  They are often used in many SEO and performance modules.
36
- Some examples are: site name, description, canonical URL and trailing slashes.
34
+ Some examples are: url, name, description and trailing slashes.
35
+
36
+ Usually it's expected that the end-user will configure these settings themselves for each module,
37
+ however,
38
+ as a module author it can be quite difficult to support all the ways this config is used, and the end-user experience
39
+ of not having a single source of truth becomes difficult to maintain.
40
+
41
+ Let's consider the case of a module that needs the URL of the site.
42
+
43
+ The module can make use of the SSR utilities to get the URL from the request headers at runtime, which is great!
37
44
 
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?
45
+ But what if:
46
+ - The URL is needed when prerendering or at build time?
47
+ - The URL needs to conditionally swap at runtime (such as a multi-tenant app or i18n seperated sites)
48
+ - The URL from the headers is not the canonical URL?
43
49
 
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).
50
+ Now imagine this problem for every module that needs this config, and the config extends beyond just the URL.
48
51
 
49
- Things start getting complicated.
52
+ ## Solution
50
53
 
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
+ We need:
55
+ - The user to be able to configure this URL at build time and runtime for any modules which need it.
56
+ - The data source to remain in sync across runtimes.
57
+ - A hierarchy of config sources, so that we can provide intelligent defaults and overrides. Relying on environment data where possible.
58
+ - A generic API for modules to use this config.
59
+
60
+ The solution is a subset of the App config functionality, except with build-time support.
61
+ The underlying implementation
62
+ is currently bespoke, but it will likely be using app config directly in the future
63
+ (once SSR runtime syncing is supported).
54
64
 
55
65
  ## Features
56
66
 
57
- - 😌 Zero-config defaults from environment: site URL, name and description
67
+ - 😌 Zero-config defaults: URL, name and description
58
68
  - 🎨 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
69
+ - 🤖 Smart stackable overrides for build-time and runtime with ledger capabilities
70
+ - Safe fallbacks with runtime assertions
71
+ - Site-config based composables for modules to use: `resolveTrailingSlash`, `useNitroOrigin`, etc.
62
72
 
63
73
  ## Install
64
74
 
75
+ :Warn: This module is experimental. The default supported site config keys are subject to change based on feedback.
76
+
65
77
  ```bash
78
+ #
66
79
  npm install nuxt-site-config
67
-
68
- # Using yarn
80
+ #
69
81
  yarn add nuxt-site-config
82
+ #
83
+ pnpm add nuxt-site-config
70
84
  ```
71
85
 
72
86
  ## Setup
73
87
 
74
- **Modules**
75
-
76
88
  ```ts
77
89
  export default defineNuxtModule<ModuleOptions>({
78
90
  async setup(config, nuxt) {
@@ -82,18 +94,6 @@ export default defineNuxtModule<ModuleOptions>({
82
94
  })
83
95
  ```
84
96
 
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
97
  ## Config Schema
98
98
 
99
99
  ```ts
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "nuxt-site-config",
3
3
  "type": "module",
4
- "version": "0.6.0",
5
- "packageManager": "pnpm@8.6.3",
4
+ "version": "0.7.3",
5
+ "packageManager": "pnpm@8.6.5",
6
6
  "description": "Shared site configuration for Nuxt 3 modules.",
7
7
  "license": "MIT",
8
8
  "funding": "https://github.com/sponsors/harlan-zw",
@@ -30,31 +30,25 @@
30
30
  "files": [
31
31
  "dist"
32
32
  ],
33
- "dependencies": {
34
- "@nuxt/kit": "3.6.1",
35
- "defu": "^6.1.2",
36
- "pkg-types": "^1.0.3",
37
- "ufo": "^1.1.2"
38
- },
39
33
  "devDependencies": {
40
- "@antfu/eslint-config": "^0.39.5",
34
+ "@antfu/eslint-config": "^0.39.6",
41
35
  "@nuxt/module-builder": "^0.4.0",
42
36
  "@nuxt/test-utils": "3.6.1",
43
37
  "@nuxtjs/eslint-config-typescript": "^12.0.0",
44
- "@types/node": "^18",
38
+ "@types/node": "^20.3.3",
45
39
  "bumpp": "^9.1.1",
46
- "eslint": "8.43.0",
40
+ "eslint": "8.44.0",
47
41
  "execa": "^7.1.1",
48
42
  "nuxt": "^3.6.1",
49
43
  "vitest": "^0.32.2"
50
44
  },
51
45
  "scripts": {
52
46
  "lint": "eslint . --fix",
53
- "build": "nuxt-module-build --stub && nuxt-module-build prepare && nuxt-module-build",
47
+ "build": "pnpm --filter='./packages/*' build",
54
48
  "dev": "nuxi dev playground",
55
49
  "dev:build": "nuxi build playground",
56
50
  "dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
57
- "release": "bumpp package.json --commit --push --tag",
51
+ "release": "bumpp package.json packages/*/package.json --commit --push --tag",
58
52
  "test": "vitest"
59
53
  }
60
54
  }
package/dist/kit.d.ts DELETED
@@ -1,16 +0,0 @@
1
- import { S as SiteConfigStack, a as SiteConfigInput, b as SiteConfig, A as AssertionModes } from './type-1234fa1d.js';
2
-
3
- declare function initSiteConfig(): Promise<SiteConfigStack | undefined>;
4
- declare function updateSiteConfig(input: SiteConfigInput): Promise<void>;
5
- declare function useSiteConfig(): Promise<SiteConfig>;
6
-
7
- declare function requireSiteConfig(context: string, requirements: Partial<Record<keyof SiteConfig, string>>, modes: Partial<Record<AssertionModes, boolean>>): void;
8
- declare function assertSiteConfig(mode: AssertionModes, options?: {
9
- throwError?: boolean;
10
- logErrors?: boolean;
11
- }): Promise<{
12
- valid: boolean;
13
- messages: string[];
14
- } | undefined>;
15
-
16
- export { assertSiteConfig, initSiteConfig, requireSiteConfig, updateSiteConfig, useSiteConfig };
package/dist/kit.mjs DELETED
@@ -1,179 +0,0 @@
1
- import { tryUseNuxt, useLogger } from '@nuxt/kit';
2
- import { readPackageJSON } from 'pkg-types';
3
-
4
- function createSiteConfigStack() {
5
- const stack = [];
6
- function push(input) {
7
- if (!input._context) {
8
- let lastFunctionName = new Error("tmp").stack?.split("\n")[2].split(" ")[5];
9
- if (lastFunctionName?.includes("/"))
10
- lastFunctionName = "anonymous";
11
- input._context = lastFunctionName;
12
- }
13
- stack.push(input);
14
- }
15
- function get() {
16
- const siteConfig = {
17
- _context: {}
18
- };
19
- for (const o in stack) {
20
- for (const k in stack[o]) {
21
- const val = stack[o][k];
22
- if (!k.endsWith("context") && typeof val !== "undefined") {
23
- siteConfig[k] = val;
24
- siteConfig._context[k] = stack[o]._context?.[k] || stack[o]._context || "anonymous";
25
- }
26
- }
27
- }
28
- return normalizeSiteConfig(siteConfig);
29
- }
30
- return {
31
- push,
32
- get
33
- };
34
- }
35
-
36
- function normalizeSiteConfig(config) {
37
- if (typeof config.indexable !== "undefined")
38
- config.indexable = String(config.indexable) !== "false";
39
- if (typeof config.trailingSlash !== "undefined")
40
- config.trailingSlash = String(config.trailingSlash) !== "false";
41
- return config;
42
- }
43
-
44
- const processShim = typeof process !== "undefined" ? process : {};
45
- const envShim = processShim.env || {};
46
- const envSiteConfig = {
47
- _context: "env",
48
- url: [
49
- envShim.NUXT_PUBLIC_VERCEL_URL,
50
- // vercel
51
- envShim.NUXT_PUBLIC_URL,
52
- // netlify
53
- envShim.NUXT_PUBLIC_CF_PAGES_URL,
54
- // cloudflare pages
55
- envShim.NUXT_PUBLIC_SITE_URL
56
- // nuxt-site-config
57
- ].find((k) => Boolean(k)),
58
- name: envShim.NUXT_PUBLIC_SITE_NAME,
59
- description: envShim.NUXT_PUBLIC_SITE_DESCRIPTION,
60
- logo: envShim.NUXT_PUBLIC_SITE_IMAGE,
61
- indexable: envShim.NUXT_PUBLIC_SITE_INDEXABLE || envShim.NUXT_PUBLIC_SITE_INDEX,
62
- titleSeparator: envShim.NUXT_PUBLIC_SITE_TITLE_SEPARATOR,
63
- trailingSlash: envShim.NUXT_PUBLIC_SITE_TRAILING_SLASH,
64
- locale: envShim.NUXT_PUBLIC_SITE_LANGUAGE || envShim.NUXT_PUBLIC_SITE_LOCALE
65
- };
66
-
67
- async function getPkgJsonContextConfig(rootDir) {
68
- const pkgJson = await readPackageJSON(void 0, { startingFrom: rootDir });
69
- if (!pkgJson)
70
- return {};
71
- return {
72
- _context: "package.json",
73
- name: pkgJson.name,
74
- description: pkgJson.description
75
- };
76
- }
77
- async function initSiteConfig() {
78
- const nuxt = tryUseNuxt();
79
- if (!nuxt)
80
- return;
81
- let siteConfig = nuxt._siteConfig;
82
- if (siteConfig)
83
- return siteConfig;
84
- const rootDir = nuxt?.options.rootDir || process.cwd();
85
- siteConfig = createSiteConfigStack();
86
- const isNodeEnv = !!process.env.NODE_ENV;
87
- siteConfig.push({
88
- _context: "system",
89
- name: rootDir.split("/").pop(),
90
- indexable: isNodeEnv ? process.env.NODE_ENV === "production" : !process.dev
91
- });
92
- siteConfig.push(await getPkgJsonContextConfig(rootDir));
93
- siteConfig.push(envSiteConfig);
94
- const runtimeConfig = nuxt.options.runtimeConfig;
95
- function getRuntimeConfig(config, env) {
96
- if (process.env[`NUXT_${env}}`])
97
- return process.env[`NUXT_${env}}`];
98
- if (process.env[`NUXT_PUBLIC_${env}}`])
99
- return process.env[`NUXT_PUBLIC_${env}}`];
100
- return runtimeConfig[`site${config}`] || runtimeConfig.public?.[`site${config}`];
101
- }
102
- siteConfig.push({
103
- _context: "legacyRuntimeConfig",
104
- url: getRuntimeConfig("Url", "_URL"),
105
- name: getRuntimeConfig("Name", "_NAME"),
106
- description: getRuntimeConfig("Description", "_DESCRIPTION"),
107
- logo: getRuntimeConfig("Image", "_IMAGE"),
108
- locale: getRuntimeConfig("Language", "_LANGUAGE"),
109
- indexable: getRuntimeConfig("Indexable", "_INDEXABLE")
110
- });
111
- siteConfig.push({
112
- _context: "runtimeConfig",
113
- ...nuxt?.options.runtimeConfig.public.site || {}
114
- });
115
- nuxt._siteConfig = siteConfig;
116
- return siteConfig;
117
- }
118
- async function getSiteConfigStack() {
119
- const lastFunctionName = new Error("tmp").stack?.split("\n")[2].split(" ")[5];
120
- const container = await initSiteConfig();
121
- if (!container)
122
- throw new Error(`Site config isn't initialized. Make sure you're calling \`${lastFunctionName}\` within the Nuxt context.`);
123
- return container;
124
- }
125
- async function updateSiteConfig(input) {
126
- const container = await getSiteConfigStack();
127
- container.push(input);
128
- }
129
- async function useSiteConfig() {
130
- const container = await getSiteConfigStack();
131
- return container.get();
132
- }
133
-
134
- function requireSiteConfig(context, requirements, modes) {
135
- const nuxt = tryUseNuxt();
136
- if (!nuxt)
137
- return;
138
- const assertions = nuxt._siteConfigAsserts || {};
139
- Object.keys(modes).forEach((mode) => {
140
- const key = mode;
141
- if (!modes[key])
142
- return;
143
- assertions[key] = assertions[key] || [];
144
- assertions[key].push({ context, requirements });
145
- });
146
- nuxt._siteConfigAsserts = assertions;
147
- }
148
- async function assertSiteConfig(mode, options) {
149
- const nuxt = tryUseNuxt();
150
- if (!nuxt)
151
- return;
152
- let valid = true;
153
- const messages = [];
154
- const logger = useLogger("nuxt-site-config");
155
- const assertions = nuxt._siteConfigAsserts?.[mode] || false;
156
- if (!assertions)
157
- return { valid, messages };
158
- const siteConfig = await useSiteConfig();
159
- assertions.forEach(({ context, requirements }) => {
160
- Object.keys(requirements).forEach((k) => {
161
- const key = k;
162
- if (!siteConfig[key]) {
163
- const msg = `\`${context}\` requires \`${key}\` to be set. ${requirements[key]}`;
164
- messages.push(msg);
165
- if (options?.logErrors !== false)
166
- logger.error(msg);
167
- valid = false;
168
- }
169
- });
170
- });
171
- if (!valid && options?.throwError !== false)
172
- throw new Error(`Missing site config for ${mode} mode.`);
173
- return {
174
- valid,
175
- messages
176
- };
177
- }
178
-
179
- export { assertSiteConfig, initSiteConfig, requireSiteConfig, updateSiteConfig, useSiteConfig };
package/dist/module.cjs DELETED
@@ -1,5 +0,0 @@
1
- module.exports = function(...args) {
2
- return import('./module.mjs').then(m => m.default.call(this, ...args))
3
- }
4
- const _meta = module.exports.meta = require('./module.json')
5
- module.exports.getMeta = () => Promise.resolve(_meta)
package/dist/module.d.ts DELETED
@@ -1,41 +0,0 @@
1
- import * as _nuxt_schema from '@nuxt/schema';
2
- import { S as SiteConfigStack, a as SiteConfigInput, b as SiteConfig } from './type-1234fa1d.js';
3
- import { AssertionModes, ModuleAssertion } from '~/src/type';
4
-
5
- interface ModuleOptions extends SiteConfigInput {
6
- }
7
- interface ModulePublicRuntimeConfig {
8
- site: SiteConfigInput;
9
- }
10
- declare module 'h3' {
11
- interface H3EventContext {
12
- siteConfig: SiteConfigStack;
13
- }
14
- }
15
- declare module 'nuxt/schema' {
16
- interface AppConfigInput {
17
- /** Theme configuration */
18
- site?: SiteConfigInput;
19
- }
20
- }
21
- declare module '@nuxt/schema' {
22
- interface AppConfigInput {
23
- /** Theme configuration */
24
- site?: SiteConfigInput;
25
- }
26
- interface Nuxt {
27
- _siteConfig?: SiteConfigStack;
28
- _siteConfigAsserts?: Partial<Record<Partial<AssertionModes>, ModuleAssertion[]>>;
29
- }
30
- }
31
- declare module '@nuxt/schema' {
32
- interface RuntimeNuxtHooks {
33
- 'site-config:resolve': (siteConfig: SiteConfig) => void;
34
- }
35
- }
36
- interface ModuleHooks {
37
- 'site-config:resolve': (siteConfig: SiteConfig) => void;
38
- }
39
- declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
40
-
41
- export { ModuleHooks, ModuleOptions, ModulePublicRuntimeConfig, _default as default };
package/dist/module.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "name": "nuxt-site-config",
3
- "compatibility": {
4
- "nuxt": "^3.6.0",
5
- "bridge": false
6
- },
7
- "configKey": "site",
8
- "version": "0.6.0"
9
- }
package/dist/module.mjs DELETED
@@ -1,83 +0,0 @@
1
- import { defineNuxtModule, createResolver, addImports, addComponent, addPlugin, addServerHandler } from '@nuxt/kit';
2
- import { updateSiteConfig, useSiteConfig, assertSiteConfig } from './kit.mjs';
3
- import 'pkg-types';
4
-
5
- const module = defineNuxtModule({
6
- meta: {
7
- name: "nuxt-site-config",
8
- compatibility: {
9
- nuxt: "^3.6.0",
10
- bridge: false
11
- },
12
- configKey: "site"
13
- },
14
- async setup(config, nuxt) {
15
- const { resolve } = createResolver(import.meta.url);
16
- const composables = ["useSiteConfig", "updateSiteConfig"];
17
- composables.forEach((c) => {
18
- addImports({
19
- from: resolve(`./runtime/composables/${c}`),
20
- name: c
21
- });
22
- });
23
- nuxt.hook("modules:done", async () => {
24
- await updateSiteConfig({
25
- _context: "nuxt:config:site",
26
- ...config
27
- });
28
- const siteConfig = await useSiteConfig();
29
- await nuxt.callHook("site-config:resolve", siteConfig);
30
- nuxt.options.runtimeConfig.public.site = siteConfig;
31
- });
32
- nuxt.hooks.hook("nitro:init", async (nitro) => {
33
- nitro.hooks.hookOnce("prerender:generate", async () => {
34
- await assertSiteConfig("prerender");
35
- });
36
- });
37
- const linkComposables = ["createInternalLinkResolver", "resolveAbsoluteInternalLink", "resolveTrailingSlash"];
38
- linkComposables.forEach((c) => {
39
- addImports({
40
- from: resolve("./runtime/composables/utils"),
41
- name: c
42
- });
43
- });
44
- addImports({
45
- from: resolve("./runtime/nitro/composables/useNitroOrigin"),
46
- name: "useNitroOrigin"
47
- });
48
- await addComponent({
49
- filePath: resolve("./runtime/component/SiteLink.vue"),
50
- name: "SiteLink"
51
- });
52
- const shared = resolve("./runtime/siteConfig");
53
- nuxt.options.build.transpile.push(shared);
54
- nuxt.options.nitro.imports = nuxt.options.nitro.imports || {};
55
- nuxt.options.nitro.imports.imports = nuxt.options.nitro.imports.imports || [];
56
- nuxt.options.nitro.imports.imports.push(...[
57
- {
58
- as: "useSiteConfig",
59
- name: "useSiteConfig",
60
- from: resolve("./runtime/nitro/composables/useSiteConfig")
61
- },
62
- {
63
- as: "useNitroOrigin",
64
- name: "useNitroOrigin",
65
- from: resolve("./runtime/nitro/composables/useNitroOrigin")
66
- },
67
- {
68
- as: "updateSiteConfig",
69
- name: "updateSiteConfig",
70
- from: resolve("./runtime/nitro/composables/updateSiteConfig")
71
- }
72
- ]);
73
- addPlugin({
74
- src: resolve("./runtime/plugins/siteConfig")
75
- });
76
- addServerHandler({
77
- middleware: true,
78
- handler: resolve("./runtime/nitro/middleware/init")
79
- });
80
- }
81
- });
82
-
83
- export { module as default };
@@ -1,33 +0,0 @@
1
- <script lang="ts" setup>
2
- // we need to intercept the `to` property and make sure that we have the right trailing slash
3
- import { computed, createInternalLinkResolver, defineProps } from '#imports'
4
-
5
- const props = defineProps({
6
- to: {
7
- type: String,
8
- required: true,
9
- },
10
- })
11
-
12
- const linkResolver = createInternalLinkResolver()
13
-
14
- const to = computed(() => {
15
- // only for relative links
16
- if (props.to !== '/' && props.to.startsWith('/'))
17
- return linkResolver(props.to)
18
- return props.to
19
- })
20
-
21
- const attrs = computed(() => {
22
- return {
23
- ...props,
24
- to: to.value,
25
- }
26
- })
27
- </script>
28
-
29
- <template>
30
- <NuxtLink v-bind="{ ...$attrs, ...attrs }">
31
- <slot />
32
- </NuxtLink>
33
- </template>
@@ -1,2 +0,0 @@
1
- import type { SiteConfigInput } from '../../type';
2
- export declare function updateSiteConfig(input?: SiteConfigInput): void;
@@ -1,10 +0,0 @@
1
- import { useNuxtApp, useRequestEvent } from "#imports";
2
- export function updateSiteConfig(input = {}) {
3
- if (process.server) {
4
- const stack2 = useRequestEvent().context.siteConfig;
5
- stack2.push(input);
6
- return;
7
- }
8
- const stack = useNuxtApp().$siteConfig;
9
- stack.push(input);
10
- }
@@ -1 +0,0 @@
1
- export declare function useSiteConfig(): import("../../type").SiteConfig;
@@ -1,12 +0,0 @@
1
- import {
2
- useNuxtApp,
3
- useRequestEvent
4
- } from "#imports";
5
- export function useSiteConfig() {
6
- if (process.server) {
7
- const stack2 = useRequestEvent().context.siteConfig;
8
- return stack2.get();
9
- }
10
- const stack = useNuxtApp().$siteConfig;
11
- return stack.get();
12
- }
@@ -1,5 +0,0 @@
1
- export declare function resolveTrailingSlash(path: string): any;
2
- export declare function resolveAbsoluteInternalLink(relativeInternalLink: string): any;
3
- export declare function createInternalLinkResolver(options?: {
4
- absolute?: boolean;
5
- }): (path: string) => string;
@@ -1,30 +0,0 @@
1
- import { withBase, withTrailingSlash, withoutTrailingSlash } from "ufo";
2
- import { computed, useSiteConfig } from "#imports";
3
- function fixSlashes(trailingSlash, path) {
4
- return trailingSlash ? withTrailingSlash(path) : withoutTrailingSlash(path);
5
- }
6
- export function resolveTrailingSlash(path) {
7
- const siteConfig = useSiteConfig();
8
- return computed(() => {
9
- if (typeof siteConfig.trailingSlash === "boolean")
10
- return fixSlashes(siteConfig.trailingSlash, path);
11
- return path;
12
- });
13
- }
14
- export function resolveAbsoluteInternalLink(relativeInternalLink) {
15
- const siteConfig = useSiteConfig();
16
- const slashes = resolveTrailingSlash(relativeInternalLink);
17
- return computed(() => {
18
- return withBase(slashes.value, siteConfig.url || "/");
19
- });
20
- }
21
- export function createInternalLinkResolver(options = {}) {
22
- const siteConfig = useSiteConfig();
23
- return (path) => {
24
- if (typeof siteConfig.trailingSlash === "boolean")
25
- path = fixSlashes(siteConfig.trailingSlash, path);
26
- if (!options.absolute)
27
- return path;
28
- return withBase(path, siteConfig.url || "/");
29
- };
30
- }
@@ -1,3 +0,0 @@
1
- import type { H3Event } from 'h3';
2
- import type { SiteConfigInput } from '../../../type';
3
- export declare function updateSiteConfig(e: H3Event, input: SiteConfigInput): void;
@@ -1,5 +0,0 @@
1
- import { createSiteConfigStack } from "../../siteConfig/index.mjs";
2
- export function updateSiteConfig(e, input) {
3
- e.context.siteConfig = e.context.siteConfig || createSiteConfigStack();
4
- e.context.siteConfig.push(input);
5
- }
@@ -1,2 +0,0 @@
1
- import type { H3Event } from 'h3';
2
- export declare function useNitroOrigin(e?: H3Event): string;
@@ -1,25 +0,0 @@
1
- import { withoutProtocol } from "ufo";
2
- import { getRequestHost, getRequestProtocol } from "h3";
3
- export function useNitroOrigin(e) {
4
- const cert = process.env.NITRO_SSL_CERT;
5
- const key = process.env.NITRO_SSL_KEY;
6
- let host = process.env.NITRO_HOST || process.env.HOST || false;
7
- let port = process.env.NITRO_PORT || process.env.PORT || (process.dev ? 3e3 : false);
8
- let protocol = cert && key || !process.dev ? "https" : "http";
9
- if (!e) {
10
- if ((process.dev || process.env.prerender) && process.env.NUXT_VITE_NODE_OPTIONS) {
11
- const origin = JSON.parse(process.env.NUXT_VITE_NODE_OPTIONS).baseURL.replace("/__nuxt_vite_node__", "");
12
- host = withoutProtocol(origin);
13
- protocol = origin.includes("https") ? "https" : "http";
14
- }
15
- } else {
16
- host = getRequestHost(e, { xForwardedHost: true }) || host;
17
- protocol = getRequestProtocol(e, { xForwardedProto: true }) || protocol;
18
- }
19
- if (typeof host === "string" && host.includes(":")) {
20
- port = host.split(":").pop();
21
- host = host.split(":")[0];
22
- }
23
- port = port ? `:${port}` : "";
24
- return `${protocol}://${host}${port}/`;
25
- }
@@ -1,2 +0,0 @@
1
- import type { H3Event } from 'h3';
2
- export declare function useSiteConfig(e: H3Event): any;
@@ -1,5 +0,0 @@
1
- import { createSiteConfigStack } from "../../siteConfig/index.mjs";
2
- export function useSiteConfig(e) {
3
- e.context.siteConfig = e.context.siteConfig || createSiteConfigStack();
4
- return e.context.siteConfig.get();
5
- }
@@ -1,2 +0,0 @@
1
- declare const _default: any;
2
- export default _default;
@@ -1,13 +0,0 @@
1
- import { defu } from "defu";
2
- import { joinURL } from "ufo";
3
- import { eventHandler, updateSiteConfig, useAppConfig, useNitroOrigin, useRuntimeConfig } from "#imports";
4
- export default eventHandler((e) => {
5
- if (!e.context.siteConfig) {
6
- const appConfig = useAppConfig();
7
- const { public: publicRuntimeConfig, app } = useRuntimeConfig();
8
- updateSiteConfig(e, defu(appConfig.site, publicRuntimeConfig.site, {
9
- // fallback to the origin
10
- url: joinURL(useNitroOrigin(e), app.baseURL)
11
- }));
12
- }
13
- });
@@ -1,2 +0,0 @@
1
- declare const _default: any;
2
- export default _default;
@@ -1,28 +0,0 @@
1
- import { createSiteConfigStack } from "../siteConfig/index.mjs";
2
- import { defineNuxtPlugin, useRequestEvent, useState } from "#imports";
3
- export default defineNuxtPlugin({
4
- name: "nuxt-site-config",
5
- enforce: "pre",
6
- // or 'post'
7
- async setup(nuxtApp) {
8
- let siteConfigStack;
9
- if (process.server) {
10
- siteConfigStack = useRequestEvent().context.siteConfig;
11
- nuxtApp.hooks.hook("app:rendered", () => {
12
- useState("site-config", () => useRequestEvent().context.siteConfig.get());
13
- });
14
- }
15
- if (!siteConfigStack)
16
- siteConfigStack = createSiteConfigStack();
17
- if (process.client) {
18
- const state = useState("site-config");
19
- if (state)
20
- siteConfigStack.push(state.value);
21
- }
22
- return {
23
- provide: {
24
- siteConfig: siteConfigStack
25
- }
26
- };
27
- }
28
- });
@@ -1,2 +0,0 @@
1
- import type { SiteConfigInput } from '../../type';
2
- export declare const envSiteConfig: SiteConfigInput;
@@ -1,22 +0,0 @@
1
- const processShim = typeof process !== "undefined" ? process : {};
2
- const envShim = processShim.env || {};
3
- export const envSiteConfig = {
4
- _context: "env",
5
- url: [
6
- envShim.NUXT_PUBLIC_VERCEL_URL,
7
- // vercel
8
- envShim.NUXT_PUBLIC_URL,
9
- // netlify
10
- envShim.NUXT_PUBLIC_CF_PAGES_URL,
11
- // cloudflare pages
12
- envShim.NUXT_PUBLIC_SITE_URL
13
- // nuxt-site-config
14
- ].find((k) => Boolean(k)),
15
- name: envShim.NUXT_PUBLIC_SITE_NAME,
16
- description: envShim.NUXT_PUBLIC_SITE_DESCRIPTION,
17
- logo: envShim.NUXT_PUBLIC_SITE_IMAGE,
18
- indexable: envShim.NUXT_PUBLIC_SITE_INDEXABLE || envShim.NUXT_PUBLIC_SITE_INDEX,
19
- titleSeparator: envShim.NUXT_PUBLIC_SITE_TITLE_SEPARATOR,
20
- trailingSlash: envShim.NUXT_PUBLIC_SITE_TRAILING_SLASH,
21
- locale: envShim.NUXT_PUBLIC_SITE_LANGUAGE || envShim.NUXT_PUBLIC_SITE_LOCALE
22
- };
@@ -1,2 +0,0 @@
1
- export * from './stack';
2
- export * from './util';
@@ -1,2 +0,0 @@
1
- export * from "./stack.mjs";
2
- export * from "./util.mjs";
@@ -1,2 +0,0 @@
1
- import type { SiteConfigStack } from '../../type';
2
- export declare function createSiteConfigStack(): SiteConfigStack;
@@ -1,32 +0,0 @@
1
- import { normalizeSiteConfig } from ".//index.mjs";
2
- export function createSiteConfigStack() {
3
- const stack = [];
4
- function push(input) {
5
- if (!input._context) {
6
- let lastFunctionName = new Error("tmp").stack?.split("\n")[2].split(" ")[5];
7
- if (lastFunctionName?.includes("/"))
8
- lastFunctionName = "anonymous";
9
- input._context = lastFunctionName;
10
- }
11
- stack.push(input);
12
- }
13
- function get() {
14
- const siteConfig = {
15
- _context: {}
16
- };
17
- for (const o in stack) {
18
- for (const k in stack[o]) {
19
- const val = stack[o][k];
20
- if (!k.endsWith("context") && typeof val !== "undefined") {
21
- siteConfig[k] = val;
22
- siteConfig._context[k] = stack[o]._context?.[k] || stack[o]._context || "anonymous";
23
- }
24
- }
25
- }
26
- return normalizeSiteConfig(siteConfig);
27
- }
28
- return {
29
- push,
30
- get
31
- };
32
- }
@@ -1,2 +0,0 @@
1
- import type { SiteConfig, SiteConfigInput } from '../../type';
2
- export declare function normalizeSiteConfig(config: SiteConfigInput): SiteConfig;
@@ -1,7 +0,0 @@
1
- export function normalizeSiteConfig(config) {
2
- if (typeof config.indexable !== "undefined")
3
- config.indexable = String(config.indexable) !== "false";
4
- if (typeof config.trailingSlash !== "undefined")
5
- config.trailingSlash = String(config.trailingSlash) !== "false";
6
- return config;
7
- }
@@ -1,71 +0,0 @@
1
- interface SiteConfig {
2
- /**
3
- * The canonical Site URL.
4
- *
5
- * @default `process.env.NUXT_PUBLIC_SITE_URL`
6
- *
7
- * - Build / Prerender: Inferred from CI environment (Netlify, Vercel)
8
- * - SSR: Inferred from request headers
9
- * - SPA: Inferred from `window.location`
10
- *
11
- * Used by: nuxt-simple-sitemap, nuxt-simple-robots, nuxt-schema-org, nuxt-og-image, etc.
12
- */
13
- url?: string;
14
- titleSeparator?: string;
15
- indexable?: boolean;
16
- trailingSlash?: boolean;
17
- locale?: string;
18
- /**
19
- * The name of the site.
20
- *
21
- * @default `process.env.NUXT_PUBLIC_SITE_NAME`
22
- *
23
- * - Build / Prerender: Inferred from CI environment (Netlify) or `package.json`
24
- * - SSR:
25
- *
26
- * Used by: nuxt-schema-org, nuxt-seo-kit
27
- */
28
- name?: string;
29
- /**
30
- * The description of the site.
31
- *
32
- * @default `process.env.NUXT_PUBLIC_SITE_DESCRIPTION`
33
- *
34
- * Used by: nuxt-schema-org, nuxt-seo-kit
35
- */
36
- description?: string;
37
- /**
38
- * The logo of the site.
39
- *
40
- * @default `process.env.NUXT_PUBLIC_SITE_LOGO`
41
- *
42
- * Used by: nuxt-schema-org, nuxt-seo-kit
43
- */
44
- logo?: string;
45
- /**
46
- * The mapping of the context of each site config value being set.
47
- */
48
- _context: Partial<Record<Exclude<keyof SiteConfig, '_meta'>, string>>;
49
- }
50
- interface SiteConfigInput {
51
- /**
52
- * A description of the context which added the config.
53
- */
54
- _context?: string;
55
- url?: string;
56
- name?: string;
57
- description?: string;
58
- logo?: string;
59
- coverImage?: string;
60
- titleSeparator?: string;
61
- locale?: string;
62
- indexable?: boolean | string;
63
- trailingSlash?: boolean | string;
64
- }
65
- interface SiteConfigStack {
66
- push: (config: SiteConfigInput | SiteConfig) => void;
67
- get: () => SiteConfig;
68
- }
69
- type AssertionModes = 'prerender' | 'generate' | 'build';
70
-
71
- export { AssertionModes as A, SiteConfigStack as S, SiteConfigInput as a, SiteConfig as b };
package/dist/types.d.ts DELETED
@@ -1,19 +0,0 @@
1
-
2
- import { ModuleOptions, ModuleHooks, ModulePublicRuntimeConfig } from './module'
3
-
4
- declare module '@nuxt/schema' {
5
- interface NuxtConfig { ['site']?: Partial<ModuleOptions> }
6
- interface NuxtOptions { ['site']?: ModuleOptions }
7
- interface NuxtHooks extends ModuleHooks {}
8
- interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
9
- }
10
-
11
- declare module 'nuxt/schema' {
12
- interface NuxtConfig { ['site']?: Partial<ModuleOptions> }
13
- interface NuxtOptions { ['site']?: ModuleOptions }
14
- interface NuxtHooks extends ModuleHooks {}
15
- interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
16
- }
17
-
18
-
19
- export { ModuleHooks, ModuleOptions, ModulePublicRuntimeConfig, default } from './module'