Skip to content

📋 Angular app to deploy to Firebase functions as Server Side rendered

Notifications You must be signed in to change notification settings

AndrewJBateman/angular-material-ssr

Repository files navigation

Angular Material SSR

  • Angular app deployed to Firebase functions with Server Side Rendering.
  • Progressive Web App
  • Optimised for an excellent Lighthouse score
  • Note: to open web links in a new window use: ctrl+click on link

GitHub repo size GitHub pull requests GitHub Repo stars GitHub last commit

📄 Table of contents

📚 General info

  • Firebase functions used to serve Node.js SSR app
  • Dist folder contains functions folder with browser and server SSR code.
  • Functions folder contains a copy of dependencies from the src package.json file
  • App contains Angular Material card to see effect on rendering speed.
  • Roboto font style: Regular 400 used

📷 Screenshots

Frontend screenshot

📶 Technologies

💾 Setup

  • Create project in Firebase console then add functions. This can all be done from firebase init in VS COde

  • npm i to install dependencies

  • npm prune to remove unused npm modules

  • ng serve for a dev server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.

  • ng update to update Angular

  • Run npm run dev:ssr to see SSR app on a dev server

  • Run npm run build:ssr to build SSR project. The browser & server build folders will be stored in the dist/functions directory.

  • cd dist/functions then run firebase emulators:start for Firebase emulator

  • From /dist/functions run npm run deploy to deploy app to firebase functions & hosting

💻 Code Examples

  • server.ts Express app exported so that it can be used by serverless Functions.
//
export function app(): express.Express {
  const server = express();
  const websiteFileLocation = environment.production
    ? 'browser'
    : 'dist/functions/browser';
  const distFolder = join(process.cwd(), websiteFileLocation);
  const indexHtml = existsSync(join(distFolder, 'index.original.html'))
    ? 'index.original.html'
    : 'index';

  // Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
  server.engine(
    'html',
    ngExpressEngine({
      bootstrap: AppServerModule,
    })
  );

  server.set('view engine', 'html');
  server.set('views', distFolder);

  server.get(
    '*.*',
    express.static(distFolder, {
      maxAge: '1y',
    })
  );

  server.get('*', (req, res) => {
    res.render(indexHtml, {
      req,
      providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }],
    });
  });

  return server;
}

🆒 Features

  • Excellent Lighthouse score

📋 Status & To-Do List

  • Status: Working SSR & PWA. Deployed to Firebase Functions
  • To-Do: Use to create an actual app with content. Update sitemap & robots.txt

👏 Inspiration

📁 License

  • This project is licensed under the terms of the MIT license.

✉️ Contact

  • Repo created by ABateman, email: gomezbateman@yahoo.com