Skip to content

Commit 81c613c

Browse files
committed
fix: remove duplication of configs
Fix #1411
1 parent 8b8b26e commit 81c613c

File tree

12 files changed

+95
-135
lines changed

12 files changed

+95
-135
lines changed

packages/nuxt/playground/nuxt.config.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { fileURLToPath } from 'node:url'
2-
import { resolve } from 'node:path'
32

43
export default defineNuxtConfig({
54
app: {
@@ -12,14 +11,12 @@ export default defineNuxtConfig({
1211
},
1312

1413
nitro: {
15-
preset: fileURLToPath(new URL('./preset', import.meta.url)),
16-
// preset: 'nitro-preset-firebase',
17-
runtimeConfig: {
18-
firebase: {
19-
functions: {
20-
httpsOptions: {
21-
region: 'europe-west1',
22-
},
14+
preset: 'firebase',
15+
firebase: {
16+
gen: 2,
17+
functions: {
18+
httpsOptions: {
19+
region: 'europe-west1',
2320
},
2421
},
2522
},

packages/nuxt/playground/preset/entry.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/nuxt/playground/preset/nitro.config.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

packages/nuxt/src/module.ts

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -42,56 +42,62 @@ export default defineNuxtModule<VueFireNuxtModuleOptions>({
4242
emulators: { enabled: true },
4343
},
4444

45-
async setup(options, nuxt) {
45+
async setup(_options, nuxt) {
4646
// ensure provided options are valid
47-
if (!options.config) {
47+
if (!_options.config) {
4848
throw new Error(
4949
'[nuxt-vuefire]: Missing firebase config. Provide a "config" option to the VueFire module options.'
5050
)
5151
}
5252

53-
const { resolve } = createResolver(import.meta.url)
54-
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
55-
const templatesDir = fileURLToPath(new URL('../templates', import.meta.url))
56-
57-
// TODO: I don't think the appConfig is the right place to store these as it makes things reactive
58-
// Let plugins and the user access the firebase config within the app
59-
nuxt.options.appConfig.firebaseConfig = markRaw(options.config)
60-
nuxt.options.appConfig.vuefireOptions = markRaw(options)
61-
53+
// resolve options
6254
const isAuthEnabled =
63-
typeof options.auth === 'object'
64-
? options.auth.enabled ?? true // allows user to comment out enabled: false
65-
: !!options.auth
66-
67-
const resolvedVueFireOptions = {
68-
...options,
55+
typeof _options.auth === 'object'
56+
? _options.auth.enabled ?? true // allows user to comment out enabled: false
57+
: !!_options.auth
58+
59+
const options = {
60+
..._options,
61+
// NOTE: TS complains otherwise
62+
config: _options.config,
6963
// ensure the resolved version easier to consume
7064
emulators: {
7165
enabled:
72-
typeof options.emulators === 'object'
73-
? options.emulators.enabled ?? true // allows user to comment out enabled: false
74-
: !!options.emulators,
75-
...(typeof options.emulators === 'object' ? options.emulators : {}),
66+
typeof _options.emulators === 'object'
67+
? _options.emulators.enabled ?? true // allows user to comment out enabled: false
68+
: !!_options.emulators,
69+
...(typeof _options.emulators === 'object' ? _options.emulators : {}),
7670
},
7771
auth: {
7872
enabled: isAuthEnabled,
7973
// enable session cookie when auth is `true`
8074
sessionCookie:
81-
typeof options.auth === 'object'
82-
? isAuthEnabled && options.auth.sessionCookie // deactivating auth also deactivates the session cookie
83-
: !!options.auth, // fallback to the boolean value of options.auth
84-
...(typeof options.auth === 'object' ? options.auth : {}),
75+
typeof _options.auth === 'object'
76+
? isAuthEnabled && _options.auth.sessionCookie // deactivating auth also deactivates the session cookie
77+
: !!_options.auth, // fallback to the boolean value of options.auth
78+
...(typeof _options.auth === 'object' ? _options.auth : {}),
8579
},
8680
} satisfies VueFireNuxtModuleOptionsResolved
8781

88-
nuxt.options.runtimeConfig.vuefire = {
89-
options: resolvedVueFireOptions,
90-
}
82+
nuxt.options.runtimeConfig.public.vuefire ??= {}
83+
// avoid any nested reactivity as it's not needed
84+
markRaw(nuxt.options.runtimeConfig.public.vuefire)
85+
// Let plugins and the user access the firebase config within the app
86+
nuxt.options.runtimeConfig.public.vuefire.config = _options.config
87+
nuxt.options.runtimeConfig.public.vuefire.appCheck = options.appCheck
88+
89+
nuxt.options.runtimeConfig.vuefire ??= {}
90+
markRaw(nuxt.options.runtimeConfig.vuefire)
91+
nuxt.options.runtimeConfig.vuefire.admin ??= options.admin
92+
93+
// configure transpilation
94+
const { resolve } = createResolver(import.meta.url)
95+
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
96+
const templatesDir = fileURLToPath(new URL('../templates', import.meta.url))
9197

9298
// we need this to avoid some warnings about missing credentials and ssr
9399
const emulatorsConfig = await willUseEmulators(
94-
nuxt.options.runtimeConfig.vuefire.options!,
100+
options,
95101
resolve(nuxt.options.rootDir, 'firebase.json'),
96102
logger
97103
)
@@ -104,6 +110,7 @@ export default defineNuxtModule<VueFireNuxtModuleOptions>({
104110

105111
// This one is set by servers, we set the GOOGLE_APPLICATION_CREDENTIALS env variable instead that has a lower priority and can be both a path or a JSON string
106112
// process.env.FIREBASE_CONFIG ||= JSON.stringify(options.config)
113+
// FIXME: remove deprecation in next release
107114
if (typeof options.admin?.serviceAccount === 'string') {
108115
process.env.GOOGLE_APPLICATION_CREDENTIALS ||=
109116
options.admin.serviceAccount
@@ -173,12 +180,13 @@ export default defineNuxtModule<VueFireNuxtModuleOptions>({
173180
addPluginTemplate({
174181
src: normalize(resolve(templatesDir, 'plugin.ejs')),
175182
options: {
183+
// FIXME: not needed
176184
...options,
177185
ssr: nuxt.options.ssr,
178186
},
179187
})
180188

181-
if (options.auth) {
189+
if (_options.auth) {
182190
if (nuxt.options.ssr && !hasServiceAccount && !emulatorsConfig) {
183191
logger.warn(
184192
'You activated both SSR and auth but you are not providing a service account for the admin SDK. See https://vuefire.vuejs.org/nuxt/getting-started.html#configuring-the-admin-sdk.'
@@ -188,7 +196,7 @@ export default defineNuxtModule<VueFireNuxtModuleOptions>({
188196
if (
189197
nuxt.options.ssr &&
190198
(hasServiceAccount || emulatorsConfig) &&
191-
resolvedVueFireOptions.auth.sessionCookie
199+
options.auth.sessionCookie
192200
) {
193201
// Add the session handler than mints a cookie for the user
194202
addServerHandler({
@@ -222,19 +230,13 @@ export default defineNuxtModule<VueFireNuxtModuleOptions>({
222230
// Emulators must be enabled after the app is initialized but before some APIs like auth.signinWithCustomToken() are called
223231

224232
if (emulatorsConfig) {
225-
const emulators = detectEmulators(
226-
nuxt.options.runtimeConfig.vuefire.options!,
227-
emulatorsConfig,
228-
logger
229-
)
233+
const emulators = detectEmulators(options, emulatorsConfig, logger)
230234
// add the option to disable the warning. It only exists in Auth
231235
if (emulators?.auth) {
232-
emulators.auth.options =
233-
nuxt.options.runtimeConfig.vuefire.options?.emulators?.auth?.options
236+
emulators.auth.options = options.emulators.auth?.options
234237
}
235238

236239
// expose the detected emulators to the plugins
237-
nuxt.options.runtimeConfig.public.vuefire ??= {}
238240
nuxt.options.runtimeConfig.public.vuefire.emulators = emulators
239241

240242
for (const serviceName in emulators) {
@@ -263,7 +265,7 @@ export default defineNuxtModule<VueFireNuxtModuleOptions>({
263265
}
264266

265267
if (hasServiceAccount || emulatorsConfig) {
266-
if (resolvedVueFireOptions.auth.sessionCookie) {
268+
if (options.auth.sessionCookie) {
267269
// decodes user token from cookie if any
268270
addPlugin(resolve(runtimeDir, 'auth/plugin-user-token.server'))
269271
}
@@ -374,40 +376,40 @@ interface VueFireRuntimeConfig {
374376
*/
375377
vuefire?: {
376378
/**
377-
* Options passed to the Nuxt VueFire module
379+
* Firebase Admin SDK Options passed to the Nuxt VueFire module
378380
* @internal
379381
*/
380-
options?: VueFireNuxtModuleOptionsResolved
382+
admin?: VueFireNuxtModuleOptionsResolved['admin']
381383
}
382384
}
383385

384386
interface VueFirePublicRuntimeConfig {
387+
/**
388+
* Public Runtime config for the VueFire module.
389+
*/
385390
vuefire?: {
386391
/**
387392
* Emulators to enable.
388393
*
389394
* @internal
390395
*/
391396
emulators?: FirebaseEmulatorsToEnable
392-
}
393-
}
394397

395-
interface VueFireAppConfig {
396-
/**
397-
* Firebase config to initialize the app.
398-
* @internal
399-
*/
400-
firebaseConfig: FirebaseOptions
398+
/**
399+
* Firebase config to initialize the app.
400+
* @internal
401+
*/
402+
config?: FirebaseOptions
401403

402-
/**
403-
* VueFireNuxt options used within plugins.
404-
* @internal
405-
*/
406-
vuefireOptions: Pick<VueFireNuxtModuleOptions, 'appCheck' | 'auth'>
404+
/**
405+
* AppCheck options.
406+
* @internal
407+
*/
408+
appCheck?: VueFireNuxtModuleOptionsResolved['appCheck']
409+
}
407410
}
408411

409412
declare module '@nuxt/schema' {
410-
export interface AppConfig extends VueFireAppConfig {}
411413
export interface RuntimeConfig extends VueFireRuntimeConfig {}
412414
export interface PublicRuntimeConfig extends VueFirePublicRuntimeConfig {}
413415
}

packages/nuxt/src/module/options.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ export interface VueFireNuxtModuleOptions {
9494
}
9595

9696
export interface VueFireNuxtModuleOptionsResolved
97-
extends Omit<VueFireNuxtModuleOptions, 'emulators' | 'auth'> {
97+
extends Omit<VueFireNuxtModuleOptions, 'emulators' | 'auth' | 'config'> {
98+
config: Exclude<VueFireNuxtModuleOptions['config'], undefined>
9899
emulators: Exclude<VueFireNuxtModuleOptions['emulators'], boolean | undefined>
99100
auth: Exclude<VueFireNuxtModuleOptions['auth'], boolean | undefined>
100101
}

packages/nuxt/src/runtime/admin/plugin.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default defineNuxtPlugin(() => {
66
const event = useRequestEvent()
77
const { vuefire } = useRuntimeConfig()
88

9-
const firebaseAdminApp = ensureAdminApp(vuefire?.options?.admin?.options)
9+
const firebaseAdminApp = ensureAdminApp(vuefire?.admin?.options)
1010

1111
// TODO: Is this accessible within middlewares and api routes? or should we use a middleware to add it
1212
event.context.firebaseApp = firebaseAdminApp

packages/nuxt/src/runtime/analytics/plugin.client.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import type { FirebaseApp } from 'firebase/app'
22
import { isSupported, initializeAnalytics } from 'firebase/analytics'
3-
import { defineNuxtPlugin, useAppConfig } from '#app'
3+
import { defineNuxtPlugin, useRuntimeConfig } from '#app'
44

55
/**
66
* Plugin to initialize the analytics module.
77
* @experimental: NOT YET RELEASED
88
*/
99
export default defineNuxtPlugin(async (nuxtApp) => {
10-
const appConfig = useAppConfig()
11-
// @ts-expect-error: not implemented yet
12-
const options = appConfig.vuefireOptions.analytics
10+
const runtimeConfig = useRuntimeConfig()
11+
// @ts-expect-error: not implemented yet, needs to be added to the type
12+
const options = runtimeConfig.public.vuefire.analytics
1313
const firebaseApp = nuxtApp.$firebaseApp as FirebaseApp
1414

1515
if (await isSupported()) {

packages/nuxt/src/runtime/app-check/plugin.client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import {
66
AppCheckOptions,
77
} from 'firebase/app-check'
88
import { VueFireAppCheck } from 'vuefire'
9-
import { defineNuxtPlugin, useAppConfig } from '#app'
9+
import { defineNuxtPlugin, useRuntimeConfig } from '#app'
1010

1111
/**
1212
* Plugin to initialize the appCheck module on the server.
1313
*/
1414
export default defineNuxtPlugin((nuxtApp) => {
15-
const appConfig = useAppConfig()
15+
const runtimeConfig = useRuntimeConfig()
1616
// NOTE: appCheck is present because of the check in module.ts
17-
const options = appConfig.vuefireOptions.appCheck!
17+
const options = runtimeConfig.public.vuefire!.appCheck!
1818
const firebaseApp = nuxtApp.$firebaseApp as FirebaseApp
1919

2020
let provider: AppCheckOptions['provider']

packages/nuxt/src/runtime/app/plugin.client.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { initializeApp } from 'firebase/app'
2-
import { defineNuxtPlugin, useAppConfig } from '#app'
2+
import { defineNuxtPlugin, useRuntimeConfig } from '#app'
33

44
/**
55
* Initializes the app and provides it to others.
66
*/
77
export default defineNuxtPlugin(() => {
8-
const appConfig = useAppConfig()
8+
const runtimeConfig = useRuntimeConfig()
99

10-
const firebaseApp = initializeApp(appConfig.firebaseConfig)
10+
// NOTE: the presence of the config is ensured by the module
11+
const firebaseApp = initializeApp(runtimeConfig.public.vuefire!.config!)
1112

1213
return {
1314
provide: {

0 commit comments

Comments
 (0)