statiqdev / Statiq.Framework

A flexible and extensible static content generation framework for .NET.
https://statiq.dev/framework
MIT License
421 stars 74 forks source link

How can a query string be read by the destination view? #268

Open ChrisPulman opened 1 year ago

ChrisPulman commented 1 year ago

Hi Dave, I want to pass a query string from a view with a list of for example A B C topics to a view with a list of A by passing a query string in the url i.e. localhost:5080/letter?query=A

I would normally get this from the context however the query string field is null when using Statiq.

  1. Is this supported?
  2. If so how can I use it to filter the topics (topics are views named as per the query string)?

We are using Statiq.Docs V1.0.0-beta.13 to produce an updated documentation website

A little assistance would be greatly appreciated :)

There is an issue also but you may know about this -:

daveaglick commented 1 year ago

I would normally get this from the context however the query string field is null when using Statiq.

The thing to remember here is that anywhere you have C# code (a Razor view, in a pipeline or module, etc.) that code has already been executed by the time any sort of client request is made. This is key to static generation: the code is run and the "static" site assets (I.e. HTML pages, images, JavaScript files, etc.) are all produced and finalized beforehand. This paradigm is quite different to a server-side web framework like ASP.NET where you have access to request-time things like query strings in the code. While the ASP.NET context is provided in Statiq Razor views for compatibility, anything that has to do with a live web request is null or uninitialized.

This means that if you want to interact with anything the client is doing during a request (such as the query strings they're using), you have to do it using JavaScript.

If so how can I use it to filter the topics (topics are views named as per the query string)?

One way to do this would be to generate all the topics on the page, but then use JavaScript to control their display. I personally love Vue.js for stuff like this, but there are plenty of libraries for jQuery and vanilla JavaScript that can display and sort/filter a set of data as well. Then get the query string using JavaScript, and if one is provided, filter the list of topics on the client. Does that make sense?

if you use dotnet run once then there is a few files which are not generated

This definitely isn't a known issue and is certainly some kind of bug that I haven't observed before. The Statiq website itself is generated multiple times a day from a fresh environment via GitHub Actions and, as far as I know, it's never not output a file. Are you able to discern any kind of pattern to the missing files? Is it always the same ones (maybe there's some sort of weird cycle going on where it won't generate file B until file A is on disk)?

ChrisPulman commented 1 year ago

Hi Dave, This is our site so far https://github.com/reactiveui/website/tree/FeatureConvertWyamToStatiq if I run it on my laptop everything generated fine most of the time, when run with Github Actions it always fails. I have commented out the API section for the short term until the Blog (posts) section is successful with Actions. There's 4 files from the theme that are not being output, they are all index pages.