Alexander
Alexander

Reputation: 538

Run Firebase NextJS app function on root url

I'm trying to run my NextJS app deployed via Firebase on the root url but it only seems to run on /next, this comes with some issues for my assets' paths and I just want it to run on the root url. If I go to the root url now I get this error:

enter image description here


How do I get it to run on the root url https://europe-west2-myfirstflutterapp-xxxxx.cloudfunctions.net/ instead of https://europe-west2-myfirstflutterapp-xxxxx.cloudfunctions.net/next?

My firebase.json:

{
  "hosting": {
    "public": "./public",
    "rewrites": [
      {
        "source": "**",
        "function": "next"
      }
    ]
  },
  "functions": {
    "source": "functions"
  }
}

My functions/index.js:

'use strict';

const functions = require('firebase-functions');
const next = require('next');
const path = require('path');

const dev = process.env.NODE_ENV !== 'production';
// const app = next({dev, dir: path.join(__dirname, '../'), conf: {distDir: 'next'}});
const app = next({dev, conf: {distDir: 'next'}});
const handle = app.getRequestHandler();

exports.next = functions.region('europe-west2').https.onRequest(async (req, res) => {
  console.log('File: ' + req.originalUrl); // log the page.js file that is being requested
  await app.prepare();
  handle(req, res);
});

Upvotes: 0

Views: 228

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317750

Cloud Functions doesn't let you trigger functions off the root of the hostname allocated for you automatically under "cloudfunctions.net".

If you meant to use Firebase Hosting as a proxy to Cloud Functions, you will have to use your Firebase Hosting domain name instead. That will be either "your-project.firebaseapp.com" or "your-project.web.app" by default. That will use the rewrite rules in your firebase.json.

Upvotes: 2

Related Questions