Setup and installation

This guide will walk you through setting up Server Side Rendering (SSR) for your Aurelia application from scratch.

Prerequisites

Before setting up SSR, ensure you have:

  • Node.js (version 8.9.0 or later)

  • npm or yarn package manager

  • Existing Aurelia CLI project or willingness to create one

Installing Required Packages

Install the core SSR packages for Aurelia:

npm install --save aurelia-middleware-koa aurelia-ssr-bootstrapper-webpack aurelia-ssr-engine

Additional Dependencies

You'll also need these supporting packages:

npm install --save koa koa-static koa-router jsdom aurelia-pal-nodejs
npm install --save-dev webpack-node-externals

Project Structure

A typical SSR-enabled Aurelia project structure looks like this:

Step 1: Create Server Entry Point

Create a server-specific entry point (src/server-main.ts):

Step 2: Update Client Entry Point

Ensure your client entry point (src/main.ts) is properly configured:

Step 3: Configure Webpack

Update your webpack.config.js to support both client and server builds:

Step 4: Create Server Setup

Create the server file (server/index.js):

Step 5: Configure SSR Server

Create the SSR configuration (server/ssr-server.js):

Step 6: Update HTML Template

Update your index.html to include the placeholder for SSR content:

Step 7: Update Package Scripts

Add build and run scripts to your package.json:

Step 8: Platform Abstraction Layer Setup

Ensure PAL is properly initialized for Node.js in your server entry point:

Development Workflow

  1. Build the application:

  2. Start the server:

  3. For development with auto-rebuild:

Verification

To verify SSR is working:

  1. Start your server and navigate to your application

  2. Disable JavaScript in your browser

  3. Refresh the page - you should still see rendered content

  4. View page source - you should see rendered HTML instead of just the loading template

Next Steps

Once you have basic SSR working:

Troubleshooting

If you encounter issues during setup:

  • Ensure all dependencies are correctly installed

  • Verify webpack configurations are correct for both client and server

  • Check that PAL is properly initialized

  • Review server logs for detailed error messages

  • Consult the Troubleshooting Guide for common issues

Last updated

Was this helpful?