How do I conditionally turn off pre-render for 2 Blazor pages

David Thielen 3,036 Reputation points
2024-08-23T16:42:08.8266667+00:00

Hi all;

I have a Blazor InteractiveServer application using .NET 8.

The Google web crawler gets just the static html for a page, not waiting for Blazor to create the SignalR circuit and therefor not waiting for OnInitializeAsync() to read from the DB and populate the page.

I have 2 pages that I need to have indexed with their full content. These pages are unique due to a parameter in the url ?id=123 and the sitemap.txt lists thousands of these 2 pages.

My question is first, how do I determine if the page is being read by a crawler? (And if this is not possible, then I can always have these 2 pages in the fully render on first request mode.)

And second, how do I then put that page in a render mode where what is first returned is the fully populated page as static HTML.

It's fine if after the page is fully rendered as static HTML it then re-renders it again as an interactive page with SignalR set up.

I have found this info on pre-rendering, but it's not clear where it goes or if this is what I need. And nothing about how to only do it if a crawler has requested a page.

thanks - dave

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,559 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 64,566 Reputation points
    2024-08-23T21:53:31.83+00:00

    There are two ways to detect the crawler.

    check the user agent,

    https://developers.google.com/search/docs/crawling-indexing/overview-google-crawlers

    Include a parameter on sitemap links and robots.txt.

    note: you do not want to turn off pre-render, but query and display the data in pre-render. The crawler can only access pre-render. If you turned off pre-render, it would only see the host page without any Blazor content.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.