0

I am new to Sveltekit and I am trying to figure out how to handle SSR.

I have a simple Sveltekit application with these configuration:

export const csr = false; export const ssr = true; export const prerender = true;

The main reason for this configuration is to maintain a sustainable SEO. When I set Csr to false, each page is rendered by the server loading Doc to the browser.

I also have an image component <Img> inside which there is an onMount function.

onMount(() => {
console.log('mounted',src);
if (lazyLoad) {
  const images = document.querySelectorAll('img.lazy-load');
  const observer = new IntersectionObserver((entries, observer) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        const image = entry.target;
        image.src = image.dataset.src;
        image.classList.remove('lazy-load');
        observer.unobserve(image);
      }
    });
  });

  images.forEach(image => {
    observer.observe(image);
  });
}});

The main problem here is that this onMount function is never called. Also, beforeUpdate, afterUpdate and if (browser) console.log("browser is true"); do not work either.

I need to find a way to check if dom is loaded while keeping csr set to false.

Main purpose is to have the best SEO, I am open to alternatives.

Rüzgar
  • 947
  • 9
  • 20
  • 1
    There is *no reason* to turn CSR off in first place. The first load will by default be server-side rendered unless SSR is explicitly turned off. Please read the docs and learn what these options do. – H.B. Aug 10 '23 at 13:18
  • Thank you for the quick response. It was already what you suggested when I first started. But if I don't set csr to false and check the Chrome Devtools' `network` tab I only see the home page being fetched. When I click on a subpage link and navigate, I do not see the subpage's `Doc` inside the Network tab. And this makes me concerned that I would lose SEO performance. On the other hand, when I set `csr=false` then every page is being fetched from the server and I can see them all just as if this is a php project. I've been reading the docs and try to understand but am still confused. – Rüzgar Aug 10 '23 at 13:36
  • The whole point of CSR is that client side routing takes over and not everything has to be loaded from scratch on navigation, but that is only the case if the client navigates from a loaded site with JS enabled which hardly will be the case with search engine crawlers; some of them do not execute JS in the first place. I would suggest *not* turning it off preemptively as it just degrades the user experience. Regardless of the setting, search engines will be able to load the page, either with or without JS. Without JS it will just be server-side rendered every time. – H.B. Aug 10 '23 at 15:41
  • Ok, I get it now. Thanks a bunch. – Rüzgar Aug 11 '23 at 08:02

0 Answers0