DEV Community

Cover image for DOM in Angular SSR with Domino package.
Dany Paredes
Dany Paredes

Posted on • Updated on

DOM in Angular SSR with Domino package.

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'))

// 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
Enter fullscreen mode Exit fullscreen mode

Photo by Charl Folscher on Unsplash

Top comments (4)

sabloger profile image
Saeed Falsafin

Where should I add these lines? I tested it in all of the server.ts scopes, but I get "ReferenceError: window is not defined" in all cases.
Is anything wrong with this line?:
global['window'] = window;

omarsalem profile image
Omar Gameel Salem

very helpful! thanks!

laxman2783 profile image

I have tried domino , but we are getting “Not yet Implemented” message
Event is undefined

danywalls profile image
Dany Paredes

Please paste more details to try help u