Skip to content

Instantly share code, notes, and snippets.

@sagarsoni7
Last active August 8, 2021 06:39
Show Gist options
  • Save sagarsoni7/92bd8caea143ddeedbf4daff35d9d8fa to your computer and use it in GitHub Desktop.
Save sagarsoni7/92bd8caea143ddeedbf4daff35d9d8fa to your computer and use it in GitHub Desktop.
Deploying Hazel server as sub route (for Electron auto updates) on Firebase Functions & Hosting

Deploy Vercel Hazel on Firebase and access it like: mySoftwareWebsite.com/get

Path "/get" can be replaced with your desired path

Related: https://github.com/vercel/hazel

const functions = require("firebase-functions");
const hazel = require("hazel-server"); // Run "npm i hazel-server" in your functions/ directory first
const express = require("express");
const cors = require("cors");
const app = express();
// Middlewares
app.use(cors());
app.use((req, res, next) => {
// Remove trailing slash
if (req.url.slice(-1) === "/") {
req.url = req.url.slice(0, -1);
}
// Redirect "/get" to "/" since Hazel needs root url ie. "/"
req.url = req.url.replace("/get", "/").replace("//", "/");
// Forward request to the handler
next("route");
});
// Internal routes are handled by Hazel
app.get("*", (req, res) => {
hazel({
//Git repo where releases are published
account: YOUR_GITHUB_USERNAME,
repository: YOUR_GITHUB_REPO_NAME,
})(req, res);
});
module.exports.fetchElectronReleases = () => {
return functions.https.onRequest(app);
};
// ++ Existing content
"hosting":[
{
"target": "YOUR_FIREBASE_SITE",
"public": "YOUR_PUBLIC_DIR",
"rewrites": [
{
"source": "/get/**",
"function": "fetchElectronReleases" //This will execute given firebase function on this hosting route
},
{
"source": "/get",
"function": "fetchElectronReleases"
},
{
"source": "**",
"destination": "/index.html"
}
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment