blog image

Server-Side Rendering in Node.js

I have recently discovered that none of my one client's details pages have ever been indexed. This is because of JavaScript, client-side rendering.

If the search engines can't see the links to the details pages, they will never get indexed.

Server-side rendering to the rescue!

Here is how I rectified the situation:

In my app.get('/example') route (in Node.js with Express) I had the following

                        

const express = require('express'),
app = express(),
db = require('./db'); // database controller

app.get('/example',function(req,res){
    var queryName = 'getExample';
    db.rows(queryName, function(err, rows){
        var data = {
            filename: 'example',
            details:rows
        };
        res.render('index',data);
    });
});
                        
                    

I simply added to the code the logic that used to be on the client-side (with slight modifications):

                        
app.get('/example',function(req,res){
    var queryName = 'getexample';
    db.rows(queryName, function(err, rows){
        var data = {
            filename: 'example',
            details:rows
        };

        data.list = '';
        for(var i = 0; i < rows.length; i++){
            if(rows[i].item_id){
                data.list += '<div style="display:inline-block;width:220px;  padding:5px;  margin:10px;  border:1px solid #DDD; height: 400px;vertical-align:top;position: relative;">' +
                        '<a class="detail" style="color: #777" id="id_' + rows[i].item_id + '" href="/details/'+rows[i].id+'">' +
                        '<img style="height: 200px; width: auto" src="/uploads/' + rows[i].hash + '/' + rows[i].top + '">' +
                        '<div>' + rows[i].title.replace('-/-','-') + '</div>'+
                        '<div style="width:  200px;font-weight: bold; text-align: right; position: absolute; bottom: 10px; right: 10px;">';
                            if(rows[i].length){
                                data.list += '<div style="text-align: right;">' + rows[i].length + 'mm x ' + rows[i].width + 'mm x ' + rows[i].depth + 'mm</div>';
                            }
                            if(parseFloat(rows[i].weight)){
                                data.list += '<div style="text-align: right;" data-id="' + rows[i].item_id + '"><b>' + rows[i].weight.toFixed(2) + ' ct</b></div>';
                            } else {
                                data.list += '<div style="text-align: right;" data-id="' + rows[i].item_id + '"></div>';
                            }
                            data.list += '<div style="text-align: right;""><b>' + rows[i].item_id + '</b></div>' +
                        '</div>'+
                    '</a></div>';
            }
        }
    });
});
res.render('index',data);
                        
                    

And on the client-side, with EJS:

                        
<div id="list">
    <%- list; %>
</div>
                        
                    

Now I have all links visible to the search engines. Hopefully I will get them indexed soon! 🤞

Leave a comment

About Me

I have been developing web sites since 1999. The knowledge and experience I have attained over the years makes me well suited to serve your web development needs, whatever they may be.

I am a husband and we have two awesome adult kids, with one in college (ASU, Pop Music) and a Creative Writer (MFA).

I love what I do and I love my clients. I look forward to serving you!

Contact Me

Your interest and/or feedback is important to me. Please use this form or call if it is urgent. Thank you!

Location

Scottsdale, AZ

Phone

602-334-7771

Make a Payment

This form will take you to PayPal. Please use this form to pay with your credit card. Thank you!

Note: You are making a payment to Web Renegade


Description:



Thank you for your business!