DOM in Angular SSR with Domino package.

Dany Paredes - Sep 13 '20 - - Dev Community

If you are using angular need to access to DOM with SSR, then use domino package.

Domino provide support for the DOM API and also CSS for querySelector(), querySelectorAll(), and matches().

Install domino from your terminal.

npm install domino --save
Enter fullscreen mode Exit fullscreen mode

Add angular universal in your app.

ng add @nguniversal/express-engine
Enter fullscreen mode Exit fullscreen mode

Edit the server.ts file and add the following lines, for configure it create "document" object and "window" as global.

const domino = require('domino');
const fs = require('fs');
const path = require('path');

// Use the browser index.html as template for the mock window
const template = fs
  .readFileSync(path.join(join(process.cwd(), 'dist/yourprojectname/browser'), 'index.html'))
  .toString();

// Shim for the global window and document objects.
const window = domino.createWindow(template);
global['window'] = window;
global['document'] = window.document;
Enter fullscreen mode Exit fullscreen mode

If you to copy and paste, please change the project name.

Keep in mind change for server side render the build command is build:ssr and need to publish browser directory.

npm run build:ssr
dist/yourprojectname/browser
Enter fullscreen mode Exit fullscreen mode

Photo by Charl Folscher on Unsplash

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player