Using the Firebase CLI, you can deploy your Next.js Web apps to Firebase and serve them with Firebase Hosting.
Note: Framework-aware Hosting is an early public preview. This means that the functionality might change in backward-incompatible ways. A preview release is not subject to any SLA or deprecation policy and may receive limited or no support.
Caution: For Next.js developers, new participation in the Hosting frameworks experiment has been closed permanently. If you're already using the frameworks experiment in the Firebase CLI, we recommend "graduating" to App Hosting. With App Hosting, you'll have a unified solution to manage everything from CDN to server-side rendering, along with improved GitHub integration.
After initializing Firebase, you can serve static content with the standard deployment command:
firebase deployIf your app includes dynamic server-side logic, the CLI deploys that logic to Cloud Functions for Firebase. You can view your deployed app on its live site.
The Firebase CLI will detect usage of getStaticProps and getStaticPaths.
When including Firebase JS SDK methods in both server and client bundles, guard
against runtime errors by checking isSupported() before using the product.
Not all products are
supported in all environments.
Tip: consider using ReactFire, which does this for you automatically.
Admin SDK bundles will fail if included in your browser build; refer to them only inside getStaticProps and getStaticPaths.
The Firebase CLI will detect usage of getServerSideProps. In such cases, the CLI will deploy functions to Cloud Functions for Firebase to run dynamic server code. You can view information about these functions, such as their domain and runtime configuration, in the Firebase console.
Using Next.js Image Optimization is supported, but it will trigger creation of a function (in Cloud Functions for Firebase), even if you’re not using SSR.
Note: Because of this, image optimization and Hosting preview channels don’t interoperate well together.
The Firebase CLI respects
redirects,
rewrites, and
headers in
next.config.js, converting them to their
respective equivalent Firebase Hosting configuration at deploy time. If a
Next.js redirect, rewrite, or header cannot be converted to an equivalent
Firebase Hosting header, it falls back and builds a function—even if you
aren’t using image optimization or SSR.
The web framework-aware Firebase deployment tooling will automatically keep client and server state in sync using cookies. There are some methods provided for accessing the authentication context in SSR:
- The Express
res.localsobject will optionally contain an authenticated Firebase App instance (firebaseApp) and the currently signed-in user (currentUser). This can be accessed ingetServerSideProps. - The authenticated Firebase App name is provided on the route query
(
__firebaseAppName). This allows for manual integration while in context:
// get the authenticated Firebase App
const firebaseApp = getApp(useRouter().query.__firebaseAppName);