I just deployed my NextJS app over Firebase Hosting and Firebase Functions. After the deployment is successful, I went to the website but I was greeted with this page.
this is my firebase.json
{
"hosting": {
"public": "public",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
"function": "nextApp"
}
]
},
"functions": {
"source": ".",
"predeploy": [
"npm install",
"npm run build"
],
"runtime": "nodejs12",
"ignore": [
"**/tests/**",
"**/node_modules/**",
"jest.config.js"
]
}
}
my cloud functions for nextJS
// This file is an entry point for Firebase Function to serve NextJS
const functions = require('firebase-functions');
const { default: next } = require('next');
const dev = process.env.NODE_ENV !== 'production';
const app = next({
dev,
conf: {
distDir: '.next',
future: {
strictPostcssConfiguration: false,
excludeDefaultMomentLocales: true,
webpack5: false,
},
},
});
const handle = app.getRequestHandler();
exports.nextApp = functions.region('asia-southeast2').https.onRequest((req, res) => {
console.log('File: ' + req.originalUrl);
return app.prepare().then(() => handle(req, res));
});
I did check Google Cloud Console and made sure that I've added allUsers to the Cloud Function Invoker role, but it isn't helping.
Sorry, silly me I've figured it out. The issue is because I'm deploying the Cloud Function to asia-southeast2 region but Firebase Hosting only able to access Cloud Functions hosted in us-central1.
Related
Is it possible to add a NextJs app as a subdirectory to a ReactJs app on firebase hosting? I have the main app already deployed to firebase with a domain and I want to add a NextJs app to its subdirectory "/blogs" for SEO purposes. Is this possible? If yes how? I have tried rewrites but did not get any success.
Update
I managed to make it work a little with the following:
The directory:
.
+-- _projectFolder
| +-- firebase.json
| +-- .firebaserc
+-- Reactjs Files...
+-- nextApp
| +-- server.js
| +-- Nextjs files...
firebase.json:
"hosting": [
{
"target": "blog",
"public": "./nextApp/public",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
"function": "nextServer"
}
]
},
{
"target": "main",
"public": "build",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "/blogs/**",
"function": "nextServer"
},
{
"source": "/blogs",
"function": "nextServer"
}
]
}
],
"functions": {
"source": "./nextApp",
"runtime": "nodejs16"
}
}
The code for the server.js
const { https } = require("firebase-functions")
const { default: next } = require("next")
const isDev = process.env.NODE_ENV !== "production"
const nextjsDistDir = require("./next.config.js").distDir
const server = next({
dev: false,
//location of .next generated after running -> yarn build
conf: { distDir: nextjsDistDir },
})
const nextjsHandle = server.getRequestHandler()
exports.nextServer = https.onRequest((req, res) => {
return server.prepare().then(() => {
return nextjsHandle(req, res)
})
})
/*
firebase-admin,firebase-functions
require these plugins, install them
*/
The next.config.js has a basePath of basePath: "/blogs"
The only problem now is somehow the next.config.js is not being detected when the build is live on firebase. None of the Image domains or the basePath is working there, these work fine locally, is there any change I need in the server.js file?
Actually the config seems to be fine. You can only add a /** for taking all requests.
{
"hosting": {
"target": "main",
"public": "build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "/blogs/**",
"destination": "/nextApp/index.html"
}
]
}
If this is a nextjs app i would suggest to check that you set the base path correctly -> documentation
If you would like to use multiple different frameworks in one project you might want to consider using Astro.
This video by Fireship explains Astro really well.
For production (mac dmg) builds of my electron app, I am unable to trigger location.reload(), connect to redux-dev-tools, and the sourcemap fails to load.
When the app is loaded, the console warns that it cannot load the sourcemap:
The index.html in the sources says the resource cannot be loaded:
Executing location.reload() causes the app to crash with a white screen and no console logs (this command works in electron dev builds).
My electron code is contained in electron/index.electron.js and the relevant snippets are:
const options = {
icon: join(__dirname, '../src/common/assets/app-icons/png/256x256.png'),
webPreferences: {
nodeIntegration: false,
preload: join(__dirname, "preload.js")
}
};
// ...
mainWindow = new BrowserWindow({
...options,
...windowOptions
});
// ...
const startUrl = process.env.ELECTRON_START_URL || url.format({
pathname: "file://../build/index.html"
});
// ...
mainWindow.loadURL(startUrl);
The relevant package.json snippets:
"homepage": "./",
"scripts": {
"electron-build:mac": "rm -rf dist/ && yarn build && electron-builder -m",
},
"build": {
"appId": "appId",
"extends": null,
"files": [
"dist/**/*",
"build/**/*",
"node_modules/**/*",
"src/common/assets/**/*",
"public/*",
"electron/**/*"
],
"directories": {
"buildResources": "./src/common/assets"
}
},
"devDependencies": {
"electron": "^11.3.0",
"electron-builder": "^22.9.1"
The only thing I can confirm in dev is that running location.reload() successfully reloads the app.
Thanks :)
your url is not valid,
you should change this:
const startUrl = process.env.ELECTRON_START_URL || url.format({
pathname: "file://../build/index.html"
});
to this
const startUrl = process.env.ELECTRON_START_URL || url.format({
pathname: "file://${ __dirname}/build/index.html"
});
I recently started seeing an error on my localhost version of Firebase hosted website in Chrome:
Uncaught FirebaseError: Installations: Missing App configuration value: "appId" (installations/missing-app-config-values).
I'm not sure what has triggered this. I'm using the Firebase Hosting Reserved URL method to include the firebase config and it's had no issues until recently.
The config is loaded via <script src="/__/firebase/init.js"></script>.
This is the config file at that URL:
if (typeof firebase === 'undefined') throw new Error('hosting/init-error: Firebase SDK not detected. You must include it before /__/firebase/init.js');
var firebaseConfig = {
"projectId": "remotesoc...",
"databaseURL": "https://remotesoc...firebaseio.com",
"storageBucket": "remotesoc...appspot.com",
"locationId": "us-central",
"apiKey": "AIzaSyC3K7HT9- ... ",
"authDomain": "remotesoc...firebaseapp.com",
"messagingSenderId": "43697..."
};
if (firebaseConfig) {
firebase.initializeApp(firebaseConfig);
}
As you can see the "appId" value is missing. I'm almost certain this used to be there.
I've built and deployed the app multiple times which is listed as the way to ensure that Firebase has the correct config values for the project.
The app is definitely using the correct project, and has a reference to the hosting site in the firebase.json
{
"database": {
"rules": "database.rules.json"
},
"functions": {
"source": "functions"
},
"hosting": {
"site": "remotesoc...",
"public": "build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
},
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
}
}
I've run >firebase init several times in an attempt to reset the CLI.
The deployed site has no missing config vars and is working perfectly this is only on localhost.
I could revert to using an included config file, however that negates the benefits of having the reserved URLs inject the values for you.
Any help would be appreciated.
When using console.log in a node app deploy on app engine, if i console.log an object, it's all written line by line.
I would like to know if it's possible to have it logged in a clean way (cf picture)
this is the result of console.log('connection: ', connection);
Idealy i would like to have my object connection appear like that :
I'm probably missing something there, if anyone can help me clear that it would be awesome ! thanks
To log objects as one entry in stackdriver, I use the client library.[1]
Using[1] this is how my app.js looks:
'use strict';
const express = require('express');
const {Logging} = require('#google-cloud/logging');
const app = express();
const logging = new Logging();
const log = logging.log('projects/[PROJECT-ID/logs/[ANY-LOG-NAME]');
const resource = {type: 'gae_app',
labels: {
'project_id': '[PROJECT-ID]',
'module_id': '[SERVICE-NAME]',
'version_id': '[ANY-VERSION-ID]',
'zone': '[CURRENT-APPENGINE-REGION]'
}
};
const entry = log.entry({resource},"Plain message one");
let dict1 = {"First name": "Jhon",
"Last name": "Smith",
"Age":32,
"Gender": "Male" };
const secondentry = log.entry({resource},{dict1});
app.get('/', (req, res) => {
console.log('Using logging client library...');
log.write([entry,secondentry]);
res.status(200).send('Printing some messages to the console').end();
});
This is my package.json:
{
"name": "node101",
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"start": "node app.js",
"test": "mocha --exit test/*.test.js"
},
"dependencies": {
"express": "^4.16.3",
"#google-cloud/logging": "^7.3.0"
},
"devDependencies": {
"mocha": "^7.0.0",
"supertest": "^4.0.2"
}
}
It is important to configure properly the resource variable, otherwise your log will not appear.
I have used[2][3] to configure the resource variable, you can look at it for more information about other resources.
I deploy using the --version flag; the value of the flag is equal to the one given resource variable in app.js
gcloud app deploy --version='[ANY-VERSION_ID]'
[1] https://cloud.google.com/logging/docs/api/tasks/creating-logs#writing_log_entries
[2] https://cloud.google.com/monitoring/api/resources#tag_gae_app
[3] https://cloud.google.com/logging/docs/reference/v2/rest/v2/MonitoredResource
I am working a react app where i am using next js and express, I have choose zeit now for servless but when i deploying this facing error Error: No serverless pages were built
next.config.js
const configuration = withTypescript(
withLess({,
target: process.env.NODE_ENV === "development" ? "server" : "serverless",
cssModules: true,
lessLoaderOptions: {
javascriptEnabled: true,
modifyVars: themeVariables // make your antd custom effective
},
exportPathMap: async function(defaultPathMap) {
return {
"/": { page: "/index" },
};
},
webpack: config => {
config.plugins = config.plugins || [];
config.plugins = [
...config.plugins,
// Read the .env file
new Dotenv({
path: path.join(__dirname, "../.env"),
systemvars: true
})
];
return config;
},
})
);
module.exports = configuration;
using next version : "#types/next": "^8.0.0",
now.json
{
"version": 2,
"name": "web",
"builds": [
{ "src": "package.json", "use": "#now/next" }
],
}
inside package json
"scripts": {
"dev": "next src",
"build": "next build src",
"start": "next run build && next start src",
"export": "npm run build && next export src -o ./out",
"now-build": "next build src","
},
Getting error from zeit Now logs
preparing lambda files...
2019-04-19T17:24:04.955Z Error: No serverless pages were built. https://err.sh/zeit/now-builders/now-next-no-serverless-pages-built
at Object.exports.build (/tmp/utils/build-module/node_modules/#now/next/index.js:305:13)
at <anonymous>
please help me here to figure it out.
Thanks.