Is it possible to implement SSR (Server-side rendering) for just one standalone component in Angular 17?

130 Views Asked by At

I want to implement Server-side Rendering (SSR) for a single standalone component within an Angular 17 application.

Command used to initialize SSR in the existing project:

Used this command: ng add @angular/ssr

Created a separate module named ssrdemo. Below is the structure of the module files:
Here is the Module file stracture SSR Module Files

Deleted two generated files: app.config.server.ts and main.server.ts Instead, created two separate files called: ssrdemo.config.server.ts ssrdemo.component.server.ts

//ssrdemo.config.server.ts

import { mergeApplicationConfig, ApplicationConfig } from '@angular/core';
import { provideServerRendering } from '@angular/platform-server';
import { appConfig } from '../app.config';

const serverConfig: ApplicationConfig = {
  providers: [
    provideServerRendering()
  ]
};

export const config = mergeApplicationConfig(appConfig, serverConfig);
//ssrdemo.component.server.ts

import { bootstrapApplication } from '@angular/platform-browser';
import { config } from './ssrdemo.config.server';
import { ServerModule } from '@angular/platform-server';
const bootstrap = () => bootstrapApplication(SSRdemoComponentServer, config);

export default bootstrap;

import { Component } from '@angular/core';

@Component({
    selector: 'app-ssrdemo',
    standalone: true,
    imports: [ServerModule],
    templateUrl: './ssrdemo.component.html',
    styleUrl: './ssrdemo.component.css'
})
export class SSRdemoComponentServer {

}

Here's the server.ts file:

//server.ts

import { APP_BASE_HREF } from '@angular/common';
import { CommonEngine } from '@angular/ssr';
import express from 'express';
import { fileURLToPath } from 'node:url';
import { dirname, join, resolve } from 'node:path';
import bootstrap from './src/app/ssrdemo/ssrdemo.component.server';

// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
  const server = express();
  const serverDistFolder = dirname(fileURLToPath(import.meta.url));
  const browserDistFolder = resolve(serverDistFolder, '../browser');
  const indexHtml = join(serverDistFolder, 'index.server.html');

  const commonEngine = new CommonEngine();

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

  // Example Express Rest API endpoints
  // server.get('/api/**', (req, res) => { });
  // Serve static files from /browser
  server.get('*.*', express.static(browserDistFolder, {
    maxAge: '1y'
  }));

  // All regular routes use the Angular engine
  server.get('*', (req, res, next) => {
    const { protocol, originalUrl, baseUrl, headers } = req;

    commonEngine
      .render({
        bootstrap,
        documentFilePath: indexHtml,
        url: `${protocol}://${headers.host}${originalUrl}`,
        publicPath: browserDistFolder,
        providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }],
      })
      .then((html) => res.send(html))
      .catch((err) => next(err));
  });

  return server;
}

function run(): void {
  const port = process.env['PORT'] ?? 4000;

  // Start up the Node server
  const server = app();
  server.listen(port, () => {
    console.log(`Node Express server listening on http://localhost:${port}`);
  });
}

run();

After doing this it's still looking for other component and getting this error.

7:02:33 pm [vite] Internal server error: Worker is not defined
      at eval (d:/olsights-github/olsights-portal-react-sprint-ankit-task-7-8-9-SSRSINGLECOMPONENT/node_modules/weatherlayers-gl/dist/weatherlayers-deck.min.js:14:83602)
      at eval (d:/olsights-github/olsights-portal-react-sprint-ankit-task-7-8-9-SSRSINGLECOMPONENT/node_modules/weatherlayers-gl/dist/weatherlayers-deck.min.js:14:97037)
      at async instantiateModule (file:///D:/olsights-github/olsights-portal-react-sprint-ankit-task-7-8-9-SSRSINGLECOMPONENT/node_modules/vite/dist/node/chunks/dep-9A4-l-43.js:50861:9) (x2)
0

There are 0 best solutions below