Skip to content

Add ability to pass in env var into initializeApp#9904

Draft
maneesht wants to merge 8 commits intomainfrom
mtewani/initialize-app-env
Draft

Add ability to pass in env var into initializeApp#9904
maneesht wants to merge 8 commits intomainfrom
mtewani/initialize-app-env

Conversation

@maneesht
Copy link
Copy Markdown
Contributor

  • Allowed json parse-able strings to be passed into initializeApp as so:
initializeApp(process.env.NEXT_PUBLIC_FIREBASE_CONFIG);

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 29, 2026

🦋 Changeset detected

Latest commit: 2270cec

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@firebase/app Minor
firebase Minor
@firebase/app-compat Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for initializing Firebase apps using JSON configuration strings in initializeApp and initializeServerApp. The changes include API updates, parsing logic with error handling, and unit tests. Review feedback suggests validating that the parsed JSON is an object to prevent runtime issues with primitives and correcting a documentation error in a function overload.

Comment thread packages/app/src/api.ts Outdated
Comment thread packages/app/src/api.ts Outdated
Comment thread packages/app/src/api.ts Outdated
Comment thread packages/app/src/api.ts Outdated
Comment thread packages/app/src/api.ts Outdated
Comment thread packages/app/src/api.ts Outdated
Comment thread .changeset/stupid-pans-shave.md Outdated
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 29, 2026

Changeset File Check ✅

  • No modified packages are missing from the changeset file.
  • No changeset formatting errors detected.

Comment thread packages/app/src/api.ts Outdated
*/
export function initializeApp(
options: FirebaseOptions,
options: FirebaseOptions | string,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are the overloads that have "name" as a second argument separated into one whose first arg is options and one whose first arg is a string, but this one is combined?

Comment thread packages/app/src/api.ts Outdated
'Unable to parse FirebaseOptions JSON string.'
);
}
if (typeof parsed !== 'object') {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to check if it's a JS object? This will pass if it's an array. I'm not sure if this check is necessary as many other things could go wrong, and we should try to keep initializeApp as minimal as possible, as every user uses it.

Comment thread packages/app/src/api.ts Outdated
);
}
if (typeof parsed !== 'object') {
throw new FirebaseError(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The firebase/app package doesn't instantiate FirebaseErrors directly but uses ERROR_FACTORY which uses a predefined error message template which has slots for certain strings to be inserted. The message probably wouldn't work for this case and is actually somewhat outdated. If you want to turn this PR over to me, I can rework it.

"firebase": minor
---

Add ability to pass in a Node environment variable into `initializeApp` and `initializeServerApp`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Add option to pass Firebase config as a JSON string into initializeApp and initializeServerApp."

Comment thread packages/app/src/api.ts
if (typeof optionsOrJsonConfigString === 'string') {
let parsed: unknown = undefined;
try {
parsed = JSON.parse(optionsOrJsonConfigString);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this intermediate variable? Can't we just options = JSON.parse(optionsOrJsonConfigString)? Also does it need to be || undefined below? Does it mess something up if it's falsy but not specifically undefined?

[AppError.INVALID_SERVER_APP_ENVIRONMENT]:
'FirebaseServerApp is not for use in browser environments.'
'FirebaseServerApp is not for use in browser environments.',
[AppError.INVALID_JSON_CONFIG]: 'Unable to parse FirebaseOptions JSON string.'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's show them the bad string. Use a template variable (it's a very weird bespoke format but you can see it in {$appName} and {$originalErrorMessage} above) and provide the bad string as the second arg when you call ERROR_FACTORY.create on this error type.

expect(app.options).to.deep.equal(config);
});

it('creates DEFAULT App with JSON config string and config object', () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"and config object" - no config object in this test, right?

expect(app.name).to.equal(DEFAULT_ENTRY_NAME);
expect(app.options).to.deep.equal(config);
});
it('creates DEFAULT app with config object as the second parameter', () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one has a name, not DEFAULT.

/Unable to parse FirebaseOptions JSON string/
);
});
it('creates FirebaseServerApp with options as first parameter and config object with automaticDataCollectionEnabled as second parameter', async () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this identical to the test in 290? This one has a more specific description though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants