JavaScript Best Practices

JavaScript Best Practices — More SEO

Spread the love

Like any kind of apps, JavaScript apps also have to be written well.

Otherwise, we run into all kinds of issues later on.

In this article, we’ll look at some best practices we should follow when writing JavaScript code.

Use Meta Robots Tags Carefully

We can add the noindex meta tag to prevent indexing.

We can add a static meta tag with:

<meta name="robots" content="noindex, nofollow">

And we can add one dynamically with:

  .then(response => response.json())
  .then(item=> {
    if (item.exists) {
    } else {
      const metaRobots = document.createElement('meta'); = 'robots';
      metaRobots.content = 'noindex';

We create the meta element, add the attributes, and attach it to the head tag with document.head.appendChild .

If Google encounters noindex , then the JavaScript won’t run.

It’ll index the page if the meta tag with noindex is removed.

Use Long-Lived Caching

We should cache assets to reducing loading time.

This may lead to outdated resources being loaded.

To clear the cache when new content is deployed, we can add a unique fingerprint to the file name of them.

Use Structured Data

We can add structured data with a script tag:

<script type="application/ld+json">
    "@context": "",
    "@type": "Recipe",
    "name": "{{recipe_name}}",
    "image": [ "{{recipe_image}}" ],
    "author": {
      "@type": "Person",
      "name": "{{recipe_author}}"

This lets Google know the content of our page.

The type has to be set to “application/ld+json” for this to work.

Follow Best Practices for Web Components

We should make sure that Google sees the rendered content of our web component.

If it’s not visible, then Google can’t index it.

To test them, we can use the Mobile-Friendly Test or the URL Inspection Tool to look at the rendered HTML.

To make sure the light DOM and shadow DOM are both rendered, we should use the slot element:

  class MyComponent extends HTMLElement {
    constructor() {
      this.attachShadow({ mode: 'open' });

    connectedCallback() {
      let p = document.createElement('p');
      p.innerHTML = 'Hello World: <slot></slot>';

window.customElements.define('my-component', MyComponent);

  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  <p>Suspendisse vitae eleifend nulla.</p>

We have the slot in the shadow DOM to render the items between the tags.

Then everything will show.

Fix Images and Lazy-Loaded Content

If we have images, we should lazy load them so that they only show when they’re displayed to the user.

This reduces load time so users and Google can see the rendered content faster.


We need a title on our pages.

We can add a title tag:

   <title>Page Title</title>

This lets Google identify the page easily.


Also, we can add the meta description to describe our content to search engines.

For example, we can write:

   <meta name="description" content="description of the page" />

Open Graph Image

To make our page show a featured image for social media, we need the open graph image tag.

For example, we can write:

   <meta property="og:image" content="https:/" />


We can improve our web app’s ranking with lazy loading, caching, and following best practices with web components.

Also, we should look add a few meta tags for search engines and social media.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *