0

My goal is to have dynamic og: tags, that can be seen by the facebook crawler. By doing some research I figured the best (and probably the only) approach is to prerender my app on the server. However I'm having problems with doing that.

I already have an existing Node.js server which looks a little different from the servers in most online guides.

const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const http = require('http');
const app = express();

// Api for retrieving data from DB
const api = require('./server/api');

// Parsers
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// Angular DIST folder
app.use(express.static(path.join(__dirname, 'dist')));

// Api location
app.use('/api', api);

// Send all other requests to the Angular app
app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, 'dist/index.html'))
})

// Set Port
const port = process.env.PORT || '3040';
app.set('port', port);

const server = http.createServer(app)

server.listen(port, () => console.log('Magic happens on localhost:' + port));

I've tried using prerender.io. I got an API key, installed prerender-node and put this right before redirecting the request to index.html:

app.use(require('prerender-node').set('prerenderToken', 'my-token'));
// Send all other requests to the Angular app
app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, 'dist/index.html'))
})

I also added this to my index.html: <meta name="fragment" content="!">

Nothing changed. Perhaps there's something else I need to do to get it working? Again, my goal is to have dynamic og: tags, that can be seen by the facebook crawler.

Additional info: For now, I'm setting the meta tags using the Meta serivce that comes with Angular 4, if it matters.

EDIT: Demo link if someone wants to test: http://aramet.demo.cdots.bg/news-preview/1

6
  • do you have a test url for us? i would like to take a closer look at the issue. Commented Jan 26, 2018 at 20:00
  • @luschn Yes, I added it into the edit Commented Jan 27, 2018 at 8:56
  • just as i thought, no og tags there. Commented Jan 27, 2018 at 10:29
  • make sure you understand exactly why this is happening: facebook does not parse javascript. not at all. obviously, some angular meta plugin will not solve the problem (because, again, no javascript). prerender.io only works for this if it renders the og tags WITHOUT any javascript. maybe this will help you: github.com/romelgomez/single-page-application-seo Commented Jan 27, 2018 at 10:34
  • 1
    this may also help you: scotch.io/tutorials/angularjs-seo-with-prerender-io Commented Jan 27, 2018 at 10:36

1 Answer 1

0

Can you try moving the:

app.use(require('prerender-node').set('prerenderToken', 'my-token'));

above the static file line like:

app.use(require('prerender-node').set('prerenderToken', 'my-token'));
// Angular DIST folder
app.use(express.static(path.join(__dirname, 'dist')));

Since your index.html file is in your dist folder and you're serving static files from the dist folder, I'm wondering if the static call is serving your index.html file somehow.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried it, but it didn't help. The way my meta tags are set exactly is by making a request to the server with the article ID, it then returns an object and the meta service sets the meta tags as properties of the object. The behaviour I'm expecting from the prerender service is to wait until all that is done, cache the HTML and return the HTML when a crawler comes.
I also have tried adding the articles in the cached pages on the prerender.io site. When I clicked "raw html" under preview to see what's in there (prerender.io/…), it threw an error in my console saying "Mixed Content: The page at '<URL>' was loaded over HTTPS, but requested an insecure stylesheet '<URL>'. This request has been blocked; the content must be served over HTTPS." and it might be part of the issue.
Your first comment is exactly how Prerender works so we should render the page as expected. The second issue you mention is not a problem since we're trying to display your raw HTML through our raw html link. You won't see that error when serving prerendered pages through your middleware. Feel free to email us at [email protected] and I can provide more things to try in order to debug this issue!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.