firebaseexpressgoogle-cloud-functionsangular-universalresponse.redirect

firebase cloud function redirect with express


I'm trying to automatically determine which language of the website should be user redirected to.

Setup is Firebase cloud function with ExpressJS server and Angular Universal SSR.

When preferred language is determined from request.acceptsLanguages(...) I'm trying to redirect via response.redirect('/en');.

When debugging it locally via firebase serve it redirects, but when deployed it doesn't seem to work at all, even logs from that endpoint don't show in the log list.

// All base routes are redirected to language specific
app.get('/', (req, res) => {
  console.log('this is /');

  if (req.acceptsLanguages('cs', 'cs-CZ', 'sk', 'sk-CZ')) {
    res.redirect(`/cs`);
  } else {
    res.redirect(`/en`);
  }
});

// All regular routes use the Universal engine
app.get('*', (req, res) => {
  console.log('this is *');

  res.render('index', { req });
});

export const ssr = functions.https.onRequest(app);

Solution

  • The problem was in the index.html being served as a static file on the / route.

    I resolved it by renaming the Angular's index.html to web-index.html (any descriptive name will do).

    It could also be solved by configuring the Express static file server to not serve index.html files if found for the matching routes, but that would disable serving any pages that have been pre-rendered (if you have SSR prerendering setup).