⚡️ Optimize SEO with Javascript
Disclaimer:
JavaScript can be problematic for search engine optimization because search engine crawlers can’t always process it well. However, if you follow best practices, JavaScript can effectively help improve your website’s SEO.
The WebDev community on Stackoverflow agrees that Google crawlers don’t “see” your website’s JavaScript, they only examine static, non-interactive content.
If you want to know what your website looks like without Javascript, use a No-Js browser plugin. I use Toggle Javascript. Deactivate Javascript on your website and you’ll be looking at your website from the perspective of a Google crawler.
Nevertheless, JavaScript can support the SEO of your website if it is used correctly. Here are some tips on how you can use JavaScript to make your website more SEO friendly.
Tip1 : Server-Side Rendering (SSR)
Search engines can have difficulty rendering dynamic JavaScript content. With SSR, the HTML code is generated on the server and sent directly to the browser.
How SSR works?
With server-side rendering (SSR), websites are pre-rendered on the server, rather than being rendered in the browser.
The server retrieves all the necessary data (from a database or API) and renders the complete HTML code for the page, with the data already inserted, and sends this finished HTML code to the client. After the page has been loaded in the user’s web browser, it is extended by JavaScript so that it becomes interactive without the need for a full reload. This is called hydration.
Compared to client-side rendering, SSR offers shorter loading times as the server does the hard work of rendering the content before delivering it to the browser.
Sequence Diagram of Server Side Rendering (SSR), created with mermaidchart.com
Tip 2: Pre-rendering
If you are building an SPA (Single Page Application) that makes heavy use of JavaScript, pre-rendering could help. The website is generated in advance as static HTML and easily indexed by search engines.
Tip 3: Avoid blocking rendering with JavaScript
Blocking scripts delay page display and increase loading times, which can have a negative impact on SEO ranking.
Solution: Use async
or defer
attributes in <script>
tags to execute non-critical JavaScript files only after the page has loaded.
<script src=“script.js” defer></script>
Tip 4: Care for correct meta tags for dynamic content
Meta tags such as <title>
and <meta description>
are important ranking factors. With dynamic content, this information must be correctly integrated into the page.
With libraries such as React Helmet or Vue Meta you can manage dynamic meta tags on every page so that search engines capture the relevant information.
Tip 5: Use structured data with JSON-LD
Structured data (Schema.org) allows search engines to better understand the content of your website, which can lead to rich snippets in search results.
Solution:** Use JavaScript to dynamically insert JSON-LD data into the <head>
section of your page.
Example:
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Organization",
"name": "WebDev Guide",
"url": "https://www.webdev-guide.com"
}
</script>
Tip 6: Lazy loading for images and videos
Lazy loading is a technique in which content such as images, videos or scripts are only loaded when they are actually needed, i.e. when they reach the visible area of the screen (viewport). This improves the loading speed and performance of a website, as only the resources that the user sees immediately are loaded.
Example: Lazy loading of an image with HTML
Example:
<img src="image-placeholder.jpg" data-src="actual-image.jpg" alt="Lazy Loaded Image" loading="lazy">
By using the property loading="lazy"
, the image is only loaded when it comes into the user’s visible area.
Lazy loading is good for performance, but not for content that is important for search engine optimization, such as the main text, headings and metadata. Make sure your core content is visible and indexable when the page first loads, or make sure search engine bots can access it.
Loading large media such as images and videos can affect page load time, which impacts your SEO.
Use Lazy Loading to load images and videos only when they appear in the user’s visible area. Use HTML attributes such as loading="lazy"
or JavaScript solutions.
Instead of downloading all elements of a page on first load, content is dynamically reloaded as soon as the user scrolls on the page. This reduces the initial loading time and saves bandwidth, which is particularly advantageous on mobile devices.
Tip 7: Use history.pushState
for SPAs
Single Page Applications (SPAs) dynamically change the content of the page without updating the URL, which can confuse search engines.
Solution: Use history.pushState
or replaceState
in JavaScript to change the URL when the page content changes so that each page is recognized by search engines.
Example:
window.history.pushState({}, 'Title', '/new-url');
Tip 8: Make sure you have good page performance
Since the introduction of Core Web Vitals by Google, page loading speed is an important ranking factor. Optimize your JavaScript files by using code splitting and delaying unnecessary scripts. Use browser caching and minify your JavaScript to reduce loading times.
Tip 9: Pay attention to mobile-first SEO
Google uses mobile-first indexing, which means that the mobile version of your website is ranked first.
Solution:Make sure your JavaScript is optimized for mobile devices and that all content is accessible and user-friendly on mobile devices. JavaScript-based menus and interactions should work smoothly on mobile devices.
Tip 10: Use canonical URLs
When JavaScript creates dynamic URLs, it can lead to duplicate content, which hurts SEO.
Use canonical tags in your pages to make it clear which URL is the main version.
Example:
<link rel="canonical" href="https://www.webdev-guide.com/main-page">
SEO in Node.js and Svelte
Both Node.js and Svelte are popular web development frameworks that deal with SEO in different ways. Here’s a closer look at how they work and what you should keep in mind if you want to optimize SEO with these frameworks.
1. Node.js
Node.js is a server-side JavaScript runtime environment that allows JavaScript code to be executed on the server. Node.js is often used as the basis for server-side applications and frameworks such as Express.js and Next.js.
SEO optimization with Node.js
Since Node.js works on the server side, there are several ways to support SEO:
-
Server-Side Rendering (SSR): With Node.js, you can render content on the server and send HTML to the browser instead of relying solely on client-side rendering, as is often the case with pure SPAs (Single Page Applications). This ensures that search engine bots can see the full HTML content of the page instead of relying on client-side JavaScript.
-
Example framework: Next.js (React): Next.js is based on Node.js and supports SSR. It generates ready-made HTML pages on the server that can be easily crawled by search engines.
-
Pre-rendering: Node.js applications can also be configured for pre-rendering, where static HTML pages are generated and sent to the clients. This prevents the browser from having to process JavaScript to make the content visible.
-
Express.js & SEO**: Express.js is a minimalist Node.js framework that has no specific SEO features, but allows SSR or pre-rendering in combination with other tools. You can incorporate meta tags, canonical URLs and structured data markup into the server-side logic to improve SEO.
Node.js SEO tips:
- Use SSR or Pre-rendering to ensure content is visible to crawlers.
- Make sure that dynamically generated pages are equipped with the correct meta tags and structured data.
- Pay attention to page performance as loading speed is an important SEO factor (e.g. minify JavaScript, use caching).
2. Svelte
Svelte is a modern frontend framework that differs from other frameworks such as React or Vue in that it has no runtime overhead. Instead, the code is compiled into high-performance JavaScript during the build process, which is particularly good for performance.
SEO optimization with Svelte
Svelte renders on the client side by default, which can lead to SEO problems, as search engines have difficulty crawling content that only appears after JavaScript has been executed. However, there are solutions for building SEO-friendly applications with Svelte.
-
Sapper (the predecessor of SvelteKit): Sapper was the official framework for SSR and pre-rendering in Svelte. It has been replaced by **SvelteKit, which offers better ways to optimize SEO, including SSR and pre-rendering.
-
Server-side rendering (SSR) with SvelteKit: With **SvelteKit, you can render content server-side so that search engines can see the entire HTML content of the page. This avoids problems that can occur if content is only reloaded on the client side via JavaScript.
-
Pre-rendering in SvelteKit**: SvelteKit provides a built-in static page generation feature where certain pages are pre-rendered in the build process and served as static HTML files. This is ideal for SEO as the pages can be fully loaded without JavaScript.
Svelte SEO tips:
- Use SvelteKit to enable SSR or pre-rendering. This ensures that all important content is indexable for search engines.
- Implement dynamic meta tags and structured data in the rendered pages.
- Use Lazy Loading for images and videos to improve loading times and optimize SEO factors such as Core Web Vitals.
Comparison: Node.js vs. Svelte for SEO
Aspekt | Node.js | Svelte (with SvelteKit) |
---|---|---|
Server-Side Rendering | Well supported with frameworks like Next.js or Express.js | Fully supported with SvelteKit |
Pre-rendering | Can be realized by static site generators or tools like Next.js | Integrated in SvelteKit, allows pre-rendering of pages |
Page Performance | Good for server-side optimizations | Excellent due to compiled JavaScript and low runtime |
Meta tags and SEO | Supported by SSR frameworks that render dynamic content | Meta tags and SEO optimizations are possible with SvelteKit |
Crawling & Indexing | Good through SSR and pre-rendering | Also good through SvelteKit, but pure Svelte (client-side) needs tools such as pre-rendering |
Conclusion
Node.js is particularly strong for server-side rendering if you use frameworks like Next.js or Express.js. You can perform complex SEO optimizations and have full control over the server-side logic. Svelte, especially with SvelteKit, offers very good performance and supports both SSR and pre-rendering. For SEO, however, it is important to use SvelteKit as it provides all the tools to render pages efficiently and make them indexable.
Both frameworks can be optimized for SEO as long as you use server-side rendering or pre-rendering and make sure your content, meta data and structured data is available for search engines.